Reputation: 19488
(specifically RESTeasy)
It would be nice (for a single file) to have a method signature like:
public void upload(@FormParam("name") ..., @FormParam("file") file: InputStream)
...
doable? or am I dreaming? doesn't seem to be that simple.
Upvotes: 29
Views: 46869
Reputation: 2312
Since JAX-RS 3.1 (officially "Jakarta RESTful Web Services"; part of Jakarta EE 10), you can do:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postWidget(
@FormParam("name") String name,
@FormParam("file") EntityPart file) {
var fileName = file.getName().orElseThrow();
var inputStream = file.getContent();
}
This is not only a standard, it's also easier to use.
Note that I just found that the @FormParam
doesn't work here in RestEasy 6.2.7 (I just submitted an issue). You'll have to read all parts and find your own:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postWidget(
List<EntityPart> parts) {
var file = parts.stream().filter(part -> "file".equals(part.getName())).findFirst().orElseThrow();
var inputStream = file.getContent();
}
Upvotes: 2
Reputation: 4738
The key is to leverage the @MultipartForm annotations that comes with RESTEasy. This enables you to define a POJO that contains all the parts of the form and bind it easily.
Take for example the following POJO:
public class FileUploadForm {
private byte[] filedata;
public FileUploadForm() {}
public byte[] getFileData() {
return filedata;
}
@FormParam("filedata")
@PartType("application/octet-stream")
public void setFileData(final byte[] filedata) {
this.filedata = filedata;
}
}
Now all you need to do is use this POJO in the entity which would look something like this:
@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response create(@MultipartForm FileUploadForm form)
{
// Do something with your filedata here
}
Upvotes: 31