I'm trying to upload a file with JAX-RS and TomEE's Apache CXF implementation (2.6.14), but the uploaded file is always null.
Here is the code:
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@Multipart(value = "file") @NotNull Attachment attachment) throws UnsupportedEncodingException {
try {
System.out.println(attachment);
return Response.ok("file uploaded").build();
} catch (Exception ex) {
logger.error("uploadFile.error():", ex);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
and a very easy HTML-file for the upload:
<form action="http://127.0.0.1:8080/eegrating/restapi/cashflowparameter/upload" method="post" enctype="multipart/form-data">
<p>File:<br>
<input name="file" type="file" size="50" maxlength="100000" accept="text/*">
<input type="submit" name="Submit" value="Send">
</p>
</form>
The request header looks fine:
------WebKitFormBoundaryOCleIjB2JgeySK0w Content-Disposition: form-data; name="file"; filename="git.txt" Content-Type: text/plain
But the attachment is always null. Any suggestions? Thanks in advance.
For cxf specific (not jersey) your code could be as below
your code is not working cause we have to pass specific
contentId
to the@Multipart
. the code look like as belownote*:
contentId
is not your file property name that you are sending from clientI would suggest to use
MultipartBody
instead of@Multipart
refer : http://cxf.apache.org/docs/jax-rs-multiparts.html
I shall be glad for any improvement or rectifying the content of the post :)
First, are you using Apache TomEE with JAX-RS?, if not you should, because this bundles JAX-RS. Try with this code. I'm using CXF specific features, I tested and works well. This resource just produces an HTML result, you can adapt it of course. As you see I referenced the CXF dependency as provided because TomEE includes it. I'm posting every file needed.
No web.xml file.
META-INF/context.xml
index.jsp
upload.MyApplication.java
upload.UploadResource.java
pom.xml