CommonsFileUpload - 方法parseRequest(HttpServletRequest)未定义类型DiskFileItemFactory

时间:2017-11-08 11:01:53

标签: java jsp servlets java-ee apache-commons-fileupload

过去两天左右,我一直试图解决这个问题,而且我已经碰壁了。

我正在尝试通过Java Servlet按字母顺序上传多个文件但是我收到以下错误:

type: Exception report

message: Servlet execution threw an exception

description: The server encountered an internal error that prevented it from fulfilling this request.

exception:

javax.servlet.ServletException: Servlet execution threw an exception
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.Error: Unresolved compilation problems: 
    Type mismatch: cannot convert from ServletFileUpload to List<FileItem>
    The method parseRequest(HttpServletRequest) is undefined for the type DiskFileItemFactory

    com.codeFactory.UploadServlet1.doPost(UploadServlet1.java:50)
    com.codeFactory.UploadServlet1.doGet(UploadServlet1.java:37)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我上传的Servlet(UploadServlet1.java):

package com.codeFactory;


import java.io.IOException;
import java.util.List;
import java.io.File;
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 org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class UploadServlet1
 */
@WebServlet("/UploadServlet1")
public class UploadServlet1 extends HttpServlet {
    private final String UPLOAD_DIRECTORY = "D:/abc";
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadServlet1() {
        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
        doPost(request,response);
        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
        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();
                        item.write(new File(UPLOAD_DIRECTORY + File.separator +name));
                    }
                }
                request.setAttribute("message", "file uploaded successfully");
            }
            catch(Exception ex)
            {
                request.setAttribute("message", "file uploaded failed"+ex);
            }
        }
        else
        {
            request.setAttribute("message", "This servlet handles only file upload request");
        }
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }

}

我的(result.jsp)代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Result page</title>
    </head>
    <body>
    <div>
    <h3> $ {message}</h3>
    </div>
    </body>
    </html>

我现在该做什么?

1 个答案:

答案 0 :(得分:0)

你的问题在于DiskFileItemFactory没有解析请求的方法。

查看文档,我可以看到ServletFileUpload对象有parseRequest method,请尝试以下代码:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if(ServletFileUpload.isMultipartContent(request))
    {
        try
        {
            ServletFileUpload uploader =new ServletFileUpload(new DiskFileItemFactory());
            List<FileItem> multiparts = uploader.parseRequest(request);
            for(FileItem item : multiparts)
            {
                if(!item.isFormField())
                {
                    String name=new File(item.getName()).getName();
                    item.write(new File(UPLOAD_DIRECTORY + File.separator +name));
                }
            }
            request.setAttribute("message", "file uploaded successfully");
        }
        catch(Exception ex)
        {
            request.setAttribute("message", "file uploaded failed"+ex);
        }
    }
    else
    {
        request.setAttribute("message", "This servlet handles only file upload request");
    }
    request.getRequestDispatcher("/result.jsp").forward(request, response);
}