Commons文件上传在Servlet中不起作用

时间:2011-06-01 16:18:09

标签: java servlets apache-commons-fileupload

我有一个servlet,用于处理一个非常大的文件的上传。我试图使用commons fileupload来处理它。目前,我尝试上传的文件是287MB。

我设置了FileItemFactory和ServletFileUpload,然后在ServletFileUpload上设置了一个非常大的最大文件大小。

不幸的是,当我尝试创建FileItemIterator时,没有任何反应。表单使用正确的操作,多部分编码和POST方法设置。

有人可以帮忙吗? servlet的doPost()发布在下面:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    // ensure that the form is multipart encoded since we are uploading a file
    if (!ServletFileUpload.isMultipartContent(req)) {
        //throw new FileUploadException("Request was not multipart");
        log.debug("Request was not multipart. Returning from call");
    }

    // create a list to hold all of the files
    List<File> fileList = new ArrayList<File>();
    try {
        // setup the factories and file upload stuff
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(999999999);

        // create a file item iterator to cycle through all of the files in the req. There SHOULD only be one, though
        FileItemIterator iterator = upload.getItemIterator(req);

        // iterate through the file items and create a file item stream to output the file
        while (iterator.hasNext()) {

            // get the file item stream from the iterator
            FileItemStream fileItemStream = iterator.next();

            // Use the Special InputStream type, passing it the stream and the length of the file
            InputStream inputStream = new UploadProgressInputStream(fileItemStream.openStream(), req.getContentLength());

            // create a File from the file name
            String fileName = fileItemStream.getName();  // this only returns the filename, not the full path
            File file = new File(tempDirectory, fileName);

            // add the file to the list
            fileList.add(file);

            // Use commons-io Streams to copy from the inputstrea to a brand-new file
            Streams.copy(inputStream, new FileOutputStream(file), true);

            // close the inputstream
            inputStream.close();

        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }

    // now that we've save the file, we can process it.
    if (fileList.size() == 0) {
        log.debug("No File in the file list. returning.");
        return;
    }

    for (File file : fileList) {

        String fileName = file.getName();
        BufferedReader reader = new BufferedReader(new FileReader(fileName));

        String line = reader.readLine();
        List<Feature> featureList = new ArrayList<Feature>(); // arraylist may not be the best choice since I don't know how many features I'm importing
        while (!line.isEmpty()) {
            String[] splitLine = line.split("|");
            Feature feature = new Feature();
            feature.setId(Integer.parseInt(splitLine[0]));
            feature.setName(splitLine[1]);
            feature.setFeatureClass(splitLine[2]);
            feature.setLat(Double.parseDouble(splitLine[9]));
            feature.setLng(Double.parseDouble(splitLine[10]));
            featureList.add(feature);

            line = reader.readLine();
        }

        file.delete();   // todo: check this to ensure it won't blow up the code since we're iterating in a for each
        reader.close();  // todo: need this in a finally block somewhere to ensure this always happens.

        try {
            featureService.persistList(featureList);
        } catch (ServiceException e) {
            log.debug("Caught Service Exception in FeatureUploadService.", e);

        }
    }
}

3 个答案:

答案 0 :(得分:2)

这是一个令人难以置信的愚蠢问题。我将名称属性从GWT UiBinder中的FileUpload条目中删除。感谢所有人的帮助。

答案 1 :(得分:0)

唯一的请求参数是否可用文件项?因为您可能想要检查:

if (!fileItemStream.isFormField()){
// then process as file

否则你会收到错误。在表面上您的代码看起来很好:Tomcat日志中没有错误?

答案 2 :(得分:-1)

您需要以html格式添加enctype='multipart/form-data'