提交多部分帖子重新提交时的java.io.FileNotFoundException

时间:2015-08-11 12:34:43

标签: java forms filenotfoundexception confluence

我正在提交包含“文件”类型输入的多部分表单。 我正在使用此输入来上传图像。表单提交给servlet,然后将上传的文件信息传递给存储文件的节点应用程序(使用API​​调用)。

我的问题是,这不适用于我尝试上传的大多数文件。在某些情况下,它确实有效,但我在这些文件中找不到任何独特的东西。

我得到的错误是:

[INFO] [talledLocalContainer] caused by: java.io.FileNotFoundException: < absolute-path-to-temp-dir>\temp\upload_1940e046_658b_40ae_9d6d_4a1ecac4e58d_00000083.tmp (The system cannot find the file specified)

以下是失败的功能:

protected HttpResponse postUpdateBadge(String url, JSONObject payload)throws IOException {

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpEntityEnclosingRequestBase httpCall = new HttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        if(payload.has("file")) {
            DiskFileItem dfi = (DiskFileItem) payload.get("file");
            if (!dfi.getName().equals("")) {
                builder.addBinaryBody("icon", dfi.getStoreLocation(), ContentType.create(dfi.getContentType()), dfi.getName());
            }
            payload.remove("file");
        }
        builder.addPart("payload", new StringBody(payload.toString(), ContentType.APPLICATION_JSON));
        httpCall.setEntity(builder.build());

    httpCall.setHeader("token", token);
    return httpClient.execute(httpCall);
}

尝试使用文件提交表单时,出现上述错误。在尝试调试代码时,我注意到在目标目录中实际上没有一个具有给定名称的文件。

我做错了什么?

下面的堆栈跟踪 -

[INFO] [talledLocalContainer] com.atlassian.confluence.web.filter.validateparam.RequestParamValidationFilter_already_filtered: true
[INFO] [talledLocalContainer] atlassian.core.seraph.original.url: /500page.jsp
[INFO] [talledLocalContainer] com.atlassian.confluence.security.websudo.MessagesDecoratorFilter__already_filtered__: true
[INFO] [talledLocalContainer] com.atlassian.labs.botkiller.BotKillerFilter: true
[INFO] [talledLocalContainer] com.atlassian.gzipfilter.GzipFilter_already_filtered: true
[INFO] [talledLocalContainer] Confluence-Request-Time: 1439296917003
[INFO] [talledLocalContainer] loginfilter.already.filtered: true
[INFO] [talledLocalContainer] javax.servlet.error.request_uri: /confluence/plugins/servlet/wspoints/badge/edit
[INFO] [talledLocalContainer] com.atlassian.core.filters.HeaderSanitisingFilter_already_filtered: true
[INFO] [talledLocalContainer] com.atlassian.prettyurls.filter.PrettyUrlsSiteMeshFixupFilter: true
[INFO] [talledLocalContainer] com.atlassian.confluence.web.ConfluenceJohnsonFilter_already_filtered: true
[INFO] [talledLocalContainer] javax.servlet.error.exception: java.io.FileNotFoundException: C:\Users\User\IdeaProjects\points-system\target\container\tomcat6x\cargo-confluence-home\temp\upload_1940e046_658b_40ae_9d6d_4a1ecac4e58d_00000139.tmp (The system cannot find the file specified)
[INFO] [talledLocalContainer] os_securityfilter_already_filtered: true
[INFO] [talledLocalContainer] com.atlassian.prettyurls.filter.PrettyUrlsSiteMeshFilter: true
[INFO] [talledLocalContainer] --------------------------
[INFO] [talledLocalContainer] Parameters
[INFO] [talledLocalContainer] --------------------------
[INFO] [talledLocalContainer] caused by: java.io.FileNotFoundException: C:\Users\User\IdeaProjects\points-system\target\container\tomcat6x\cargo-confluence-home\temp\upload_1940e046_658b_40ae_9d6d_4a1ecac4e58d_00000139.tmp (The system cannot find the file specified)
[INFO] [talledLocalContainer] at java.io.FileInputStream.open0(Native Method)
[INFO] [talledLocalContainer]

1 个答案:

答案 0 :(得分:1)

问题可能在于您尝试上传的文件大小。

DiskFileItem dfi = (DiskFileItem) payload.get("file");

意味着它只能获取保存在磁盘上的文件(我认为只有10kb以上),所以可能你的问题是小尺寸。

尝试更改

DiskFileItem dfi = (DiskFileItem) payload.get("file");

FileItem dfi = (FileItem) payload.get("file");

builder.addBinaryBody("icon", dfi.getStoreLocation(), ContentType.create(dfi.getContentType()), dfi.getName());

builder.addBinaryBody("icon", dfi.get(), ContentType.create(dfi.getContentType()), dfi.getName());

这将从磁盘和内存中获取文件数据,并应解决您的问题。