Please have a look at he following code
JSP
<%--
Document : index
Created on : Nov 27, 2012, 1:11:48 PM
Author : Yohan
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div>
<div>Content for New Div Tag Goes Here</div>
<p> </p>
<p> </p>
<p> </p>
<div>
<form method="post" action="FileSelector" enctype="multipart/form-data">
Select File: <input type="file" name="location"/></div>
<br>
<input type="submit" value="Submit"/>
</form>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
</body>
</html>
Servlet
package importWizard;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileSelector extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
PrintWriter pw = response.getWriter();
File location = (File)request.getParameter("location");
pw.write(location);
}
}
As you can see, I am unable to send the file from JSP to Servlet. I don’t need to send the file, but at least the complete location of the file (It is only sending the file name). How can I send the file or the complete location of the file from JSP to servlet?
That’s not possible using standard HTML
<input type="file">element. It sends merely the entire file contents along with the filename as that’s basically the only way for the server to get the file contents. The server has namely no direct access to the client’s local disk file system, so the client’s absolute disk file system path as sole information would have been useless. Note that the MSIE browser will due to a security bug send the full absolute client side disk file system path along instead of only the filename, but that’s thus not how things are supposed to work.If you really need to have only the client’s absolute disk file system path, then your best bet is to create a (signed) applet or webstart application which obtains it by
JFileChooserand finally embed it in the web page.See also: