我正在尝试使用带有Jersey的multipart / form-data客户端上传文件和其他表单数据 . 我也使用Jersey上传到REST Web服务 . 这是服务器代码:
@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}";
}
}
我已经在桌面上使用基本的html表单测试了服务器代码,它运行正常 . 问题似乎在客户端 . 这是相关的客户端代码 .
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);
我从服务器得到400响应“客户端发送的请求在语法上不正确”
这是从 Logger 中吐出的消息,这个消息没有文件来保持输出简短:
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--
我在客户端做错了什么才能使其正常工作?
4 回答
如果要将字符串添加到
FormDataMultiPart
,只需使用.field("name", "value")
方法,就像它用于文件附件一样(queryParam不起作用) .以下是一份工作样本:
首先,服务器部分将读取的文件的内容作为String返回:
二,发布文件的客户端方法:
三,测试环境:
最后,maven依赖项:
file.txt
位于类路径的根目录中,包含Hello, World
.Yves解决方案在客户端不适合我 . 我环顾四周,发现:
http://puspendu.wordpress.com/2012/08/23/restful-webservice-file-upload-with-jersey/
http://neopatel.blogspot.de/2011/04/jersey-posting-multipart-data.html
http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
不适用于我目前的球衣1.18(见下面的pom提取物) . 最麻烦的是客户端 . 我会收到如下错误消息:
服务器端使用这段代码快速工作(它对上传的InputStream没有任何兴趣 - 适合您的需求)
客户端将使用此代码:
pom.xml提取:
或者只是写一个新文件并上传它: