java 将上传文件的路径传递给Java程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3653704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:52:10  来源:igfitidea点击:

To pass the path of the uploaded file to the Java program

javajspfile-upload

提问by LGAP

I have created two JSP programs as follows.

我创建了两个 JSP 程序如下。

First One: Addmulti.jsp

第一个: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

第二个: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,

SearchLink 是一个 java 程序,其中包含如下方法,

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.

我希望将上面提到的第一个jsp程序中选择的文件的完整路径传递给名为addmultiple(String)的java方法。相反,一旦调用 Test2.jsp 程序,就会在浏览器中打印 null 作为输出。

What actually will be passed to the method with the parameter String in the code described above?

在上面描述的代码中,实际上将什么传递给带有参数 String 的方法?

How to send the complete path of the file that is selected in the first jsp code to the java program? Please advise.

如何将第一个jsp代码中选中的文件的完整路径发送给java程序?请指教。

回答by BalusC

You can't.

你不能。

First, request.getParameter()will always return nullin multipart/form-datarequests. You need to parse the request body. How to do this is answered and commented in your previous question.

首先,request.getParameter()将始终nullmultipart/form-data请求中返回。您需要解析请求正文。如何做到这一点在您之前的问题中得到了回答和评论。

Second, the webbrowser is supposed to send only the file namealong 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.Fileor so? How would you get that to work when webbrowser and webserver runs at physically different machines?

其次,网络浏览器应该只发送文件内容中的文件。MSIE 是唯一(错误地)沿名称发送完整路径的网络浏览器。你不应该依赖它。您也不应该对文件路径感兴趣。你能用它做什么?使用java.io.File左右打开文件?当 webbrowser 和 webserver 在物理上不同的机器上运行时,您将如何使其工作?

See also:

也可以看看: