分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

使用 HttpClient ?进行文件上传

发布时间:2023-09-06 02:31责任编辑:赖小花关键词:文件上传

1.使用 AddPart 方法

public static void upload(String authorization,String baseUrl,String filePath,String userId,String isOverWrite,String remotePath){

??CloseableHttpClient client = HttpClients.createDefault();
??String uploadFile_url = Utils.getValue("uploadFile.url");
??String url=baseUrl+uploadFile_url;

??HttpPost post = new HttpPost(url);
??System.out.println("---->upload_url:"+url);

??//设置请求头
??HashMap<String, String> header = new HashMap<>();
??header.put("Authorization", authorization);//Basic YWRtaW4xMjM6MTIzcXdl
??Iterator<String> iterator_header = header.keySet().iterator();
??while(iterator_header.hasNext()){
?????String key = iterator_header.next();
?????post.addHeader(key,header.get(key));
??}

??//构造待上传数据,加入builder
??File file=new File(filePath);
??String fimeName = file.getName();
??MultipartEntityBuilder builder = MultipartEntityBuilder.create();
??builder.setMode(HttpMultipartMode.RFC6532);
??builder.setCharset(Charset.forName("UTF-8"));
??builder.addPart("userId", new StringBody(userId, ContentType.MULTIPART_FORM_DATA));
??builder.addPart("remotePath", new StringBody(remotePath, ContentType.MULTIPART_FORM_DATA));
??builder.addPart("isOverWrite", new StringBody(isOverWrite, ContentType.MULTIPART_FORM_DATA));
??builder.addPart("isSendEmail", new StringBody(String.valueOf(false), ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowChunkNumber", new StringBody("1", ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowChunkSize", new StringBody("104857600", ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowCurrentChunkSize", new StringBody("90058", ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowTotalSize", new StringBody("90058", ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowIdentifier", new StringBody("90058-2pdf", ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowFilename", new StringBody(fimeName, ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowRelativePath", new StringBody(fimeName, ContentType.MULTIPART_FORM_DATA));
??builder.addPart("flowTotalChunks", new StringBody("1", ContentType.MULTIPART_FORM_DATA));
??builder.addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY));
??HttpEntity entity = builder.build();

??post.setEntity(entity);
??HttpResponse response = null;
??try {
?????response = client.execute(post);
?????System.out.println("---->reponse:"+response);
?????entity = response.getEntity();
?????JSONObject result = JSONObject.parseObject(EntityUtils.toString(entity));
??} catch (IOException e) {
?????e.printStackTrace();
??}
}


2.使用 addTextBody 方法

 ?public static void uploadInterface(String authorization,String baseUrl,String filePath,String userId,String isOverWrite,String remotePath){

?????CloseableHttpClient client = HttpClients.createDefault();
    String uploadFile_url = Utils.getValue("uploadFile.url");
    String url=baseUrl+uploadFile_url;
?????HttpPost httpPost=new HttpPost(url);//通过post传递 ?
?????
    //heard处理
?????HashMap<String, String> header = new HashMap<>();
    header.put("Authorization", authorization);//
?????Iterator<String> iterator_header = header.keySet().iterator();
?????while(iterator_header.hasNext()){
??????String key = iterator_header.next();
??????httpPost.addHeader(key,header.get(key));
?????}
?????File file=new File(filePath);
?????String ?fileName=file.getName();
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

??  builder.addTextBody("userId", userId,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("remotePath", "/",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("isOverWrite", "-1",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("isSendEmail", String.valueOf(false),ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowChunkNumber", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowChunkSize", "104857600",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowCurrentChunkSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowTotalSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowIdentifier", "3493921-1jpg",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowFilename", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowRelativePath", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
?????.addTextBody("flowTotalChunks", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"));

?   builder.addBinaryBody("file", file, ContentType.create("image/jpeg"), fileName);
?????HttpEntity entity = builder.build();

?????httpPost.setEntity(entity); ?
?????
?????/**发送请求*/ ?
?????try { ?
??????String result="";
?????????HttpResponse response=client.execute(httpPost); ?
?????????HttpEntity responseEntity = response.getEntity();
?????????if (responseEntity != null) {
?????????????// 将响应内容转换为字符串
?????????????result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
?????????}
?????????//判断是否上传成功 ?返回200 ?
?????????if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ ?
?????????} ?
?????} catch (ClientProtocolException e){ ?
?????????e.printStackTrace(); ?
?????} catch (IOException e) { ?
?????????e.printStackTrace(); ?
?????} ?

?}


3.addBinaryBody中文文件乱码解决方法

ContentType contentType = ContentType.create("text/plain",Charset.forName("UTF-8"));
HttpEntity caiJiEntity= MultipartEntityBuilder.create()
.addBinaryBody("file", new File("d://2.mp4"), ContentType.create("video/mp4"), "2.mp4")
.addBinaryBody("file1",new File("d:/1-120915094151.jpg"),

 ContentType.create("image/jpg"), "1-120915094151.jpg")

.addTextBody("mtxs", "三面立柱",contentType)
.build();

使用 HttpClient ?进行文件上传

原文地址:https://www.cnblogs.com/xiaomifeng0510/p/10331493.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved