如果我用 注释以下 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 来上传文件。