简体   繁体   中英

Java REST - Correct parameters for JQuery multipart/form-data file upload?

We've come across a problem in our websites file upload functionality for Safari 5.x.

JQuery normally sends the file to the REST service as a File with the correct Content-Type (eg image/png) assigned, however with Safari 5.x it appears it can only send it as "multipart/form-data"

I've tried adding the new endpoint to accept this via both Jersey and RestEasy, but I have had no success.

I believe the problem is simply that I'm having trouble determining what the parameters should be. No matter what I try it seems to result in a 415 response.

The request being sent by the client (which I have no control over) looks as follows: Note: It is only a single file, however it appears to support multiple.

Header

Accept:application/json, text/javascript, */*; q=0.01
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary15QUDazCkPkvqWTQ
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2

Payload

------WebKitFormBoundary15QUDazCkPkvqWTQ
Content-Disposition: form-data; name="files[]"; filename="myFile.png"
Content-Type: image/png


------WebKitFormBoundary15QUDazCkPkvqWTQ--

I've tried both of the following on the API side:

Jersey

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadMultipart(FormDataMultiPart multiPart) throws IOException{ 

    List<FormDataBodyPart> fields = multiPart.getFields("files");        
    for(FormDataBodyPart field : fields){
        InputStream inputStream = field.getValueAs(InputStream.class);
    }

    // respond...
}

RestEasy

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadMultipart(@MultipartForm UploadForm form) {

    System.out.println(form.getFile().length);
    System.out.println(form.getName());

    //respond...
}



public class UploadForm {

    private String name;
    private File file;  /// Have tried various Objects & Arrays here - all with no success;

    @FormParam("filename")
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @FormParam("files")
    public void setFile(File file) {
        this.file = file;
    }

    public File[] getFile() {
        return file;
    }
}

Could someone please point out what I'm getting wrong? I'd prefer to stick with Jersey, but happy for any working solution at this point.

I use Jersey, but I never tried to use @MultipartForm UploadForm form

My parameter is :

@Context final HttpServletRequest request

You can use it this way (but this might be a bit overkill for you usage ):

final ServletFileUpload upload = new ServletFileUpload();
final FileItemIterator iter = upload.getItemIterator(request);

 while (iter.hasNext()) {
        //You should have only one element, but you may have several as multipart content
        final FileItemStream item = iter.next();

        final String name = item.getFieldName();
        final InputStream stream = item.openStream();
        //... and here you've got your inputstream
}

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