对账单文件下载都可以用到 微信对账单 GZIP 文件返回 支付宝,银联 返回下载链接 放入下载都可以

本文介绍了一种通过Java实现的对账单文件下载方法,包括使用POST请求发送数据、处理gzip压缩文件及将文件保存至本地的过程。文章详细展示了如何设置HTTP请求参数,如请求方法、超时时间、请求头等,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对账单文件下载  GTE,POST都可以注意参数区分

注意微信下载是没有格式的文件注意文件改成txt格式或者直接用notepad++打开

   public static void main(String[] arg) {
        
        downloadFile(wxUrl, "D:/test/WX/****.gzip", reuqestXml, "POST");

    }

/**
     * 
     * @param urlPath 请求地址
     * @param saveUrl 保存文件地址
     * @param data 请求参数
     * @param status 请求类型 post get
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static void downloadFile(String urlPath, String saveUrl, String data,String status) {
        // 设置访问地址
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        InputStream inputStream = null;
        FileOutputStream fos = null;
        try {
            url = new URL(urlPath);
            // http的连接类 得到网络访问对象
            httpURLConnection = (HttpURLConnection) url.openConnection();

            // 设置请求参数(访问方式,超时时间,输入,输出流,请求头信息),以流的形式进行连接
            // 设定请求的方法,默认是GET
            httpURLConnection.setRequestMethod(status);
            // 超时时间
            httpURLConnection.setConnectTimeout(5000);
            // 设置是否向HttpURLConnection输出
            httpURLConnection.setDoOutput(true);
            // 设置是否从httpUrlConnection读入
            httpURLConnection.setDoInput(true);
            // 设置使用编码格式参数的名-值对
            httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            httpURLConnection.setRequestProperty("Charsert", "UTF-8");
            // 简历连接
            httpURLConnection.connect();
            if(data!=null&&data!=""){
                 // 写入参数到请求中
                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 = 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("创建失败");
                        }
                    }

                    byte[] temp = new byte[1024];
                    int b;
                    fos = new FileOutputStream(file);
                    while ((b = inputStream.read(temp)) != -1) {
                        fos.write(temp, 0, b);
                        fos.flush();
                    }



                }
            }

            // 关闭 http连接,释放资源
            httpURLConnection.disconnect();
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            //最终都执行关流
            try {
                if (inputStream != null)
                    inputStream.close();
                if (fos != null)
                    fos.close();
                if (httpURLConnection != null)
                    httpURLConnection.disconnect();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值