import java.io.File;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Test;
public class MyTest {
@Test
public void test2() {
try {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("");// 请求地址
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
ContentType strContent = ContentType.create("text/plain", Charset.forName("UTF-8"));
meb.addBinaryBody("file1", new File(""));// 需要传输的文件,参数也可以是流
meb.addTextBody("username", "陈智勇", strContent);// 普通字段,指定编码
HttpEntity httpEntity = meb.build();
httpPost.setEntity(httpEntity);
httpClient.execute(httpPost);
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用httpclient传输流加普通字段
最新推荐文章于 2024-07-04 13:07:39 发布