PDF文件下载 信息获取

完成订单信息获取

同时下载订单快递pdf

同时获取pdf 文件内容

写一个 httpclient 请求类,完成模拟请求

public class TestUtil {
	// httpclient 请求
    private CloseableHttpClient httpClient;
   // 配置信息
    private RequestConfig config;
    public TestUtil() {
        config = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                // 信任所有证书
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext,
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {
                @Override
                public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
                    return 60 * 1000; // 20 seconds,because tomcat default keep-alive timeout is 20s
                }
            };
            ConnectionConfig connectionConfig = ConnectionConfig.custom()
                    .setBufferSize(4128)
                    .build();
            httpClient = HttpClients.custom().setSSLSocketFactory(sslFactory).setRetryHandler(new DefaultHttpRequestRetryHandler())
                    .setKeepAliveStrategy(connectionKeepAliveStrategy).setDefaultConnectionConfig(connectionConfig).build();
        } catch (Exception e) {
            e.printStackTrace();
            httpClient = HttpClients.createDefault();
        }
    }
    // form data 模拟请求
    public CloseableHttpResponse formPost(String url, List<NameValuePair> para) throws Exception {
        HttpPost httppost = new HttpPost(url);
        httppost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httppost.addHeader("Cookie", "PHPSESSID=7mo5dqteclrsjc0l9o223poop1; ");
        httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36");
        httppost.addHeader("X-Requested-With", "XMLHttpRequest");
        try {
            httppost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httppost.setConfig(config);
            httppost.setEntity(new UrlEncodedFormEntity(para, "utf-8"));
            CloseableHttpResponse res = httpClient.execute(httppost);
            return res;
        } catch (Exception e) {
            throw e;
        } finally {
        }
    }

完成订单信息获取

public static JSONArray getAllShopeeOrder(Integer pageNo) {
        TestUtil testUtil = new TestUtil();
        List<NameValuePair> para = new ArrayList<NameValuePair>();
        para.add(new BasicNameValuePair("OrderPlus.isNewOrder", "2"));
        para.add(new BasicNameValuePair("isshowordercombosku", "2"));
        para.add(new BasicNameValuePair("page", pageNo.toString()));
        para.add(new BasicNameValuePair("rowsPerPage", "500"));
        para.add(new BasicNameValuePair("Order_isCloud", "2"));
        para.add(new BasicNameValuePair("m", "order"));
        try {
            CloseableHttpResponse removeRes = testUtil.formPost("https://www.xxxxx.com/index.php?mod=order.oTc", para);
            HttpEntity entity = removeRes.getEntity();
            String result = EntityUtils.toString(entity, "utf-8");
            JSONArray orderDataArray = JSONObject.parseObject(result).getJSONArray("orderDataList");
            return orderDataArray;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
根据pdf 的URL下载pdf
public static String sendPdfToServer(String pdfUrl, String name) {
        String pdfSavePath = "";
        try {
            // new一个URL对象
            URL url = new URL(pdfUrl);
            // 打开链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置请求方式为"GET"
            conn.setRequestMethod("GET");
            // 超时响应时间为5秒
            conn.setConnectTimeout(20 * 1000);
            // 通过输入流获取图片数据
            InputStream inStream = conn.getInputStream();
            // 得到图片的二进制数据,以二进制封装得到数据,具有通用性
            byte[] data = readInputStream(inStream);
            pdfSavePath = "D://pdf/" + name + ".pdf";
            // new一个文件对象用来保存图片,默认保存当前工程根目录
            File imageFile = new File(pdfSavePath);
            // 创建输出流
            FileOutputStream outStream = new FileOutputStream(imageFile);
            // 写入数据
            outStream.write(data);
            // 关闭输出流
            outStream.close();
            if (!imageFile.exists() || !imageFile.isFile()) {
                System.out.println("PDF 生成失败");
                conn = (HttpURLConnection) url.openConnection();
                // 设置请求方式为"GET"
                conn.setRequestMethod("GET");
                // 超时响应时间为5秒
                conn.setConnectTimeout(20 * 1000);
                // 通过输入流获取图片数据
                InputStream inStreamRetry = conn.getInputStream();
                // 得到图片的二进制数据,以二进制封装得到数据,具有通用性
                byte[] dataRetry = readInputStream(inStreamRetry);
                // new一个文件对象用来保存图片,默认保存当前工程根目录
                File imageFileRetry = new File(pdfSavePath);
                // 创建输出流
                FileOutputStream outStreamimageFileRetry = new FileOutputStream(imageFileRetry);
                // 写入数据
                outStreamimageFileRetry.write(dataRetry);
                // 关闭输出流
                outStreamimageFileRetry.close();
            }
            return pdfSavePath;
            // 服务器文件路径
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 读取文件流
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        // 创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        // 每次读取的字符串长度,如果为-1,代表全部读取完毕
        int len = 0;
        // 使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            // 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        // 关闭输入流
        inStream.close();
        // 把outStream里的数据写入内存
        return outStream.toByteArray();
    }
导入pdf 检测的包 更新maven
 <dependency>
    <groupId>org.apache.pdfbox</groupId>
     <artifactId>pdfbox</artifactId>
     <version>2.0.23</version>
 </dependency>
利用对应的API 完成pdf的读取
public static String scanPdf(String pdfUrl) throws IOException {
     String info = "";
     try {
         File file = new File(pdfUrl);
         //multipartFile为multipartFile文件类型,将文件转化为文件流被PDDocument加载
         PDDocument document = PDDocument.load(file);
         document.getClass();
         //使用PDFTextStripper 工具
         PDFTextStripper tStripper = new PDFTextStripper();
         //设置文本排序,有规则输出
         tStripper.setSortByPosition(true);
         info =  tStripper.getText(document);
         //获取所有文字信息
     } catch (Exception e) {
         e.printStackTrace();
     }
     return info;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值