Javascript 如何在jsp中获取<input type="file"的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2592403/
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
How to get the file name for <input type="file" in jsp
提问by deepthinker121
I want to read the file path from html input type="file"(the entry selected in the file dialog by the user)
我想从 html 中读取文件路径input type="file"(用户在文件对话框中选择的条目)
<script>
function OpenFileDialog(form) {
var a = document.getElementById("inputfile").click();
SampleForm.filePath.value = //set the path here
document.SampleForm.submit();
}
</script>
<form name="SampleForm" action="TestServlet" method="get">
<input type="file" style="display:none;" id="inputfile"/>
<a href="javascript:OpenFileDialog(this.form);">Open here</a>
<input type="hidden" name="filePath" value=""/>
</form>
I want the path of the selected file to be read in my Servlet class
How do I get the file path? Can I read it from var a?
Or is there any way to directly access the file path from the input type="file"from my servlet?
我希望在我的 Servlet 类中读取所选文件的路径 如何获取文件路径?我可以从中读取var a吗?或者有什么方法可以直接input type="file"从我的 servlet访问文件路径?
回答by BalusC
First, to clear a common misunderstanding: the file path is worthlessin the server side. Imagine that I am the client and I give you the file path c:/passwords.txt, how would you as being the server ever get its contents? Using java.io.File? No? That would only work if both client and server runs at the physically same machine. The onlypossible occurence is the local development environment.
首先,澄清一个常见的误解:文件路径在服务器端毫无价值。想象一下,我是客户端,我给你文件路径c:/passwords.txt,你作为服务器如何获取它的内容?使用java.io.File? 不?这仅在客户端和服务器运行在同一台物理机器上时才有效。该只可能发生难度当地的发展环境。
Second, to clarify a restriction: Javascript cannot do anything with a input type="file"element due to security restrictions. If it was possible, then one could develop a website which sets the uploaded file to c:/passwords.txtand submits the form during onload. That's easy unaskingly collecting of all password files from everyone who visits the website! No?
其次,澄清一个限制:input type="file"由于安全限制,Javascript 不能对元素做任何事情。如果有可能,那么人们可以开发一个网站,该网站上传的文件设置为c:/passwords.txt,并在提交表单onload。毫不费力地从访问该网站的每个人那里收集所有密码文件很容易!不?
After all, you're rather interested in the file contents. As stated in the HTML forms specyou need to set the request method to POSTand the request encoding to multipart/form-datain the parent <form>element.
毕竟,您对文件内容很感兴趣。如HTML 表单规范中所述,您需要在父元素中设置请求方法POST和请求编码。multipart/form-data<form>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
This way the file will be sent in the request body. As the standard Servlet API versions up to with 2.5 doesn't have builtin facilities to parse mulipart/form-datarequests, you need to parse the request yourself. The best way is to use Apache Commons FileUploadfor this. Follow the link and read both the User Guideand Frequently Asked Questionsfor code examples and tips&tricks. When you're already on Servlet 3.0, then you can just use the Servlet API provided HttpServletRequest#getParts()for this. You can find here an article with code examples about that.
这样,文件将在请求正文中发送。由于 2.5 之前的标准 Servlet API 版本没有内置工具来解析mulipart/form-data请求,您需要自己解析请求。最好的方法是为此使用Apache Commons FileUpload。点击链接并阅读用户指南和常见问题以获取代码示例和提示与技巧。如果您已经在使用 Servlet 3.0,那么您只需使用HttpServletRequest#getParts()为此提供的 Servlet API 。您可以在此处找到有关此代码示例的文章。
回答by mindas
(Taken from http://www.jguru.com/faq/view.jsp?EID=1045507)
(取自http://www.jguru.com/faq/view.jsp?EID=1045507)
Solution A:
解决方案一:
- Download http://www.servlets.com/cos/index.html
- Check http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html- it has the relevant getter.
- 下载http://www.servlets.com/cos/index.html
- 检查http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html- 它有相关的 getter。
Solution B:
解决方案B:
- download http://commons.apache.org/fileupload/
- invoke readHeaders() in org.apache.commons.fileupload.MultipartStream
- 下载http://commons.apache.org/fileupload/
- 在 org.apache.commons.fileupload.MultipartStream 中调用 readHeaders()
Solution C:
解决方案 C:
- download http://users.boone.net/wbrameld/multipartformdata/
- invoke getParameter on com.bigfoot.bugar.servlet.http.MultipartFormData
- 下载http://users.boone.net/wbrameld/multipartformdata/
- 在 com.bigfoot.bugar.servlet.http.MultipartFormData 上调用 getParameter

