why eclipse browser able to upload and read image file while out side browser not ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rspvsanjay
    New Member
    • Sep 2016
    • 21

    why eclipse browser able to upload and read image file while out side browser not ?

    I have three code file in which two are the jsp file and one servlet file created as follow :

    servlet file UploadServlet.j ava


    package net.codejava.se rvlet;

    import java.io.File;
    import java.io.IOExcep tion;

    import javax.servlet.S ervletException ;
    import javax.servlet.a nnotation.Multi partConfig;
    import javax.servlet.a nnotation.WebSe rvlet;
    import javax.servlet.h ttp.HttpServlet ;
    import javax.servlet.h ttp.HttpServlet Request;
    import javax.servlet.h ttp.HttpServlet Response;
    import javax.servlet.h ttp.Part;

    @WebServlet("/UploadServlet")
    @MultipartConfi g(fileSizeThres hold=1024*1024* 2, // 2MB
    maxFileSize=102 4*1024*10, // 10MB
    maxRequestSize= 1024*1024*50) // 50MB
    public class UploadServlet extends HttpServlet {

    /**
    * Name of the directory where uploaded files will be saved, relative to
    * the web application directory.
    */
    private static final String SAVE_DIR = "uploadFile s";

    /**
    * handles file upload
    */
    protected void doPost(HttpServ letRequest request,
    HttpServletResp onse response) throws ServletExceptio n, IOException {
    // gets absolute path of the web application
    String appPath = request.getServ letContext().ge tRealPath("");
    // constructs path of the directory to save uploaded file
    String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala";

    // creates the save directory if it does not exists
    File fileSaveDir = new File(savePath);
    if (!fileSaveDir.e xists()) {
    fileSaveDir.mkd ir();
    }
    System.out.prin tln(savePath);
    //savePath="C:\\U sers\\SANJAY GUPTA\\Download s\\eclipse";
    for (Part part : request.getPart s()) {
    String fileName = extractFileName (part);
    // refines the fileName in case it is an absolute path
    fileName = new File(fileName). getName();
    part.write(save Path + File.separator + fileName);
    }

    request.setAttr ibute("message" , "Upload has been done successfully!") ;
    getServletConte xt().getRequest Dispatcher("/message.jsp").f orward(
    request, response);
    }

    /**
    * Extracts file name from HTTP header content-disposition
    */
    private String extractFileName (Part part) {
    String contentDisp = part.getHeader( "content-disposition");
    String[] items = contentDisp.spl it(";");
    for (String s : items) {
    if (s.trim().start sWith("filename ")) {
    return s.substring(s.i ndexOf("=") + 2, s.length()-1);
    }
    }
    return "";
    }
    }


    first jsp file index.jsp



    <%@ page language="java" contentType="te xt/html; charset=ISO-8859-1"
    pageEncoding="I SO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>File Upload</title>
    </head>
    <body>
    <center>
    <h1>File Upload</h1>
    <form method="post" action="UploadS ervlet"
    enctype="multip art/form-data">
    Select file to upload: <input type="file" name="file" size="60" /><br />
    <br /> <input type="submit" value="Upload" />
    </form>
    </center>
    </body>
    </html>


    second jsp file message.jsp



    <%@ page language="java" contentType="te xt/html; charset=ISO-8859-1"
    pageEncoding="I SO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Upload </title>
    </head>
    <body>
    <h2>${requestSc ope.message}</h2>
    <%@ page import="java.io .File"%>
    <%
    String SAVE_DIR = "uploadFiles";S tring appPath = request.getServ letContext().ge tRealPath("");
    String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala"+ File.separator;

    out.write("<h3> This is the Profile Picture</h3><br><br><but ton type=\"submit\" name=\"img\">"+
    "<img src=\""+savePat h+"333.jpg\" style=\"width:3 04px;height:228 px;\">"+
    "</button>");

    %>
    </body>
    </html>

    to run these files code I am using eclipse IDE, inside eclipse IDE I am able to view the 333.jpg image and able to upload other file, but when I copy the local host URL from eclipse and running the program by using chrome browser I am able to upload file but can not able to view the 333.jpg image file which is uploaded already into given path.

    I want to know why that is happening and what is the solution for it?
Working...