获取post请求中的文件流

获取post请求中的文件流

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
    String uri = "http://xxxx.xx.xx.xx:xxxx/materialManageHB.do?method=downFileHB";
    HttpPost httppost = new HttpPost(uri);
    //设置请求参数
    String params = "xxxxxxxxxxxxxxx";
    httppost.setEntity(new StringEntity(params, ContentType.create("application/x-www-form-urlencoded")));
    //执行请求
    CloseableHttpResponse response = httpclient.execute(httppost);
    try {
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
        	//获取返回的输入流
            InputStream inputStream = resEntity.getContent();
            File file = new File("c:/d/dd.pdf");
            if (file.exists()) {
                file.delete();
            }
            byte[] bytes = IOUtils.toByteArray(inputStream);
            FileUtils.writeByteArrayToFile(file, bytes);
        }
        EntityUtils.consume(resEntity);
    } finally {
        response.close();
    }
} finally {
    httpclient.close();
} 

直接通过url获取文件流

public static void main(String args[]) throws IOException {
     String url1 = "http://xxxx.xx.xx.xx:xxxx/temp/5be6a4e0-a10d-45b2-be39-865075a62337.pdf";
     URL url = new URL(url1);
     InputStream is = url.openStream();
     OutputStream os = new FileOutputStream("D:/111.pdf");
     byte bf[] = new byte[1024];
     int length = 0;
     while ((length = is.read(bf, 0, 1024)) != -1) {
        os.write(bf, 0, length);
     }
     is.close();
     os.close();
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值