使用servlet将文件上传到我在主机上的应用程序

时间:2014-01-13 14:53:52

标签: servlets file-upload image-upload

我有一个上传文件的serlvet ...它可以在我的服务器上运行,我上传到“http://localhost:8084/TestAmazon”并且它可以工作......但我们部署我的Web应用程序来托管并上传文件不行......请帮帮我

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
           String relativeWebPath = "/image";
    String absoluteFilePath = getServletContext().getRealPath(relativeWebPath);
    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(absoluteFilePath + File.separator + name));
                }
            }
            request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception e) {
            request.setAttribute("message", "File Upload Failed due to " + e);

        }
    } else {
        request.setAttribute("message",
                "Sorry this Servlet only handles file upload request");
    }
    request.getRequestDispatcher("result.jsp").forward(request, response);
}

我收到了错误消息:

    exception

javax.servlet.ServletException: Servlet execution threw an exception
root cause

java.lang.NoSuchMethodError: org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload.parseRequest(Ljavax/servlet/http/HttpServletRequest;)Ljava/util/List;
    sample.ProcessServlet.doPost(ProcessServlet.java:73)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

0 个答案:

没有答案