1

我有一个 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);

        }
    }
}
4

3 回答 3

2

这是一个非常愚蠢的问题。我在 GWT UiBinder 中的 FileUpload 条目中保留了 name 属性。感谢大家的帮助。

于 2011-06-02T10:34:00.403 回答
0

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

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

否则你会得到错误。从表面上看,您的代码看起来不错:Tomcat 日志中没有错误?

于 2011-06-01T16:25:08.953 回答
-1

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

于 2013-08-25T08:49:15.237 回答