我正在尝试使用Jersey使用POST发送ByteArrayOutputStream zip文件。
Client client = Client.create();
client.resource(url);
ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, myBaosObject.toByteArray());
但是在服务器端,我收到:
WARN org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper-javax.ws.rs.WebApplicationException:org.apache.cxf.interceptor.Fault:无法确定消息的边界!
我的pom:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>
当我用Postman调用ws方法时,文件发送成功。
我还要做些什么?
答案 0 :(得分:0)
我能够做到这一点:
File file = null;
try {
// Transform baos into file
InputStream is = new ByteArrayInputStream(baos.toByteArray());
file = File.createTempFile("file ", "zip");
FileUtils.copyInputStreamToFile(is, file);
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
// Send file as part of body
FileBody uploadFilePart = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", uploadFilePart);
httpPost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httpPost);
return response.toString();
} finally {
if (file != null) {
file.delete();
}
}
我不得不添加以下依赖项:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>