对账单文件下载都可以用到 微信对账单 GZIP 文件返回

  • 在看到这个文章之前很多同学都是在用HttpClient 请求操作的,这里就进入了一个误区,也不是说用HttpClient不行,我也把文件下载下来了,但是内容中多出了很多乱码,不知道是什么东西,如果你能用HttpClient请求下载成功无乱码欢迎分享经验.
  • 这里最终是用HttpURLConnection 建立TCP连接  与服务器进行交互的.下面我就贴出代码
      /**
         * 
         * @param urlPath 请求地址
         * @param saveUrl 保存文件地址
         * @param data 请求参数
         * @return
         * @throws IOException
         * @throws Exception
         */
        public static void downFile(String urlPath, String saveUrl, String data) throws IOException {
            // 设置访问地址
            URL url = new URL(urlPath);
            // 连接类的父类,抽象类
            URLConnection urlConnection = url.openConnection();
            // http的连接类 得到网络访问对象
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
    
            // 设置请求参数(访问方式,超时时间,输入,输出流,请求头信息),以流的形式进行连接
            // 设定请求的方法,默认是GET
            httpURLConnection.setRequestMethod("POST");
            // 超时时间
            httpURLConnection.setConnectTimeout(3000);
            // 设置是否向HttpURLConnection输出
            httpURLConnection.setDoOutput(true);
            // 设置是否从httpUrlConnection读入
            httpURLConnection.setDoInput(true);
            // 设置使用编码格式参数的名-值对
            httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            // 简历连接
            httpURLConnection.connect();
            // 写入参数到请求中
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"));
            // 传递参数
            writer.write(data.toString());
            writer.flush();
            writer.close();
    
            // 获取响应code 200成功
            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();
                // 判断字节大小
                if (inputStream.available() != 0) {
                    System.out.println("结果大小:" + inputStream.available());
                    File file = new File(saveUrl);
                    if (!file.getParentFile().exists()) {
                        boolean result = file.getParentFile().mkdirs();
                        if (!result) {
                            System.out.println("创建失败");
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    int size = 0;
                    int len = 0;
                    byte[] buf = new byte[1024];
                    while ((size = inputStream.read(buf)) != -1) {
                        len += size;
                        out.write(buf, 0, size);
                    }
                    System.out.println("最终写入字节数大小:" + len);
    
                    inputStream.close();
                    out.close();
    
                }
            }
    
            // 关闭 http连接,释放资源
            httpURLConnection.disconnect();
        }

     

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值