无法上传文件:代码无法进入while循环

时间:2015-03-02 06:29:58

标签: java file-upload

我无法上传文件。没有例外,没有错误,但是当代码运行时,文件没有上传。可能是什么原因?

正在调试:

iterator.hasNext() returns false

`

<form action='UploadMatch' method='post' enctype="multipart/form-data">
              <input type='file' /> 
              <input type='submit' class='btn btn-default' id='uploadmatch' />upload
</form>


/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package servlet;

  import java.io.File;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Iterator;
  import java.util.List;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  import org.apache.commons.io.FilenameUtils;

  /**
   *
   * @author user
   */
  public class UploadMatch extends HttpServlet {


      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          response.setContentType("text/plain");
          PrintWriter writer = response.getWriter();

          String path = request.getParameter("Data");
          try {
             boolean isMultipart = ServletFileUpload.isMultipartContent(request);
             if( !isMultipart ) {
                 writer.println("File cannot be uploaded !");
             } else {
                 DiskFileItemFactory diskFileItem = new DiskFileItemFactory();
                 ServletFileUpload fileUpload = new ServletFileUpload(diskFileItem);
                 List list = null;

                 try {
                     list = fileUpload.parseRequest(request);
                 }catch(Exception exc) {
                     writer.println(exc);
                 }

                 Iterator iterator = list.iterator();
                 while(iterator.hasNext()) { // WHILE LOOP (DOESN'T ENTER)
                    FileItem fileItem = (FileItem)iterator.next();
                    if(fileItem.isFormField()) {
                        // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                    } else {
                        // Process form file field (input type="file").
                        String fieldName = fileItem.getFieldName();
                        String fileName = FilenameUtils.getName(fileItem.getName());

                        File file = new File(request.getServletContext().getContextPath(),fileName);
                        writer.println(">>>>>>>>>>W:/" + " " + fileName);
                        fileItem.write(file);

                    }
                 }
             }
          }catch(Exception exc) {
              writer.println(exc);
          }
      }
  }

注意:代码不会进入while循环

0 个答案:

没有答案