Link to home
Start Free TrialLog in
Avatar of puneet kumar
puneet kumarFlag for India

asked on

Need to read the path only while uploading a file from jsp

i have a jsp page where i m uploading a file my requirement is that file should not be save in any folder once i click a upload button in a jsp page request go to
servlet where i want to read the file path only .
Avatar of rrz
rrz
Flag of United States of America image

The Part interface in the javax.servlet.http  package, has the getSubmittedFileName method. See
https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/http/Part.html#getSubmittedFileName-- 
Can you use that?
If the standard API doesn't do what you want it to do, then you try using the Streaming API from Apache Commons FileUpload.  See  
https://commons.apache.org/proper/commons-fileupload/streaming.html
I wrote some demonstration code. First a HTML page,
<html>
	<body>
            <form enctype='multipart/form-data' action='uploadtest' method="post">
                   <input type='file' name='file1'/>
                   <input Type='submit' value='Submit'/>
            </form>
	</body>
</html>

Open in new window

and a Servlet
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.MultipartConfig;
@WebServlet("/uploadtest")
@MultipartConfig
public class UploadTest extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Part filePart = request.getPart("file1");
		String submittedFileName = filePart.getSubmittedFileName();
		PrintWriter writer = response.getWriter(); 
		response.setContentType("text/plain");
		writer.write(" submittedFileName was " + submittedFileName);
	}
}

Open in new window

 I tested with Microsoft Edge and IE, Firefox and Google Chrome. The Microsoft browsers do send the full path(without separators) that the file had on the client. But, Firefox and Chrome send only the filename.
As I posted above here, the getSubmittedFileName method seems to remove the file separators from the path(sent by Microsoft browsers).
If you want the path with separators, then you could get it from response header as I have done in the following code.
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.MultipartConfig;
@WebServlet("/uploadtest")
@MultipartConfig
public class UploadTest extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Part filePart = request.getPart("file1");
		String submittedFileName = filePart.getSubmittedFileName();
		PrintWriter writer = response.getWriter(); 
		response.setContentType("text/plain");
		writer.write(" submittedFileName was " + submittedFileName + "<br/>");
		writer.write(" from header filename was " + getFileName(filePart));
	}
	private String getFileName(Part part) {
        String contentDisp = part.getHeader("content-disposition");
        System.out.println(" content-disposition header is " + contentDisp);
        String[] tokens = contentDisp.split(";");
        for (String token : tokens) {
            if (token.trim().startsWith("filename")) {
                return token.substring(token.indexOf("=") + 2, token.length()-1);
            }
        }
        return " none sent";
    }
}

Open in new window

Avatar of puneet kumar

ASKER

Hi above gives file name only but i said earlier i want file path . suppose your file in C:\\Test\\Test.csv location . i want this location on servlet without uploading file in any folder . jsp there is a upload option i browse the file request go to servlet then i need path of the file . let me know if you need more clarification.basically need to read a file while uploading but not save to any folder.
Hi above gives file name only but i said earlier i want file path
 Which browser did you use to test?
As I posted above here, the Firefox and Chrome browsers do not send the file's path to the server. For those browsers you could ask the client to type the path into a text input field. The Microsoft browsers do send the path to the server.
.basically need to read a file while uploading but not save to any folder.
You probably will have to use the Streaming API as I posted above here. For the standard API, Tomcat automatically uses its temp folder to store the file temporarily. Is that a problem?
please provide some code regarding chrome and Firefox browser.want to implement for non Microsoft browser or give me some solution that file will be upload some type of temp folder but it will delete the file as well folder after reading the file.
Which servlet container are you using? Tomcat?  
What does
tempdir is <%=System.getProperty("java.io.tmpdir")%>

Open in new window

print from a JSP?
Jboss I m using
I don't use JBoss.
let me know if you need more clarification.basically need to read a file while uploading but not save to any folder.  

What type of file?
How are you reading it?
any kind of file it can be csv it can be xls any type . i am reading with input stream . or one more option is there while uploading a file read the browse the path in jsp with the help of java script or jquery and send to servlet.
is there while uploading a file read the browse the path in jsp with the help of java script or jquery and send to servlet.
 As far as I know, the browser will not allow this because of security concerns.  I am not a Javascript expert. Maybe an expert will comment.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.