0

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

servlet file UploadServlet.java

package net.codejava.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/UploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
                 maxFileSize=1024*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 = "uploadFiles";

    /**
     * handles file upload
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // gets absolute path of the web application
        String appPath = request.getServletContext().getRealPath("");
        // 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.exists()) {
            fileSaveDir.mkdir();
        }
        System.out.println(savePath);
        //savePath="C:\\Users\\SANJAY GUPTA\\Downloads\\eclipse";
        for (Part part : request.getParts()) {
            String fileName = extractFileName(part);
            // refines the fileName in case it is an absolute path
            fileName = new File(fileName).getName();
            part.write(savePath + File.separator + fileName);
        }

        request.setAttribute("message", "Upload has been done successfully!");
        getServletContext().getRequestDispatcher("/message.jsp").forward(
                request, response);
    }

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

first jsp file index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-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="UploadServlet"
        enctype="multipart/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="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-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>${requestScope.message}</h2>
    <%@ page import="java.io.File"%>
    <%
     String SAVE_DIR = "uploadFiles";String appPath = request.getServletContext().getRealPath("");
     String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala"+ File.separator;

     out.write("<h3>This is the Profile Picture</h3><br><br><button type=\"submit\" name=\"img\">"+
                "<img src=\""+savePath+"333.jpg\" style=\"width:304px;height:228px;\">"+
                "</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?

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
sanjay gupta
  • 29
  • 1
  • 6
  • i found the solution just replace request.getServletContext().getRealPath(""); with request.getContextPath(); then that will work – sanjay gupta Jun 13 '17 at 08:07
  • Please add codejava.net to your Internet blacklist. Related: https://stackoverflow.com/q/1958808 – BalusC Jun 13 '17 at 08:23

1 Answers1

0

just change message.jsp file

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-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>${requestScope.message}</h2>
<%@ page import="java.io.File"%>
<%
 String SAVE_DIR = "uploadFiles";
 String appPath =  request.getContextPath(); 
 String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala"+ File.separator;

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

%>

sanjay gupta
  • 29
  • 1
  • 6