使用servlet和html的单个文件上传,对于upload.parseRequest(request)返回的大小为-1

时间:2018-07-11 08:22:12

标签: java file-upload

我试图将文件从本地机器上传到Eclipse Dynamic Web Project中的/ resources文件夹中。我正在使用html和servlet。我成功获取了消息文件,但文件夹中没有文件。 我发现的问题 1.在我的代码中

List<FileItem> multiparts = new ServletFileUpload(
                      new DiskFileItemFactory()).parseRequest(request);

此处调试器中用于多部分的size值显示为-1,其文件名正确显示为data.txt。 2.i在item.write().

处也出现了filenotfound异常
  

没有这样的文件或目录C:\ Temp \ upload / data.txt

我的HTML代码

Select a file to upload: <br />
      <form action = "upload" method = "post"
         enctype = "multipart/form-data">
         <input type = "file" name = "file" size = "50" />
         <br><br>
         <input type = "submit" value = "Upload File" />
      </form>

文件夹结构 enter image description here

SERVLET CODE

   package Upload;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import java.io.File;
import java.io.IOException;
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;

/**
 * Servlet implementation class uploadFile
 */

public class uploadFile extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public uploadFile() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //doGet(request, response);
        ProcessRequest(request, response);
    }
    void ProcessRequest(HttpServletRequest request, HttpServletResponse response) {
         // String  UPLOAD_DIRECTORY ="/resources";
          String dir="C:\\Temp\\upload";
          String mess=null;
        //process only if its multipart content
        if(ServletFileUpload.isMultipartContent(request)){
            try {
                List<FileItem> multiparts = new ServletFileUpload(
                      new DiskFileItemFactory()).parseRequest(request);

                for(FileItem item : multiparts){
                    if(!item.isFormField()){
                        String name = new File(item.getName()).getName();
                        System.out.println("name "+name);
                        item.write(
                          new File(dir + File.separator + name));
                    }
                }

               //File uploaded successfully
                mess="File Uploaded Successfully";
               request.setAttribute("message", "File Uploaded Successfully");
            } catch (Exception ex) {
               request.setAttribute("message", "File Upload Failed due to " + ex);
               mess="File Upload Failed due to " + ex;
            }

        }else{
            request.setAttribute("message",
                   "Sorry this Servlet only handles file upload request");
        }

        //request.getRequestDispatcher("/result.jsp").forward(request, response);
        try {
            response.getWriter().print(mess);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


}
}

0 个答案:

没有答案