new diskfileitemfactory())。parserequest(request)抛出异常

时间:2015-06-24 13:42:04

标签: jsp upload

我正在研究在学校项目中使用的应用程序。这是一个将文件上传到服务器的POC。但是,当我尝试上传文件时,此行会引发异常:

new diskfileitemfactory())。parserequest(request);

这是我的代码:

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 to handle File upload request from Client
* @author Javin Paul
*/
public class FileUploadHandler extends HttpServlet {
private final String UPLOAD_DIRECTORY = "C:/uploads";

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse     response)
        throws ServletException, IOException {

    //process only if its multipart content
    if(ServletFileUpload.isMultipartContent(request)){
    try {
            List<FileItem> multiparts = new ServletFileUpload(


                new DiskFileItemFactory()).parseRequest(request);
 //this is the problematic line^    


            for(FileItem item : multiparts){
                if(!item.isFormField()){
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                }
            }

           //File uploaded successfully
           request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception ex) {
           request.setAttribute("message", "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);

}

}

我看到显示JSP代码是值得赞赏的:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>File Upload Example in JSP and Servlet - Java web application</title>
    </head>

    <body> 
        <div>
            <h3> Choose File to Upload in Server </h3>
            <form action="upload" method="post" enctype="multipart/form-data">
                <input type="file" name="file" />
                <input type="submit" value="upload" />
            </form>          
        </div>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试此代码,可能对您有所帮助。

DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(MEMORY_THRESHOLD);
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(MAX_FILE_SIZE);
        upload.setSizeMax(MAX_REQUEST_SIZE); // sets maximum size of request (include file + form data)
        String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;

        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }

        List<FileItem> multiparts= upload.parseRequest(request);