public static String uploadFileToOSS(MultipartFile multfile) {
CloseableHttpClient client = null;
String result = null;
try {
client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("请求地址");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//addTextBody(String,String)该方法可以传入参数,例如请求的地址需要接受一个id
//builder.addTextBody("id", "1");
builder.addTextBody("arg", "value");
//addBinaryBody()该方法传入二进制内容,可以传入InputStream,File, 参数三是传入的类型,参数四是文件名称
builder.addBinaryBody("form", multfile.getInputStream(), ContentType.MULTIPART_FORM_DATA, "1.jpg");
httpPost.setEntity(builder.build());
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
// 取回服务器端的响应结果
String result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}