使用http调用返回流式文件,zip文件解压缩txt文件解析

 public static void main(String[] args) throws IOException {
        String api="http://xxxxx";
        String body="value";
        PostMethod postMethod = null;
        postMethod = new PostMethod(api) ;//添加请求头数据
        postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
        //参数设置,需要注意的就是里边不能传NULL,要传空字符串
        NameValuePair[] data = {
                new NameValuePair("key",body),
                // new NameValuePair("_ArgStr2","*")
        };
        postMethod.setRequestBody(data);

        org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
        int response = httpClient.executeMethod(postMethod); // 执行POST方法
        byte[] file =input2byte(postMethod.getResponseBodyAsStream());
        writeBytesToFile(file,"C:\\a.zip");
        System.out.println(file);
    }

    public static final byte[] input2byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

    public static File writeBytesToFile(byte[] b, String outputFile) {
        File file = null;
        FileOutputStream os = null;

        try {
            file = new File(outputFile);
            os = new FileOutputStream(file);
            os.write(b);
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            try {
                if(os != null) {
                    os.close();
                }
            } catch (IOException var12) {
                var12.printStackTrace();
            }

        }

        return file;
    }

``以上是通过http远程调用,接口返回的是流式文件,转成file并生成,下面是将上述生成的zip文件解压,里面是txt文件,并解析内容

public static List<String> unZip(File srcFile, String destDirPath) throws RuntimeException {
        long start = System.currentTimeMillis();

        // 判断源文件是否存在

        if (!srcFile.exists()) {
            throw new RuntimeException(srcFile.getPath() + "所指文件不存在");

        }

        // 开始解压

        ZipFile zipFile = null;
        List<String> paths=new ArrayList<>();

        try {
            zipFile = new ZipFile(srcFile);

            Enumeration<?> entries = zipFile.entries();

            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                paths.add(destDirPath+"\\"+entry.getName());

                System.out.println("解压" + entry.getName());

                // 如果是文件夹,就创建个文件夹

                if (entry.isDirectory()) {
                    System.out.println("是文件夹");
                    String dirPath = destDirPath + "/" + entry.getName();

                    File dir = new File(dirPath);

                    dir.mkdirs();

                } else {
                    // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                    System.out.println("是文件");
                    File targetFile = new File(destDirPath + "/" + entry.getName());

                    // 保证这个文件的父文件夹必须要存在

                    if(!targetFile.getParentFile().exists()){
                        targetFile.getParentFile().mkdirs();

                    }

                    targetFile.createNewFile();

                    // 将压缩文件内容写入到这个文件中

                    InputStream is = zipFile.getInputStream(entry);

                    FileOutputStream fos = new FileOutputStream(targetFile);

                    int len;

                    byte[] buf = new byte[BUFFER_SIZE];

                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);

                    }

                    // 关流顺序,先打开的后关闭

                    fos.close();

                    is.close();

                }

            }

            long end = System.currentTimeMillis();

            System.out.println("解压完成,耗时:" + (end - start) +" ms");

        } catch (Exception e) {
            throw new RuntimeException("unzip error from ZipUtils", e);

        } finally {
            if(zipFile != null){
                try {
                    zipFile.close();

                } catch (IOException e) {
                    e.printStackTrace();

                }

            }

        }
        return paths;

    }

上述是把zip文件解压到某个文件夹下,接下来是解析txt文件

 public static void readFile( File file) {
        BufferedReader reader = null;
        String tempString = null;
        try {
            int i = 0;
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8"); //避免中文乱码,gbk或者utf-8切换
            reader = new BufferedReader(read);
            while ((tempString = reader.readLine()) != null) {//按行读取
                System.out.println(tempString);
                String[] segments = tempString.split("\t");
                //System.out.println(segments[10]);
                //System.out.println(segments[11]);
                System.out.println(segments[0]);
                System.out.println(segments[14]);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();

                }
            }
        }

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值