java 如何从JSP中的文件类型获取完整路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4344740/
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 full path from file type in JSP?
提问by janasainik
i written some code in JSP like this--
我用 JSP 写了一些这样的代码——
<input type="file" name="imagename" >
and in servlet i am retrieving 'imagename' value. But it is giving the name of the image instead of full path. My servlet code is like this:
在 servlet 中,我正在检索 'imagename' 值。但它给出了图像的名称而不是完整路径。我的servlet代码是这样的:
String imagename = request.getParameter("imagename");
and i dont want to use javascript. any ideas? Thanks in advance
我不想使用 javascript。有任何想法吗?提前致谢
采纳答案by Daniel Fath
Maybe you should checkout this question: How to get the file path from HTML input form in Firefox 3
也许你应该看看这个问题:How to get the file path from HTML input form in Firefox 3
There is little to no reason why server should have to know full file path. If you want to upload a file, you'll need to use an appropriate library like Apache Commons FileUploadand transfer the file using.
服务器几乎没有理由必须知道完整的文件路径。如果要上传文件,则需要使用适当的库,例如Apache Commons FileUpload并使用传输文件。
<form action="upload-script-url" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
Apache Commons FileUpload will then accept and transform encoded file into a usable form.
Apache Commons FileUpload 然后将接受编码文件并将其转换为可用形式。
Otherwise you'll need to use JavaScript to get that path.
否则,您将需要使用 JavaScript 来获取该路径。
回答by madhurtanwani
Assuming that you are trying to upload a file to your server, please note that file uploads are a little more than what you are trying to do - do not expect that if you have a "file" input type in a form, on submission the file just reaches your sever, with no effort. There is a certain procedure to do that.
假设您正在尝试将文件上传到您的服务器,请注意文件上传比您尝试执行的要多一点 - 不要指望如果表单中有“文件”输入类型,在提交时文件直接到达您的服务器,无需任何努力。有一定的程序可以做到这一点。
This article might e a good reference : http://www.cs.tut.fi/~jkorpela/forms/file.html
这篇文章可能是一个很好的参考:http: //www.cs.tut.fi/~jkorpela/forms/file.html
For Java, use Apache's commons-fileupload : http://commons.apache.org/fileupload/
对于 Java,请使用 Apache 的 commons-fileupload:http: //commons.apache.org/fileupload/
回答by Luca Matteis
imagename
contains the variable you pass to the servlet... the actual HTTP request parameter. If you want the full path, be sure that the program that is calling your HTTP page is passing the full path instead of just the image name.
imagename
包含您传递给 servlet 的变量...实际的 HTTP 请求参数。如果您需要完整路径,请确保调用您的 HTTP 页面的程序正在传递完整路径,而不仅仅是图像名称。