How to get rid of WebKitFormBoundary in uploaded file

11k views Asked by At

I'm implementing a file upload in a web application.

The front-end is written in angularJS and uses the angular-file-upload package (the one that can be found at this link https://github.com/nervgh/angular-file-upload).

The back-end is Java / Jersey Web Services.

My Question is:

The uploaded file contains a WebKitFormBoundary header and footer, like this:

------WebKitFormBoundarylqskdjlqksdjl
Content-Disposition: form-data; name="upload"; filename="foo.bar"
Content-Type: multipart/form-data

Therefore, I'm not sure whether I'm uploading a file or a request. And of course, my back-end application considers that the uploaded files are corrupted and would not display them unless those lines are removed (for now manually).

Bottom line is : how do I get rid of that header and footer in the uploaded file?

Here are some code samples.

Front-End

Once again: angularJS angular-file-upload

item.headers = {
    'Content-Disposition': 'attachment; filename="' + item.file.name + '"',
    'Content-Type': 'multipart/form-data'
};

Back-End

and Java / Jersey

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("someurl/{fileName}")
public Artifact uploadArtifact(InputStream uploadedStream, @PathParam("fileName") String fileName) throws Exception;

Note

I'm wondering if the Content-Disposition: attachment in my angularJS part could be what's messing it up?

And that it should rather be Content-Disposition: form-data?

Thx in advance!

1

There are 1 answers

0
vsminkov On BEST ANSWER

You need to place @FormDataParam annotation in order to properly handle boundary.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("someurl/{fileName}")
public Artifact uploadArtifact(
    @FormDataParam("file") InputStream uploadedStream,
    @FormDataParam("file") FormDataContentDisposition fileDetails,
    @PathParam("fileName") String fileName) throws Exception;