我的问题是 setSizeThreshold 和 setRepository 是如何工作的。
如果我错了,请纠正我,我的理解是如果文件大小 > setSizeThreshold(无论数字如何),那么它应该将文件发送到临时存储的 setRepository。但我从来没有看到这个文件存储在 setRepository(filePath) 中。我没有 setFileCleaningTracker,所以从技术上讲,文件应该仍然在 setRepository(filePath) 中?
- 如何测试存储库是否正常工作?有办法吗?
这是我的代码,一切都来自文档,没什么特别的。
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(20480); // purposely make it small to test
factory.setRepository(new File(root + "tempFile"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setSizeMax(20480); // This makes the upload stop
try
{
// Parse the request
List<FileItem> items = upload.parseRequest(request);
// Process the uploaded items
Iterator<FileItem> i = items.iterator();
while(i.hasNext())
{
FileItem item = i.next();
//request.setAttribute("item", item);
if(item.isFormField()){
String fieldName = item.getFieldName();
String fieldValue = item.getString();
out.println(fieldName + ": " + fieldValue);
}
else
{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
File name = new File(item.getName());
// File.separator: "/"
item.write( new File(root + "uploads" + File.separator + name));
}
}
} catch (FileUploadException ex)
{
ex.printStackTrace();
} catch (Exception ex)
{
Logger.getLogger(uploadFileServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
在文档中
Larger items should be written to a temporary file on disk.
Very large upload requests should not be permitted.
- 究竟什么是“非常大”?什么尺寸被认为是非常大的?
提前致谢...