I have an HTML form with multiple fields and a file upload.
On the Java side, I get 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 this is not what I need, as illustrated by what it uploadedInputStreamcontains (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, the form has more fields than just a file. But if I change the signature uploadFileto
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]
Switching to @FormParam("date")also does not help, since it is not in the end and returnsNullPointerException
EDIT. I like the assumption suggested in the answer below. I really decided not to mention something directly (you can see it in the code, though). I am using a custom XWiki framework. It is possible that the body is read, and then there is nothing to read.