java 在 JSP 中上传文件 - 如何更改上传文件的默认路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1233639/
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
upload file in JSP - how to change a default path for the uploaded file
提问by Greener
I have two jps pages to handle an upload of the single file. Here is a code for selecting a file:
我有两个 jps 页面来处理单个文件的上传。这是选择文件的代码:
org.apache.commons.io.FilenameUtils, java.util.*,
java.io.File, java.lang.Exception" %>
...
<form name="uploadFile" method="POST" action="processUpload.jsp"
enctype="multipart/form-data">
<input type="file" name="myfile"><br />
<input type="submit" value="Submit" />
</form>
....
//--------handle uploaded file---------------------
//--------处理上传的文件--------------------
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%
System.out.println("Content Type ="+request.getContentType());
System.out.println("Cookies" + request.getCookies());
DiskFileUpload fu = new DiskFileUpload();
// If file size exceeds, a FileUploadException will be thrown
fu.setSizeMax(1000000);
List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();
while(itr.hasNext()) {
FileItem fi = (FileItem)itr.next();
//Check if not form field so as to only handle the file inputs
//else condition handles the submit button input
if(!fi.isFormField()) {
System.out.println("\nNAME: "+fi.getName());
System.out.println("SIZE: "+fi.getSize());
//System.out.println(fi.getOutputStream().toString());
File fNew= new File(application.getRealPath("/"), fi.getName());
System.out.println(fNew.getAbsolutePath());
fi.write(fNew);
}
else {
System.out.println("Field ="+fi.getFieldName());
}
}
%>
This code put a file into my build\web folder. How to set a path to a different directory on the server (assuming the write permissions are set) ? Thanks,
此代码将文件放入我的 build\web 文件夹中。如何设置服务器上不同目录的路径(假设设置了写权限)?谢谢,
采纳答案by David Rabinowitz
Use the following code (adapted for the user guide):
使用以下代码(适用于用户指南):
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(dir);
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
回答by Ravi Wallau
Why simply not specify the path when creating the file? You could set the path as part of your application configuration (JNI), or as a system property when the web server starts (using the -Dpath=...), and reading it using System.getProperty("path"). You could even use an environment variable defined on your system and read that environment variable using the System.getenvmethod.
为什么在创建文件时不指定路径?您可以将路径设置为应用程序配置 (JNI) 的一部分,或者在 Web 服务器启动时将其设置为系统属性(使用 -Dpath=...),并使用 System.getProperty("path") 读取它。您甚至可以使用系统上定义的环境变量,并使用System.getenv方法读取该环境变量。
Alternativaly, you could create just a temporary file using the File.getTempFile method. If you just need to save the file to do something with it and never use it again, that's a better option - nevertheless, you have to delete the file yourself after you use it.
或者,您可以使用 File.getTempFile 方法只创建一个临时文件。如果您只需要保存文件以对其进行处理并且不再使用它,这是一个更好的选择 - 尽管如此,您必须在使用后自行删除该文件。
回答by Powerlord
To set a file upload path:
设置文件上传路径:
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File('/path/to/store/files'));
DiskFileUpload fu = new DiskFileUpload(factory);
回答by vector
... try something like this:
...尝试这样的事情:
File newFile = new File(request.getSession().getServletContext().getRealPath("/someUploadDirectoryOnServer/"), multipartFile.getOriginalFilename());

