简体   繁体   中英

File Upload With Jersey Not Working

I can't get a Jersey file upload to work. Using Jersey 1.9. InputStream is null when the it gets to the service. I've tried it as a normal form submission and as an AJAX submission using the jQuery Form plugin, but get the same result either way. There are no exceptions logged either, making it all the more frustrating.

HTML Form

<form id="docCategoryForm" name="docCategoryForm" action="someAction" method="post" enctype="multipart/form-data">
Document
<input type="file" name="fileupload_name" id="fileupload_name" >
<button id="submitButton" type="submit" title="Select to search">Submit</button>
</form>

POM

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.9</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.9</version>
    <scope>provided</scope>
</dependency>

The Dependency Hierarchy in Eclipse shows that mimepull.jar 1.6 is also getting pulled in.

The REST service

@ResourceFilters({RequestLoggingResourceFilter.class})
@POST
@Path(PATH_UPLOAD)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public MyResponseObject uploadDocument(@FormDataParam("fileupload_name") InputStream file) {
  // Some code
}

If I add @FormDataParam("fileupload_name") FormDataContentDisposition fileDetail, I get that just fine, but the InputStream is still null.

Here is the request after trying to upload a simple text file. Looks ok to me.

* Server in-bound request
> POST http://localhost:7001/webmodule/app/contmang/page/docCategory
/uploadDocument
> Host: localhost:7001
> Connection: keep-alive
> Content-Length: 209
> Accept: */*
> Origin: http://localhost:7001
> X-Requested-With: XMLHttpRequest
> User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36
> Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryl0Uv1KBFoVtOpHto
> Referer: http://localhost:7001/webmodule/app/contmang/page/docCategory
> Accept-Encoding: gzip,deflate,sdch
> Accept-Language: en-US,en;q=0.8
> Cookie: JSESSIONID_HEARS=yWHQSpFbZnh1Jfp6hmn4xmfyxxTpybnvTTxsTykyqXzQyt207wym!-153888503
>  Entity:------WebKitFormBoundaryl0Uv1KBFoVtOpHto
Content-Disposition: form-data; name="fileupload_name"; filename="test.txt"
Content-Type: text/plain

Hello, World!!

------WebKitFormBoundaryl0Uv1KBFoVtOpHto--

Thought I should bring closure to this one. The problem ended up being really dumb. Somehow the wrong InputStream class had been imported. Once I changed it to java.io.InputStream it worked as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM