get Pdf from oss when 302 从第三方接口获取文件流生成PDF并存储到本地

 package http;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.function.Function;

class PdfUtil {
    public void getPdffrom302(String url, String filePath, Function<String, String> changeUtil){
        try {
            System.out.println("访问地址:" + url);
//发送get请求
            URL serverUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
            conn.setRequestMethod("GET");
//必须设置false,否则会自动redirect到重定向后的地址
            conn.setInstanceFollowRedirects(false);
            conn.addRequestProperty("Accept-Charset", "UTF-8;");
            conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
            // conn.addRequestProperty("Referer", "http://matols.com/");
            conn.connect();
//判定是否会进行302重定向
            if (conn.getResponseCode() == 302) {

//如果会重定向,保存302重定向地址,以及Cookies,然后重新发送请求(模拟请求)
                String location = conn.getHeaderField("Location");
                location = changeUtil.apply(location);
                String cookies = conn.getHeaderField("Set-Cookie");
                serverUrl = new URL(location);
                conn = (HttpURLConnection) serverUrl.openConnection();
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Cookie", cookies);
                conn.addRequestProperty("Accept-Charset", "UTF-8;");
                conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
                //    conn.addRequestProperty("Referer", "http://matols.com/");
                conn.connect();
                System.out.println("跳转地址:" + location);
            }
//将返回的输入流转换成字符串
            InputStream is = conn.getInputStream();
            File f = new File(filePath);
            OutputStream os = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            os.flush();
            is.close();
            os.close();
// 释放资源
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        PdfUtil p= new PdfUtil();
        String url="http://www ";

        p.getPdffrom302(url,"1.pdf", new Function<String, String>() {
            public String apply(String location) {
                return location.replace("files.xxxx.com", "128.0.0.1");
            }
        });
    }


}

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值