To pass the path of the uploaded file to the Java program
I have created two JSP programs as follows.
First One: Addmulti.jsp
<html>
<head><title>Upload Excel File</title><开发者_运维百科;/head></p> <p><body>
<form action="Test2.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<br><br>
Upload File:
<input name="file" type="file" id="file"><br><br>
<input type="submit" name="Submit" value="Submit"/><br><br>
<input type="reset" name="Reset" value="Reset"/>
</form>
</body>
</html>
Second one: Test2.jsp
<%@ page import="myfirst.*" %>
<%
String filevar=request.getParameter("file");
String result="";
myfirst.SearchLink z=new myfirst.SearchLink();
result=z.addmultiple(filevar);
System.out.println(result);
out.println(result);
%>
SearchLink is a java program which contains the method as below,
public String addmultiple(String file)throws SQLException{
return file;
}
I want the complete path of the file that is selected in the first jsp program mentioned above to be transmitted to the java method named addmultiple(String). Instead, null is getting printed in the browser as output once the Test2.jsp program is called.
What actually will be passed to the method with the parameter String in the code described above?
How to send the complete path of the file that is selected in the first jsp code to the java program? Please advise.
You can't.
First, request.getParameter()
will always return null
in multipart/form-data
requests. You need to parse the request body. How to do this is answered and commented in your previous question.
Second, the webbrowser is supposed to send only the file name along the file content. MSIE is the only webbrowser which (incorrectly) sends the full path along the name. You should not be relying on that. You should also not be interested in the file path. What can you do with it? Open a file using java.io.File
or so? How would you get that to work when webbrowser and webserver runs at physically different machines?
See also:
- How to upload files in JSP/Servlet
- How to get file path from HTML input form in Firefox
精彩评论