jersey client上传文件demo
File file = new File("/tmp/test.jpg");
System.out.println(file.exists());
FormDataMultiPart part = new FormDataMultiPart();
part.bodyPart(new FileDataBodyPart("file", file));
part.bodyPart(new FormDataBodyPart("myparam", "false"));
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
Client writerClient = Client.create(cc);
// 处理文件将超时设置为10S
writerClient.setConnectTimeout(new Integer(3000));
writerClient.setReadTimeout(new Integer(3000));
try {
WebResource resource = writerClient.resource("http://127.0.0.1:8080/rest/upload");
String response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
System.out.println(response);
} finally {
writerClient.destroy();
}
网友评论