问题描述
我们在Safari 5.x的网站文件上传功能中遇到了问题.
We've come across a problem in our websites file upload functionality for Safari 5.x.
JQuery通常以分配了正确的Content-Type(例如image/png)的文件的形式将文件发送到REST服务,但是使用Safari 5.x时,它似乎只能以"multipart/form-data"的形式发送
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"
我尝试添加新的终结点,以通过Jersey和RestEasy接受此终结点,但是我没有成功.
I've tried adding the new endpoint to accept this via both Jersey and RestEasy, but I have had no success.
我认为问题很简单,我在确定参数应该是什么时遇到了麻烦.不管我尝试什么,它似乎都会导致415响应.
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.
标题
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
有效载荷
------WebKitFormBoundary15QUDazCkPkvqWTQ
Content-Disposition: form-data; name="files[]"; filename="myFile.png"
Content-Type: image/png
------WebKitFormBoundary15QUDazCkPkvqWTQ--
我已经在API方面尝试了以下两种方法:
I've tried both of the following on the API side:
泽西岛
@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;
}
}
有人可以指出我出了什么问题吗?我更愿意坚持使用Jersey,但目前对任何可行的解决方案都很满意.
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.
推荐答案
我使用Jersey,但是我从未尝试使用@MultipartForm UploadForm表单
I use Jersey, but I never tried to use @MultipartForm UploadForm form
我的参数是:
@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
}
这篇关于Java REST-JQuery多部分/表单数据文件上传的参数正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!