Hello Adam,
I reduced your sample to:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public String create(@FormDataParam("file") InputStream file,
@FormDataParam("file") FormDataContentDisposition fileInfo,
@FormDataParam("name") String name,
@FormDataParam("description") String description) {
return "YEAH!";
}
and client code:
@Test
public void testPostMultipart() {
File file = new File("./pom.xml");
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource webResource =
client.resource("
http://localhost:9998/helloworld");
FormDataMultiPart fdmp = new FormDataMultiPart();
if (file != null) {
fdmp.bodyPart(new FileDataBodyPart("file", file,
MediaType.APPLICATION_OCTET_STREAM_TYPE));
}
fdmp.bodyPart(new FormDataBodyPart("name", "ingredientName"));
fdmp.bodyPart(new FormDataBodyPart("description",
"ingredientDesc"));
ClientResponse response =
webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class,
fdmp);
String string = response.getEntity(String.class);
System.out.println(string);
}
and I'm not getting any errors etc, in fact returned string contains
"YEAH!" as expected. Further investigation don't show any error, I'm
able to print all parameters defined in resource method:
file # org.jvnet.mimepull.DataHead$ReadMultiStream_at_47e9d9b1
fileInfo # form-data; filename="pom.xml"; modification-date="Fri, 25
Mar 2011 08:43:16 GMT"; size=17364; name="file"
name # ingredientName
description # ingredientDesc
So I'm not sure where the problem could be.. Can you please try isolate it?
Thanks,
Pavel
On 4/24/11 8:01 PM, adam_ruggles wrote:
> I'm trying to upload a file and other form data using multipart/form-data
> client with Jersey. I'm uploading to a REST web service also using Jersey.
> Here is the server code:
>
> @POST
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> @Produces(MediaType.APPLICATION_JSON)
> public String create(@FormDataParam("file") InputStream file,
> @FormDataParam("file") FormDataContentDisposition fileInfo,
> @FormDataParam("name") String name,
> @FormDataParam("description") String description) {
> Ingredient ingredient = new Ingredient();
> ingredient.setName(name);
> ingredient.setDescription(description);
> ingredient.setImageName(fileInfo.getFileName());
> ingredient.setImagePath(context.getRealPath("/resources/uploads/"));
> // TODO save the file.
> try {
> JSONObject json = new JSONObject();
> try {
> ingredientService.create(ingredient);
> } catch (final InvalidParameterException ex) {
> logger.log(Level.INFO, ex.getMessage());
> json.put("result", false);
> json.put("error", ex.getMessage());
> return json.toString();
> } catch (final GoodDrinksException ex) {
> logger.log(Level.WARNING, null, ex);
> json.put("result", false);
> json.put("error", ex.getMessage());
> return json.toString();
> }
> json.put("ingredient", JsonUtil.ingredientToJSON(ingredient));
> return json.put("result", true).toString();
> } catch (JSONException ex) {
> logger.log(Level.SEVERE, null, ex);
> return "{\"result\",false}";
> }
> }
>
> I've tested the server code using a basic html form on my desktop and it
> works fine. The problem seems to be in the client. Here is the relevant
> client code.
>
> ClientConfig config = new DefaultClientConfig();
> client = Client.create(config);
> client.addFilter(new LoggingFilter());
> webResource =
> client.resource("http://localhost:8080/webapp/resources").path("ingredient");
> FormDataMultiPart fdmp = new FormDataMultiPart();
> if (file != null) {
> fdmp.bodyPart(new FileDataBodyPart("file", file,
> MediaType.APPLICATION_OCTET_STREAM_TYPE));
> }
> fdmp.bodyPart(new FormDataBodyPart("name", ingredient.getName()));
> fdmp.bodyPart(new FormDataBodyPart("description",
> ingredient.getDescription()));
>
> ClientResponse response =
> webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class,
> fdmp);
> String string = response.getEntity(String.class);
> logger.log(Level.INFO, "response: {0}", string);
>
> I'm getting a 400 response from the server "The request sent by the client
> was syntactically incorrect"
>
> Here is the message that is spit out of the logger, this one is without a
> file to keep the output brief:
>
> 1> POST http://localhost:8080/webapp/resources/ingredient
> 1> Content-Type: multipart/form-data
> 1>
> --Boundary_5_1545082086_1303666703655
> Content-Type: text/plain
> Content-Disposition: form-data;name="name"
> Adam
> --Boundary_5_1545082086_1303666703655
> Content-Type: text/plain
> Content-Disposition: form-data;name="description"
> Test
> --Boundary_5_1545082086_1303666703655--
>
> What am I doing wrong in the client to get this working correctly?
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Trying-to-upload-a-file-using-the-client-libraries-tp6301309p6301309.html
> Sent from the Jersey mailing list archive at Nabble.com.
>