在httppost上预先附加到文件的额外行

时间:2016-09-14 13:06:29

标签: java apache-httpclient-4.x

在httppost上预先附加到文件的额外行。

  

- O0sNZZ0ePHR0Rg-tonfSMTbakWL6aYsI1jlV ^ M Content-Disposition:form-data; NAME = “文件”; filename =“2016-09-13_23:54:23_123143_1896875081.xml”^ M Content-Type:application / octet-stream ^ M Content-Transfer-Encoding:binary ^ M ^ M

import org.apache.http.client.HttpClient;
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(URL);
MultipartEntity multipart = new MultipartEntity();
File file = new File(FOLDER_PATH + "/" + name);
multipart.addPart("file", new FileBody(file));
post.setEntity(multipart);
HttpResponse response = client.execute(post);

发布文件需要做什么?

1 个答案:

答案 0 :(得分:1)

如果您不想要额外的东西,那么您不应该使用MultipartEntity。相反,您应该使用FileEntity

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(URL);
File file = new File(FOLDER_PATH + "/" + name);
FileEntity fileEntity = new FileEntity(file);
post.setEntity(fileEntity);
HttpResponse response = client.execute(post);