httpclient通过post multipart/form-data 上传文件
public static String httpClientUploadFile(String url, File file) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
//每个post参数之间的分隔。随意设定,只要不会和其他的字符串重复即可。
String boundary ="--------------4585696313564699";
try {
//文件名
String fileName = file.getName();
HttpPost httpPost = new HttpPost(url);
//设置请求头
httpPost.setHeader("Content-Type","multipart/form-data; boundary="+boundary);
//HttpEntity builder
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//字符编码
builder.setCharset(Charset.forName("UTF-8"));
//模拟浏览器
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//boundary
builder.setBoundary(boundary);
//multipart/form-data
builder.addPart("multipartFile",new FileBody(file));
// binary
// builder.addBinaryBody("name=\"multipartFile\"; filename=\"test.docx\"", new FileInputStream(file), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
//其他参数
builder.addTextBody("filename", fileName, ContentType.create("text/plain", Consts.UTF_8));
//HttpEntity
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
// 执行提交
HttpResponse response = httpClient.execute(httpPost);
//响应
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.err.println("result"+result);
return result;
}
//main 方法
public static void main(String[] args) {
httpClientUploadFile("http://127.0.0.1:8080/upload",new File("d:/test/test.docx"));
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。