[英]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. 我们在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" jQuery通常将文件作为分配了正确的Content-Type(例如image / png)的文件发送到REST服务,但是对于Safari 5.x,它似乎只能将其作为“ multipart / form-data”发送
I've tried adding the new endpoint to accept this via both Jersey and RestEasy, but I have had no success. 我尝试添加新的终结点以同时通过Jersey和RestEasy接受此设置,但是我没有成功。
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. 不管我尝试什么,它似乎都会导致415响应。
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: 我已经在API方面尝试了以下两种方法:
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. 我宁愿坚持使用Jersey,但目前对任何可行的解决方案都很满意。
I use Jersey, but I never tried to use @MultipartForm UploadForm form 我使用Jersey,但从未尝试使用@MultipartForm UploadForm表单
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.