java.io.FileNotFoundException 提交多部分 post 请求时

java.io.FileNotFoundException when submitting a multipart post requert

我正在提交包含 "file" 类型输入的多部分表单。 我正在使用此输入来上传图像。表单提交给 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]

问题可能与您尝试上传的文件大小有关。

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());

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