檢查你的服務器端cxf的配置是如下所示。
@POST
@Path("/upload")
@Produces(MediaType.TEXT_XML)
public String upload(
@Multipart(value = "File", type = MediaType.APPLICATION_OCTET_STREAM) final InputStream fileStream,
@Multipart(value = "DATA1", type = MediaType.TEXT_PLAIN) final String fileNumber,
@Multipart(value = "DATA2", type = MediaType.TEXT_PLAIN) final String outputType) {
BufferedImage image;
try {
image = ImageIO.read(fileStream);
LOG.info("Received Image with dimensions {}x{} ", image.getWidth(), image.getHeight());
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
LOG.info("Received Multipart data1 {} ", fileNumber);
LOG.info("Received Multipart data2 {} ", outputType);
return "Recieved all data";
}
測試的客戶端文件
public static void main(String[] args) throws IOException {
WebClient client = WebClient.create("http://localhost:8080/services/kp/upload");
ClientConfiguration config = WebClient.getConfig(client);
config.getInInterceptors().add(new LoggingInInterceptor());
config.getOutInterceptors().add(new LoggingOutInterceptor());
client.type("multipart/form-data");
InputStream is = FileUtils.openInputStream(new File("vCenter_del.jpg"));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
Attachment att = new Attachment("File", is, cd);
Attachment pageNumber = new Attachment("DATA1", MediaType.TEXT_PLAIN, "1");
Attachment OutputType = new Attachment("DATA2", MediaType.TEXT_PLAIN, "2");
attachments.add(att);
attachments.add(pageNumber);
attachments.add(OutputType);
MultipartBody body = new MultipartBody(attachments);
Response res = client.post(body);
String data = res.readEntity(String.class);
System.out.println(data);
}
注:總之,如果在內容的ID不匹配,如果文件,數據1或內容類型,服務器可能無法收到兩個數據你會得到適當的錯誤,如400和415分別。