Aviram Gabay
Aviram Gabay

Reputation: 201

java.io.FileNotFoundException when submitting a multipart post requert

I'm submitting a multipart form that contains a "file" type input. I'm using this input to upload images. The form submits to a servlet that then passes the uploaded file information to a node app (using an API call) which stores the file.

My issue is that this doesn't work with most of the files I'm trying to upload. In some cases, it does work, but I couldn't find anything unique in these files.

The error I'm getting is:

[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)

Below is the function that fails:

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

When trying to submit a form with a file, I get the above error. When trying to debug the code, I notice that in no point in time is there actually a file with the given name in the destination directory.

What am I doing wrong?

Stack trace below -

[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]

Upvotes: 1

Views: 1379

Answers (1)

Yaron Schwimmer
Yaron Schwimmer

Reputation: 5357

The problem is probably with the size of files you are trying to upload.

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

Means it will only be able to fetch files that are saved the on the disk (only above 10kb I believe), so probably your issue is with small size.

Try changing

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

to

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

and

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

to

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

This will fetch file data both from disk and memory and should solve your issue.

Upvotes: 1

Related Questions