package com.zjxt.demo.test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: heiheihaxi
* @Date: 2019/4/26 10:02
*/
public class TestUploader {
public static void main(String[] args) throws IOException {
String url="http://localhost:8085/Uploader/";
List<File> files = new ArrayList<>();
File f1=new File("C:\\Users\\zzh\\Desktop\\tmp\\临时.jpg");
File f2=new File("C:\\Users\\zzh\\Desktop\\tmp\\aaa.jpg");
files.add(f1);
files.add(f2);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置参数
MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
// 解决乱码问题
meBuilder.setCharset(Charset.forName("UTF-8"));
meBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
for (File file : files) {
FileBody fileBody = new FileBody(file , ContentType.create("multipart/form-data",Charset.forName("UTF-8")));
meBuilder.addPart("files", fileBody);
}
HttpEntity reqEntity = meBuilder.build();
httpPost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
String result = EntityUtils.toString(resEntity,"utf-8");
System.out.println(result);
}
}
}
}
如果参数为普通的字符串参数,可以用以下方式
package com.zjxt.demo.test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: heiheihaxi
* @Date: 2019/4/26 10:23
*/
public class Test {
public static void main(String[] args) throws IOException {
String url="http://localhost:8087/Uploader/";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);// 创建httpPost
File f1=new File("C:\\Users\\zzh\\Desktop\\tmp\\bbb.mp3");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("fileName", f1.getName()));
nvps.add(new BasicNameValuePair("lastModified", Long.toString(f1.lastModified())));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
System.out.println("responseEntity:"+responseEntity);
}
else{
System.out.println("请求返回:"+state+"("+url+")");
}
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}