我正在尝试使用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方法时,文件成功发送。
我还需要做什么?
我能做到这一点:
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>