需要先引入MAVEN(spring-boot-starter-test) 它里面有个MockMultipartFile类,可以将File转换为MultipartFile。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
1:先准备文件工具类
import org.apache.commons.httpclient.util.HttpURLConnection;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/***
* 文件工具类
*/
public class FileHttpClient {
public static String sendFilePost(String url, Map<String, String> headers, String body, List<MultipartFile> multipartFiles, String fileParName, Map<String, String> fileNames) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(java.nio.charset.Charset.forName("UTF-8"));
//单个文件
String fileName = null;
MultipartFile multipartFile = null;
for (int i = 0; i < multipartFiles.size(); i++) {
//第一个参数为 相当于 Form表单提交的file框的name值 第二个参数就是我们要发送的InputStream对象了
//第三个参数是文件名
//3)
multipartFile = multipartFiles.get(i);
fileName = fileNames.get(multipartFile.getName());
InputStream inputStream = null;
try {
inputStream = multipartFile.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
builder.addBinaryBody(fileParName,inputStream, ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
}
//4)构建请求参数 普通表单项
/* StringBody stringBody = new StringBody("12", ContentType.MULTIPART_FORM_DATA);
builder.addPart("id", stringBody);*/
//决中文乱码
ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, Consts.UTF_8);
builder.addTextBody("params", body, contentType);
HttpEntity entity = builder.build();
if (null!=headers&&headers.size()>0){
for (Map.Entry<String, String> entry : headers.entrySet()) {
httppost.addHeader(entry.getKey(),entry.getValue());
}
}
httppost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
return null;
}
String result = null;
try {
if(response!=null){
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null && response.getStatusLine().getStatusCode()== HttpURLConnection.HTTP_OK) {// 判断请求状态
result = EntityUtils.toString(httpEntity);
}
}
} catch (Exception e) {
} finally {
try {
if(response!=null){
response.close();
}
} catch (IOException e) {
}
}
return result;
}
}
2:ServiceImpl方法调用(根据注释步骤,每个人的业务方法不一样)
List<MultipartFile> multipartFiles = new ArrayList<>(); //1:定义一个MultipartFile集合
Map<String, String> fileNames = new HashMap<>();
for(int i=0; i<list.size(); i++){
Map<String, String> map = list.get(i);
String fileName = map.get("fileName");
String filePath = map.get("filePath");
String[] split = filePath.split("\\.");
System.out.println("fileName:"+fileName);
System.out.println("filePath:"+filePath);
if(split.length>1){
fileNames.put(fileName, fileName+"."+split[1]);
File file = new File(filePath); //2:根据路径获取File
if(file.exists()){
FileInputStream fileInputStream = new FileInputStream(file); //3:将File转换为FileInputStream
MultipartFile multipartFile = new MockMultipartFile(fileName, fileInputStream); //4:将fileInputStream转换为MultipartFile
multipartFiles.add(multipartFile); //5:放入multipartFiles集合中
}
}
}
configJson.put("taskToolNodeId", taskToolNodeId);
configJson.put("tenantId", tenantId);
JSONObject params = new JSONObject();
params.put("params", configJson);
String s = FileHttpClient.sendFilePost(ip + saveUrl, null, params.toJSONString(),
multipartFiles, "file", fileNames);
//6:调用远程方法(地址url,请求头(可以为null),携带json参数,文件流集合,调用接口接收的文件paramName,文件名称集合)