使用 commons-fileupload 上传文件时检查有效路径

发布于 2024-07-25 18:07:19 字数 378 浏览 3 评论 0原文

我正在使用一些代码从表单上传图像并将其存储在我们的服务器上。 在 Internet Explorer 中,用户可以手动输入路径,我想知道如何检查该文件是否存在,即用户输入了有效的路径。

有一个 FileItem 对象用于检查大小(例如,fileItem.getSize() < MAX_SIZE),我想知道是否有一个好的方法是使用 size 来检查文件是否存在。 例如:

if (fileItem.getSize() == 0) {
  // Somethings wrong -- invalid path.
} else {
  // File exists -- valid path.
}

如有任何建议,我们将不胜感激。 谢谢!

I'm working with some code that uploads an image from a form and stores it on our server. In Internet Explorer the user can enter a path manually, and I wonder how I can check that the file exists, i.e., that the user entered a valid path.

There's a FileItem object that's being used to check size (e.g., fileItem.getSize() < MAX_SIZE), and I wonder if a good approach would be to use size to check that the file exists. For example:

if (fileItem.getSize() == 0) {
  // Somethings wrong -- invalid path.
} else {
  // File exists -- valid path.
}

Any suggestions are appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2024-08-01 18:07:20

在客户端上,您无法通过脚本可靠地读取文件上传控件的文本。 例如,IE8 和 Opera10 会欺骗您并提供包含“C:\fakepath\”的通用路径。 这样做是出于隐私原因。

在服务器上,您可以完全按照您所做的那样进行操作,只需检查是否确实上传了一个文件,如果是,那么您可以检查该文件,确定它是否符合您的条件。

On the client, you cannot reliably read the text of a file upload control with script. IE8 and Opera10, for instance, will lie to you and provide a generic path containing "C:\fakepath\". This is done for privacy reasons.

On the server, you can do exactly as you've done, simply check to see that you actually got a file in the upload, and if so, then you can examine the file, determine if it matches your criteria.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文