Rest 如何将新头添加到jersey客户端以上载多部分文件
请查找以下jersey客户端代码以上载多部分文件:Rest 如何将新头添加到jersey客户端以上载多部分文件,rest,jersey,jax-rs,Rest,Jersey,Jax Rs,请查找以下jersey客户端代码以上载多部分文件: String url = "http://localhost:7070" Client client = Client.create(); WebResource webresource = client.resource(url); File file = new File("C://Data//image1.jpg"); File thumbnail = new File("C://Data/image2.jpg"); InputStr
String url = "http://localhost:7070"
Client client = Client.create();
WebResource webresource = client.resource(url);
File file = new File("C://Data//image1.jpg");
File thumbnail = new File("C://Data/image2.jpg");
InputStream isthumbnail = new FileInputStream(thumbnail);
InputStream isfile = new FileInputStream(file);
FormDataMultiPart multiPart = new FormDataMultiPart();
FormDataBodyPart bodyPart1 = new FormDataBodyPart(FormDataContentDisposition.name("Thumbnail").fileName("thumbnail").build(), isthumbnail, MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataBodyPart bodyPart2 = new FormDataBodyPart(FormDataContentDisposition.name("File").fileName("file").build(), isfile, MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(bodyPart);
multiPart.bodyPart(bodyPart1);
//New Headers
String fileContentLength = "form-data; contentLength=\""+Long.toString(file.length())+ "\"";
String thumbnailContentLength = "form-data; contentLength=\""+Long.toString(file.length())+ "\"";
final ClientResponse clientResp = webresource.type(MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.APPLICATION_XML).post(ClientResponse.class, multiPart);
System.out.println("File Upload Success with Response"+clientResp.getStatus());
我需要添加字符串fileContentLength和thumbnailContentLength作为标题
内容长度。
如何将标题添加为multipart的一部分并发布请求?如有任何帮助,请使用a作为参数
因此,添加.size给了我类似于dispo{form data;“filename=“test.txt”;size=31;name=“file”}的东西。我必须执行bodyPart.getHeaders().add(“Content Length”,String.valueOf(value.getBytes().Length));
final FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final String value = "Hello World";
final FormDataContentDisposition dispo = FormDataContentDisposition
.name("file")
.fileName("test.txt")
.size(value.getBytes().length)
.build();
final FormDataBodyPart bodyPart = new FormDataBodyPart(dispo, value);
formDataMultiPart.bodyPart(bodyPart);