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

人盡茶涼 提交于 2019-12-11 23:20:37

问题


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 (e.g. 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.


回答1:


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
}


来源:https://stackoverflow.com/questions/15157677/java-rest-correct-parameters-for-jquery-multipart-form-data-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!