使用HttpClient发送HttpPost请求包含上传本地图片和远程图片的传输实现

 

在实际项目中需要在当前系统中模拟浏览器发送一个post请求,正常情况下传文字没多大问题,但是如果带上传文件功能的话,

网上的资料不太好找,好在经过我多方寻找,加上自由发挥,真让我搞出来了。

 

下面代码为核心代码,

可以上传 

File对象,

转换成byte数组类型(String类型Base64编码的信息的图片)

String类型 的数据,

满足了,发送post请求大部分上传需要,每一步都有详细说明,并且都有log打出。希望能帮到需要的人。微笑

项目代码在下面,实现作为参考

项目代码地址

 https://github.com/kioo/uploaddemo

public class HttpTest {

    public String start(String path,String base64String,String imageFilePath) throws ClientProtocolException, IOException{
        // 1. 创建上传需要的元素类型
        // 1.1 装载本地上传图片的文件
        File imageFile = new File(imageFilePath);
        FileBody imageFileBody = new FileBody(imageFile);
        // 1.2 装载经过base64编码的图片的数据
        String imageBase64Data = base64String;
        ByteArrayBody byteArrayBody = null;
        if(StringUtils.isNotEmpty(imageBase64Data)){
            byte[] byteImage = Base64.decodeBase64(imageBase64Data);
            byteArrayBody = new ByteArrayBody(byteImage,"image_name");
        }
        // 1.3 装载上传字符串的对象
        StringBody name = new StringBody("admin",ContentType.TEXT_PLAIN);
        System.out.println("装载数据完成");
        // 2. 将所有需要上传元素打包成HttpEntity对象
        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("name", name)
                .addPart("file1",imageFileBody)
                .addPart("file2",byteArrayBody).build();
        System.out.println("打包数据完成");
        // 3. 创建HttpPost对象,用于包含信息发送post消息
        HttpPost httpPost = new HttpPost(path);
        httpPost.setEntity(reqEntity);
        System.out.println("创建post请求并装载好打包数据");
        // 4. 创建HttpClient对象,传入httpPost执行发送网络请求的动作
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        System.out.println("发送post请求并获取结果");
        // 5. 获取返回的实体内容对象并解析内容
        HttpEntity resultEntity = response.getEntity();
        String responseMessage = "";
        try{
            System.out.println("开始解析结果");
            if(resultEntity!=null){
                InputStream is = resultEntity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuffer sb = new StringBuffer();
                String line = "";
                while((line = br.readLine()) != null){
                    sb.append(line);
                }
                responseMessage = sb.toString();
                System.out.println("解析完成,解析内容为"+ responseMessage);
            }
            EntityUtils.consume(resultEntity);
        }finally{
            if (null != response){
                response.close();
            }
        }
        return responseMessage;
    }

    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpTest ht = new HttpTest();
        ht.start("http//127.0.0.1:8080/test/uplod/upload.do","dfsdfsdfsdf","C:\\Intel\\test.jpg");
    }

}

 

 

 

 

 

  • 9
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值