Java 如何在jsp中获取上传文件的完整路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22756186/
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-08-13 17:43:52  来源:igfitidea点击:

how to get full path for upload a file in jsp?

javafilejspupload

提问by sathya

In my jsp page, use file uploading and pass file for string to java page for copy to particular folder. I want whole path for copying my file. but i get only a file name with extension.

在我的jsp页面中,使用文件上传并将字符串文件传递给java页面以复制到特定文件夹。我想要复制我的文件的完整路径。但我只得到一个带扩展名的文件名。

scan file : ABC.pdf

扫描文件:ABC.pdf

it show only : ABC.pdf

它只显示:ABC.pdf

i want to show: c:/abc.pdf

我想显示:c:/abc.pdf

回答by Scary Wombat

JSP is code that produces the client facing HTML code (commonly called the View) and the Servlet is server code. In reality they will be on different machines, so what is the use of the full path. The file contents should be POSTED to the servlet when submitting your form.

JSP 是生成面向客户端的 HTML 代码(通常称为视图)的代码,而 Servlet 是服务器代码。实际上他们会在不同的机器上,那么全路径有什么用呢。提交表单时,应将文件内容发布到 servlet。

your jsp should something like:

你的jsp应该是这样的:

<form action="UploadServlet" method="post"
                        enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>

Of course you have other input fields as well.

当然,您还有其他输入字段。

See this link

看这个链接

How to upload files to server using JSP/Servlet?

如何使用 JSP/Servlet 将文件上传到服务器?

回答by developerwjk

The Local filepath is useless on the server-side. It would only be of use to hackers. That's why browsers don't send it. Its a security measure. You should be glad its there. I'm surprised none of the existing answers pointed this out.

本地文件路径在服务器端是无用的。它只会对黑客有用。这就是浏览器不发送它的原因。它是一种安全措施。你应该很高兴它在那里。我很惊讶现有的答案都没有指出这一点。

On the server-side you decide where to save the file. You wouldn't want the user deciding that, obviously. Giving them the ability to decide where to save the file on your server would give them the ability to overwrite your system files.

在服务器端,您决定保存文件的位置。显然,您不希望用户决定这一点。让他们能够决定在您的服务器上保存文件的位置将使他们能够覆盖您的系统文件。