我的問題是setSizeThreshold和setRepository是如何工作的。Java Commons File Upload,size,repository
糾正我,如果我錯了,我的理解是如果文件大小> setSizeThreshold(什麼數目)然後它應該發送文件到setRepository臨時存儲。但我從來沒有看到這個文件存儲在setRepository(filePath)中。我沒有設置FileCleaningTracker,因此在技術上該文件應該仍然在setRepository(filePath)中?
- 如何測試Repository是否正在工作?有沒有辦法?
這是我的代碼,一切都是從doc沒有什麼特別的。
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);
}
}
在DOC
Larger items should be written to a temporary file on disk.
Very large upload requests should not be permitted.
- 究竟是什麼 「非常大」?什麼大小被認爲非常大?
在此先感謝...
**題外話:主要觀點基於:**你基本上是問的東西,不能比你自己以外的任何人來回答。你認爲什麼大。我定期與PETABYTES的數據合作,我的觀點與其他大多數人相比將會大不相同。與其他人一樣。 –