base64格式压缩文件反向编译成pdf

/**
 * 浦发银行
 * @param base64Code 响应的pdf文件zip压缩后转为base64编码的字符串。 例子:ASDASDASDASDASDAS/DASDASDAS/DSADASD-DASD/....
 * @param targetPath 要保存的位置  c:/admin/aaa/aaa/ssssss.pdf
 * @param fileName   文件名字 xxxxx.pdf
 * @return
 * @throws Exception
 */
public Boolean toFile(String base64Code,String targetPath ,String fileName) throws Exception {

 // 这是把 base64格式数据转换成字节,然后再用流的形式读取出来压缩文件然后再保存到指定的位置 
    byte[] byts = java.util.Base64.getDecoder().decode(base64Code);
    try {
        ByteArrayInputStream byteArray = new ByteArrayInputStream(byts);
        ZipInputStream zipInput = new ZipInputStream(byteArray);
        ZipEntry entry = zipInput.getNextEntry();
        File fout = null;
        File file = new File(targetPath);
        String parent = file.getParent();
        while (entry != null && !entry.isDirectory()) {
            log.info("文件名称:    [{}]", entry.getName() );

   // 如果不需要改变解析出来的文件名字直接用该方法就行
         //   fout = new File(parent, entry.getName());

  // 如果需要改变解压出来文件的名字则需要自己手动拼写
            fout = new File(parent, fileName + ".pdf");
            if (!fout.exists()) {
                (new File(fout.getParent())).mkdirs();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fout));
            int offo = -1;
            byte[] buffer = new byte[1024];
            while ((offo = zipInput.read(buffer)) != -1) {
                bos.write(buffer, 0, offo);
            }
            bos.close();
            // 获取 下一个文件
            entry = zipInput.getNextEntry();
        }
        zipInput.close();
        byteArray.close();
    } catch (Exception e) {
        throw new RuntimeException("解压流出现异常", e);
    }
    return true;
}

posted on 2021-01-07 17:28  UnmatchedSelf  阅读(474)  评论(0编辑  收藏  举报

导航