Reputation: 2341
I have an HTML form with several fields and a file upload.
On the Java side, I'm receiving the form as follows, and it works:
@Component("org.phenotips.metabolites.FileUploaderResource")
@Path("/metabolites")
public class FileUploaderResource extends XWikiResource
{
@GET
@Path("/test")
public Response test() {
String response = "<html>Successful</html>";
return Response.ok(response).build();
}
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("filepath") InputStream uploadedInputStream
)
{
try {
String errorMessage = "This is an error response";
URI redirect = new URI("http://localhost:8080/");
return Response.temporaryRedirect(redirect).header("error_msg", errorMessage).build();
} catch (Exception ex) {
return Response.serverError().build();
}
}
}
But that's not what I need, as exemplified by what uploadedInputStream
contains (example)
-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="id"
0000002
-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="column_order"
yes, no,
-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="date"
05/02/2015
-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="filepath"; filename="phenotips_2014-08-28_10-35.csv"
Content-Type: text/csv
"History (code)","History","History (code and name)","observed"
"","","","HP:0001622"
-----------------------------184640265716083967621914753489--
As you can see there are more fileds to the form than just the file.
But if I change the uploadFile
's signature to
public Response uploadFile(
@FormDataParam("date") String date,
@FormDataParam("filepath") InputStream uploadedInputStream
)
I get the following error:
The root resource class org.phenotips.metabolites.FileUploaderResource is not a valid root resource class: The entity is already read. The 1. parameter requires one of the following annotations: [interface javax.ws.rs.core.Context, interface javax.ws.rs.HeaderParam, interface javax.ws.rs.MatrixParam, interface javax.ws.rs.QueryParam, interface javax.ws.rs.PathParam, interface javax.ws.rs.CookieParam]
Changing to @FormParam("date")
also does not help, as it does not end up being found and returns a NullPointerException
EDIT. I like the guess proposed in the answer below. I did indeed decide not to mention something directly (you can see it in the code though) - I am using a custom framework, XWiki. It is entirely possible that the body is read, and then there's nothing left to read.
Upvotes: 4
Views: 150
Reputation: 209004
This is not an answer: Just seeing if maybe it's something the OP is not showing us or telling us
I have tested this with Postman, and I have no problem. Maybe it's something else you're not showing us.
@Path("/multipart")
public class MutlipartResource {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postMultiPart(@FormDataParam("date") String date,
@FormDataParam("filepath") InputStream stream)
throws Exception {
ImageIO.read(stream);
return Response.ok(date).build();
}
}
Here is my interpretation of the error, though I may be wrong, it's just a guess. It seems like something is reading the body part prior to reaching the method, so there is nothing left to be read. In which case the error maybe saying that since it is not a readable part, it should not be defined as a multipart part put instead some other form, and as you can only have one entity body, the date
cannot be a body, but instead must be a query param, path param, etc (something that's not a entity body). Again this is just a guess. But something maybe you can look into.
Upvotes: 2