前言:
之前用了spring框架的restTemplate实现multipart-form-data混合数据(包括多个文件和json字符串等)的上传,现在使用httpClient方式来实现。
环境:
<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.3</version> </dependency>
实现:
发送端:
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; 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.ContentBody; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils;
public static void main(String[] args) { List<String> filePaths = new ArrayList<>(); filePaths.add("E:\\test\\picture\\大.png"); filePaths.add("E:\\test\\picture\\2.png"); sendMultiFormMixData("http://localhost:8080/receive", filePaths, "测试多文件数据中文乱码"); }
public static void sendMultiFormMixData(String url, List<String> filePaths, String data) { HttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); try { // 设置传输参数,设置编码。设置浏览器兼容模式,解决文件名乱码问题 MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532); for (String filePath : filePaths) { File file = new File(filePath); if (!file.exists()) { continue; } else { // // 无法解决中文文件名?的问题 // ContentType contentType = ContentType.create("multipart/form-data", Charset.forName("UTF-8")); // ContentBody fileBody = new FileBody(file, contentType); ContentBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA); builder.addPart("file", fileBody); } } // 解决中文字符?的问题 ContentType contentType = ContentType.create("text/plain", Charset.forName("UTF-8")); builder.addPart("data", new StringBody(data, contentType)); HttpEntity httpEntity = builder.build(); httpPost.setEntity(httpEntity); HttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String jsonData = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println("response result = " + jsonData); } else { System.out.println("no response"); } } catch (Exception e) { e.printStackTrace(); } }
接收端:
@PostMapping(value = "/receive") public Object testUploading(@RequestParam("file") MultipartFile[] files , String data) { String filepath = "E:\\test\\save\\"; File targetFile = new File(filepath); if (!targetFile.exists()) { targetFile.mkdirs(); } List<String> dp = new ArrayList<>() ; for(int i = 0 ; i < files.length ; i++){ MultipartFile file = files[i] ; try { FileOutputStream out = new FileOutputStream(filepath + file.getOriginalFilename()); out.write(file.getBytes()); } catch (Exception e) { e.printStackTrace(); } } return "SUCCESS"; }
总结:
1、httpClient工具类在平常的工作中使用还是比较广泛的,一般网上都到的对其封装的工具类都是比较好的,除了一些特殊的要求需要自己查看资料编写一下,问题不大;
2、注意导包的问题以及一些api过时,既然有新的,代码上尽量还是用新的。