0

我使用 apache commons file-upload 在 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());

哪里可能有问题?请求正确发送

4

2 回答 2

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

于 2013-08-02T08:45:16.773 回答
0

创建 DiskFileItemDirectory 时使用此构造函数

DiskFileItemFactory factory = new DiskFileItemFactory(FILE_SIZE, new File("/yourfolder"));

FILE_SIZE 是您允许上传的最大文件大小。确保您有权访问“/yourfolder”中的文件夹位置(更改此...)

于 2013-08-02T10:04:02.687 回答