0

如果我用 注释以下 servlet @MultipartConfig,我将无法使用Apache common upload.

@MultipartConfig

public class SendTheFileName extends HttpServlet {
  // something
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
           moveToSharedDirectory(request,path); // call to upload
  }
}

例如 :

public boolean moveToSharedDirectory(HttpServletRequest request,String path) {
    System.out.println("1. OUTSIDE THE TRY BLOCK OF UPLOAD CLASS");
    try {
       System.out.println("2. IN THE TRY BLOCK OF UPLOAD CLASS");
       boolean isMultipart = ServletFileUpload.isMultipartContent(request);
       System.out.println("3. AFTER THE BOOLEAN STATEMENT " + isMultipart);
       if(!isMultipart) {
           // Error:File cannot be uploaded
           System.out.println("Message : IS NOT MULTIPART");
       } else {
           DiskFileItemFactory dfif = new DiskFileItemFactory();
           ServletFileUpload fileUpload = new ServletFileUpload(dfif);
           List list = null;
           list = fileUpload.parseRequest(request); // returns a list of FileItem instances parsed from the request
           Iterator iterator = list.iterator();
           System.out.println("4. JUST BEFORE ENTERING THE WHILE LOOP");
           System.out.println("5. CHECKING IF THE ITERATION HAS ANY ELEMENT : " + iterator.hasNext());

           // ... some more here {-}
         }
    } // close catch
} 

上面代码段中的语句:

System.out.println("5. CHECKING IF THE ITERATION HAS ANY ELEMENT : " + iterator.hasNext());

打印false!这是为什么 ?

如果我删除我得到的注释true并且能够上传文件。

对应的HTML:

<form method="post" action="SendTheFileName" enctype="multipart/form-data">
                <div id="Files_to_be_shared"> 
                      <input type="file" id="File" name="FileTag" />
                      <input type="submit" value="Share" /> 
                    </div>
 </form>

注意:由于某种原因,我不得不使用 apache commons 来上传文件。

4

1 回答 1

3

在调用servlet 之前@MultipartConfig触发 Servlet 3.0 内置请求正文解析。因此,当轮到 Apache Commons FileUpload 解析请求时,它将面临一个空的请求正文。换句话说,你不能混合它们。将它们混合起来也没有任何意义,因为它们都在幕后做着完全相同的工作。multipart/form-dataservice()

您有 2 个选项:

  1. 删除@MultipartConfig并保留 Apache Commons FileUpload。

  2. 或者,保留@MultipartConfig并删除 Apache Commons FileUpload。

也可以看看:

于 2013-01-22T11:05:32.383 回答