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

标签 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);

        }
    }
}

最佳答案

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

关于java - Commons 文件上传在 Servlet 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6204412/

相关文章:

tomcat - Apache 共享文件上传 : problems uploading files greater than 100kb using HTTPS

java - 如何将 JSP 中的多部分/混合内容解析为 servlet

java - Quartz API 和 Joda Time API 之间有什么关系?

java - javascript 函数终止 session 后调用 servlet

java - 为什么这个 java servlet 不起作用 [OpenShift+Tomact6+Git+Jboss]

java - 无法通过 JSP 中的列表获取结果

java - upload.parseRequest(request) 在 commons.fileUpload 中返回空列表

java - 使用联接定义 JPA 存储库 native 查询

java - 当目标位于 NAT 之后时 VisualVM 远程工作?

java - ArrayIndexOutOfBoundsException - 数组本身没有初始化为默认值吗? ( java )