欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

httpclient4.5 文件上传

程序员文章站 2022-07-13 13:53:50
...

 

compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.1'

compile group: 'commons-io', name: 'commons-io', version: '2.5'

compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.6'

compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.6'

 

// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient

compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'

 

 

 

 

package com.cloud.test;

 

import java.io.File;

import java.io.IOException;

import java.nio.charset.Charset;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;

 

import org.apache.http.HttpEntity;

import org.apache.http.HttpStatus;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.mime.MultipartEntity;

import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

 

public class HttpFilePost {

 

 

 

public static void main(String[] args) {

//上传文件

String fileName ="d:/123456.txt";

 

        HttpFilePost json = new HttpFilePost();        

        String url ="http://localhost:8080/cloud_server/nocheck/file/agree.do";

        //pic1,pic2,zip 上传文件 from 中的名称

        try {

        Map<String,String> map = new HashMap<String,String>();

        map.put("pic1", fileName);

        //map.put("pic2", fileName);

        //map.put("zip", fileName);

        //pickupId 文本框名称

        Map<String,String> param = new HashMap<String,String>();

        param.put("status", "4");

        param.put("pickupId", "104993");

        param.put("cause", "质量原因 ");

        param.put("connectname", "姓名信息");

        param.put("connectphone", "15902248763");

        param.put("details", "内容");

 

       

       

       

json.post(url, map,param);

} catch (IOException e) {

e.printStackTrace();

}

 

//{"resData":{"status":"1","message":"成功","data":null},"token":null}

}

 

 

private RequestConfig requestConfig = RequestConfig.custom()  

           .setSocketTimeout(15000)  

           .setConnectTimeout(15000)  

            .setConnectionRequestTimeout(15000)  

            .build();  

      

private final String charSet ="UTF-8"; 

public void post(String url,Map<String,String> fileList,Map<String,String> param) throws ClientProtocolException, IOException {

//HttpClient httpclient = new DefaultHttpClient();

 

CloseableHttpClient httpclient = HttpClients.createDefault();

 

HttpPost post = new HttpPost(url);

post.setConfig(requestConfig);

MultipartEntity entity = new MultipartEntity();

 

//上传文件

if(null != fileList){

Iterator<Entry<String, String>> item = fileList.entrySet().iterator();

while(item.hasNext()){

Entry<String,String> it = item.next();

FileBody fileBody = new FileBody(new File(it.getValue()));

entity.addPart(it.getKey(), fileBody);

}

}

 

//请求参数

if(null != param){

Iterator<Entry<String, String>> item = param.entrySet().iterator();

while(item.hasNext()){

Entry<String,String> it = item.next();

//编码格式utf-8

StringBody stringBody = new StringBody(it.getValue(),Charset.forName(charSet));

entity.addPart(it.getKey(), stringBody);

}

}

 

post.setEntity(entity);

 

CloseableHttpResponse  response = httpclient.execute(post);

if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {

HttpEntity entitys = response.getEntity();

if (entity != null) {

System.out.println(EntityUtils.toString(entitys,charSet));

}

}

 

//关闭

if(null !=response){

response.close();

}

 

if(null != httpclient){

httpclient.close();

}

}

 

//防止上传文件名称中文乱码

public void post436(String url,Map<String,String> fileList,Map<String,String> param) throws ClientProtocolException, IOException {

//httpclient 4.36 版本

CloseableHttpClient httpclient = HttpClients.createDefault();

 

HttpPost post = new HttpPost(url);

post.setConfig(requestConfig);

//MultipartEntity entity = new MultipartEntity(null,null,Charset.forName("UTF-8"));

 

MultipartEntityBuilder entity =  MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

 

//上传文件

if(null != fileList){

Iterator<Entry<String, String>> item = fileList.entrySet().iterator();

while(item.hasNext()){

Entry<String,String> it = item.next();

FileBody fileBody = new FileBody(new File(it.getValue()));//,"application/octet-stream","UTF-8"

 

entity.addPart(it.getKey(), fileBody);

}

}

 

//请求参数

if(null != param){

Iterator<Entry<String, String>> item = param.entrySet().iterator();

while(item.hasNext()){

Entry<String,String> it = item.next();

//编码格式utf-8

StringBody stringBody = new StringBody(it.getValue(), ContentType.create(

           "text/plain", Consts.UTF_8));

//new StringBody(it.getValue(),Charset.forName(charSet));

entity.addPart(it.getKey(), stringBody);

}

}

 

post.setEntity(entity.setCharset(CharsetUtils.get("UTF-8")).build());

 

CloseableHttpResponse  response = httpclient.execute(post);

if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {

HttpEntity entitys = response.getEntity();

if (entity != null) {

System.out.println(EntityUtils.toString(entitys,charSet));

}

}

 

//关闭

if(null !=response){

response.close();

}

 

if(null != httpclient){

httpclient.close();

}

}

 

}

 

相关标签: httpclient