下面列出了org.apache.http.entity.mime.FormBodyPart#addField ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testFileUploadWithEagerParsingAndNonASCIIFilename() throws Exception {
DefaultServer.setRootHandler(new EagerFormParsingHandler().setNext(createHandler()));
TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
MultipartEntity entity = new MultipartEntity();
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
File uploadfile = new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile());
FormBodyPart filePart = new FormBodyPart("file", new FileBody(uploadfile, "τεστ", "application/octet-stream", Charsets.UTF_8.toString()));
filePart.addField("Content-Disposition", "form-data; name=\"file\"; filename*=\"utf-8''%CF%84%CE%B5%CF%83%CF%84.txt\"");
entity.addPart(filePart);
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
public MultipartEntityBuilder addPart(FormBodyPart bodyPart) {
if (bodyPart == null) {
return this;
}
if (this.bodyParts == null) {
this.bodyParts = new ArrayList<FormBodyPart>();
}
if(mtom) {
Header header = bodyPart.getHeader();
String contentID;
String fileName = bodyPart.getBody().getFilename();
header.removeFields("Content-Disposition");
if(fileName == null) {
contentID = "<"+bodyPart.getName()+">";
}
else {
bodyPart.addField("Content-Disposition", "attachment; name=\""+bodyPart.getName()+"\"; filename=\""+fileName+"\"");
contentID = "<"+fileName+">";
}
bodyPart.addField("Content-ID", contentID);
if(firstPart == null)
firstPart = contentID;
}
this.bodyParts.add(bodyPart);
return this;
}