多附件上传实例:
/**
* 多附件上传
* @param host
* @param uri
* @param attachment 附件
* @param param body参数
* @return
*/
public String upload(String host, String uri, Map<String,String> attachment, Map<String, String> param) {
logger.info("*****************request*****************");
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();;
File file = null;
InputStream inputStream = null;
String url = host + uri;
String rst = "";
try {
httpClient = HttpClients.createDefault();
logger.info("请求路径: "+ url);
HttpPost httpPost = new HttpPost(url);
for(String fileName:attachment.keySet()){
inputStream = this.getClass().getClassLoader().getResourceAsStream(attachment.get(fileName)); //获取resources下的资源文件
file = new File(attachment.get(fileName));
builder.addBinaryBody(fileName, inputStream, create("multipart/form-data", Consts.UTF_8), file.getName());
}
for (Map.Entry<String, String> entry : param.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
StringBody stringBody = new StringBody(value, create(
"text/plain", Consts.UTF_8));
builder.addPart(key, stringBody);
logger.info("请求参数:"+key+":"+ value);
}
HttpEntity reqEntity = builder.build();
httpPost.setEntity(reqEntity);
// 发起请求 并返回请求的响应
response = httpClient.execute(httpPost);
// 获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
// 打印响应内容
rst = EntityUtils.toString(resEntity, Charset.forName("UTF-8")) ;
logger.info("*****************response*****************");
logger.info("响应结果: " +rst);
}
// 销毁
EntityUtils.consume(resEntity);
return rst;
} catch (Exception e) {
logger.info("出错啦: "+e.getMessage());
e.printStackTrace();
return "出错了";
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
调用方法
public String uploadImage () throws Exception {
Map<String, String> params = new HashMap<>();
params.put("uploadType", "1");
params.put("repayReason", "测试数据");
Map<String, String> attachment= new HashMap<>();
attachment.put("imageFile","picture/repayPic.jpg"); //该附件放在resources包下的picture包里面
attachment.put("images","picture/repay.jpg");
String rst = baseRequest.upload("http://xxxx.com","/web/upload", attachment, params);
return rst;
}
请求类型表:查看
1.设置上传的模式;
setMode(HttpMultipartMode mode),其中mode主要有BROWSER_COMPATIBLE,RFC6532,STRICT三种,默认值是STRICT。
2.创建MultipartEntityBuilder对象,并添加需要上传的数据;
a.利用MultipartEntityBuilder.create()来创建对象;
b.addBinaryBody:以二进制的形式添加数据,可以添加File、InputStream、byte[]类型的数据。
addBinaryBody(String name, File file, ContentType contentType, String filename)
addBinaryBody(String name, InputStream stream, ContentType contentType, String filename)
addBinaryBody(String name, byte[] b, ContentType contentType, String filename)
c.addTextBody:添加文本数据
addTextBody(String name, String text, ContentType contentType)
d.addPart:以Key/Value的形式添加ContentBody类型的数据
addPart(String name, ContentBody contentBody)
MultipartEntityBuilder用法
2.MultipartEntityBuilder这个类主要用于创建HttpEntity。它的主要方法有:
修饰符和类型 | 方法和描述 |
MultipartEntityBuilder | addBinaryBody(String name, byte[] b) 将字节数组以二进制的形式添加数据。 |
MultipartEntityBuilder | addBinaryBody(String name, byte[] b, ContentType contentType, String filename) 将字节数组以二进制的形式添加数据。 |
MultipartEntityBuilder | addBinaryBody(String name, File file) 将文件以二进制的形式添加数据。 |
MultipartEntityBuilder | addBinaryBody(String name, File file, ContentType contentType, String filename) 将文件以二进制的形式添加数据。 |
MultipartEntityBuilder | addBinaryBody(String name, InputStream stream) |
MultipartEntityBuilder | addBinaryBody(String name, InputStream stream, ContentType contentType, String filename) 将输入流以二进制的形式添加数据。 |
MultipartEntityBuilder | addPart(String name, ContentBody contentBody) 添加ContentBody 类型的数据。 |
MultipartEntityBuilder | addTextBody(String name, String text) 添加文本数据。 |
MultipartEntityBuilder | addTextBody(String name, String text, ContentType contentType) 以指定的内容类型添加文本数据。 |
HttpEntity | build() 创建一个HttpEntity。 |
static MultipartEntityBuilder | create() 创建一个MultipartEntityBuilder对象。 |
MultipartEntityBuilder | setBoundary(String boundary) 设置边界。 |
MultipartEntityBuilder | setCharset(Charset charset) 设置请求的编码格式。 |
MultipartEntityBuilder | setLaxMode() |
MultipartEntityBuilder | setMode(HttpMultipartMode mode) 设置模式。 |
MultipartEntityBuilder | setStrictMode() |