我使用apache commons文件上传来上传servlet上的文件。但在解析请求后,我总是得到文件项的空列表。我的代码如下所示:
if (!ServletFileUpload.isMultipartContent(request)) {
throw new UnsupportedOperationException("Expected `multipart/...` content-type header ");
}
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
List<FileItem> items = upload.parseRequest(request);
logger.debug("Items size: " + items.size());
哪里有问题?请求发送正确
答案 0 :(得分:0)
在表单操作中添加enctype =“multipart / form-data”
<form action="UploadServlet" method="post"
enctype="multipart/form-data">
</form>
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("c:\\temp"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
}catch(Exception e){
}
http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm
答案 1 :(得分:0)
创建DiskFileItemDirectory时,请使用此构造函数
DiskFileItemFactory factory = new DiskFileItemFactory(FILE_SIZE, new File("/yourfolder"));
FILE_SIZE是您允许上传的最大文件大小。确保您对“/ yourfolder”中的文件夹位置有权限(更改此内容...)