悠然哈哈哈

导航

访问远程zip并解析csv

public List<Info> exportsCode(String orderNo) {

       
        List<Info> infoResponses = new ArrayList<Info>();

        String token = queryToken();
        if (StringUtils.isBlank(token)) {
            
            return exportsCode(orderNo);
        }

        String urlStr ="";
try {
            // 创建URL对象[请求路径]
            URL url = new URL(urlStr);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("GET");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            String authString = "Bearer " + token;
            connection.setRequestProperty("Authorization", authString);
            connection.addRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            // 连接
            connection.connect();
            // 发送请求
            int responseCode = connection.getResponseCode();
          
            if (responseCode != 200) {
                
                return exportsCode(orderNo);
            }
            InputStream inputStream = connection.getInputStream();

            ZipInputStream zin = new ZipInputStream(inputStream);
            BufferedInputStream bs = new BufferedInputStream(zin);
            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null) {
                if (!ze.isDirectory()) {
                    //文件读取处理
                    byte[] buffer = new byte[1024];
                    int len;
                    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                    while ((len = zin.read(buffer)) != -1) {
                        byteStream.write(buffer, 0, len);
                    }
                    // 关闭流
                    byteStream.close();

                    InputStream byteArrayInputStream = new ByteArrayInputStream(byteStream.toByteArray());
                    InputStreamReader in = new InputStreamReader(byteArrayInputStream, "gbk");
                    BufferedReader bufferedReader = new BufferedReader(in);
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        String[] split = line.split(",");
                        String co = splitResult(split[0]);
                        String barCode = splitResult(split[1]);
                        if ("平文".equals(co)) {
                            continue;
                        }
                        Info response = new Info();
                        response.setCo(co.trim());
                        response.setBarCode(barCode.trim());
                        infoResponses.add(response);
                    }
                    bufferedReader.close();
                    in.close();
                }
            }
            zin.close();
            bs.close();
            inputStream.close();
        } catch (Exception e) {
            logger.error(orderNo + "异常:{}", e);
        }
        return infoResponses;
    }
    private String splitResult(String once) {

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < once.length(); i++) {
            if (once.charAt(i) != '"') {
                stringBuilder.append(once.charAt(i));
            }
        }
        return stringBuilder.toString();
    }

参考:https://blog.csdn.net/web15085599741/article/details/123574480

https://blog.csdn.net/weixin_39655220/article/details/93058079

posted on 2023-10-07 10:40  悠然886  阅读(8)  评论(0编辑  收藏  举报