问题
Quite a few links here that suggest using multipart/form-data and how to get the file upload here. Couldn't really find one for a combination of multiple files uploaded via CURL command and to a REST service accepting FormDataMultiPart.
The code in service currently looks like :
@javax.ws.rs.POST
@javax.ws.rs.Path("/sample-bulk")
@javax.ws.rs.Consumes(javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA)
public javax.ws.rs.core.Response bulkUpload(@FormDataParam("file") org.glassfish.jersey.media.multipart.FormDataMultiPart multiPart) {
log.info("{} log", multiPart.getField("file"));
return Response.ok().build();
}
and the CURL I am trying to call the service is as :
curl -X POST "http://localhost:37200/api/sample-bulk" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "file=@/Users/naman/Desktop/Media/video.mp4;type=video/mp4"
But it results in multiPart being null at the service and of course NPE.
Anything silly that I've missed here?
回答1:
The problem is with the method parameter (the presence of the @FormDataParam)
public Response bulkUpload(@FormDataParam("file") FormDataMultiPart multiPart) {}
@FormDataParam is used when you want to declaratively extract individual parts from the multipart request, whereas FormDataMultiPart is used to get an entire multipart body and programmatically extract each part. It's possible to have a nested multipart where a complete multipart is an individual part (in which case what you have would work), but this is not the case here.
If you remove the @FormDataParam("file"), then it will work as expected. You can start extracting parts out of the multipart using the method you are using getField(fieldName). This will give you a FormDataBodyPart for each part you extract. You can get the data with FormDataBodyPart#getValueAs(InputStream.class) if you want that part as an InputStream, or you can use File.class or byte[].class, whatever your preference. This is how to extract data from the FormDataMultiPart.
Each part has it's own name and you extract the part using that name. In the case of your cURL request, you sent one part, and the part's name is file. i.e. "file=@/Users/...". So if you want to send another part, just add another parameter with a different name, as mentioned by Vladimir:
curl -X POST "http://localhost:37200/api/sample-bulk"\
-H "accept: application/json"\
-H "Content-Type: multipart/form-data"\
-F "file1=@/Users/naman/Desktop/Media/video.mp4"\
-F "file2=@/Users/naman/Desktop/Media/another_video.mp4"
As I mentioned earlier, @FormDataParam is used to extract parts declaratively. You use the part's name as the annotation value. So with the previous cURL command, you could do.
public Response bulkUpload(
@FormDataParam("file1") InputStream file1,
@FormDaraParam("file1") FormDataContentDisposition file1Fdcd,
@FormDataParam("file2") InputStream file2,
@FormDaraParam("file2") FormDataContentDisposition file2Fdcd) {
}
You can get information about the part, such as the file name from the FormDataContentDisposition.
See also
- Why “FormDataMultiPart” type parameter is treated differently
- Jersey documentation for its multipart support (on the server side)
- File upload along with other object in Jersey restful web service
回答2:
Use this parameter
public Response uploadFile(@FormDataParam("files") List<FormDataBodyPart> file)
Use for loop
for (int j = 0; j < files.size(); j++) {
FormDataBodyPart this_formDataBodyPartFile = files.get(j);
ContentDisposition this_contentDispositionHeader = this_formDataBodyPartFile
.getContentDisposition();
InputStream this_fileInputStream = this_formDataBodyPartFile.getValueAs(InputStream.class);
FormDataContentDisposition fileDetail = (FormDataContentDisposition) this_contentDispositionHeader;
//Write the code upload code }
回答3:
Try using separate -F flag for every multipart entry, something like that:
curl -X POST "http://localhost:37200/api/sample-bulk" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "file=@/Users/naman/Desktop/Media/video.mp4" -F "type=video/mp4"
来源:https://stackoverflow.com/questions/56954122/multiple-files-upload-in-a-rest-service-using-jersey