原始jdbc连接数据读取字符串生成zip文件并读成byte数组

发布时间:2022-08-18 18:23

代码如下:
本次数据库为sqlserve

 try {
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=cs", "sa", "Zmy123456.");
          String sql = "select ATFile,ATFileName from AT_AttachmentFile where ATGuid = 'A1:05c5bf44-2323-48ec-8f23-0f6623ee7fcd'";

          PreparedStatement ps =connection.prepareStatement(sql);
          ResultSet rs = ps.executeQuery();
          ResultSetMetaData rsmd = rs.getMetaData();
          Map mapMetaData = new HashMap<>();
          ZipFile zipFile = null;
          Stream signStream = null;
          Stream zipStream = null;
          while (rs.next()) {
              String columnLabel = rsmd.getColumnLabel(1);
              Blob columnValue = rs.getBlob(columnLabel);
              InputStream ins= columnValue.getBinaryStream();
              File file = new File("D:\\","cs.zip");
              org.apache.commons.io.FileUtils.copyInputStreamToFile(ins, file);
              zipFile = new ZipFile(file, Charset.defaultCharset());
              signStream = zipFile.stream();
              zipStream = zipFile.stream();
              //断言
              Predicate signTxt = ze -> ze.getName().contains("sign,txt");
              Predicate zipTxt = ze -> ze.getName().endsWith(".zip");
              //,过滤
              Optional signInfo = (Optional) signStream.filter(signTxt).findFirst();
              Optional zipInfo = (Optional) zipStream.filter(zipTxt).findFirst();
              ins.close();
              zipFile.close();
              //获取文件名称
              String fileName = rsmd.getColumnLabel(2);
              Object fileNames = rs.getObject(fileName);
              String zipDir = "D:\\";
              //解压zip文件
              String name = ZipUtils.unzip(file.getPath(),zipDir);
              file.delete();
              //读成byte数组返回
              byte[]  bytes = FileUtil.readFile2Bytes(new File(zipDir+File.separator+name),true);
              
              System.out.println(bytes);

          }



          //将字符串写入到zip中如下
//            File file = new File("D:\\","cs.zip");
//            java.util.zip.ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
//            mapMetaData.forEach((k,v)->{
//                try {
//                    zipOutputStream.putNextEntry(new ZipEntry("15.pdf"));
//                    IoUtil.write(zipOutputStream,false, (byte[]) v);
//                    zipOutputStream.flush();
//                    zipOutputStream.closeEntry();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            });
      } catch (Exception e) {
          e.printStackTrace();
      }

解压文件方法unzip

//解压zip文件
    public static String unzip(String filePath,String zipDir){
        String name = null;
        BufferedOutputStream des = null;
        BufferedInputStream is =null;
        ZipFile zipFile = null;
        try {
            ZipEntry entry;
            zipFile = new ZipFile(filePath);
            Enumeration dir = zipFile.entries();
            while (dir.hasMoreElements()){
                entry = (ZipEntry) dir.nextElement();
                if (entry != null){
                    name = entry.getName();
                }
            }
            Enumeration e = zipFile.entries();
            while (e.hasMoreElements()){
                entry = (ZipEntry) e.nextElement();
                if (entry.isDirectory()){
                    continue;
                }else {
                    is = new BufferedInputStream(zipFile.getInputStream(entry));
                    int count;
                    byte[] bytes = new byte[2048];
                    FileOutputStream fos = new FileOutputStream(zipDir+entry.getName());
                    des = new BufferedOutputStream(fos,2048);
                    while ((count = is.read(bytes,0,2048)) !=-1){
                        des.write(bytes,0,count);
                    }

                }
            }

            return name;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                zipFile.close();
                des.flush();
                des.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
       return name;
    };

读成byte数组

 public static byte[] readFile2Bytes(File file, boolean isDelete) throws IOException {
        InputStream inputStream = new FileInputStream(file);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            int len = 0;
            byte[] b = new byte[10240];
            while ((len = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, len);
            }
            byte[] byteArray = outputStream.toByteArray();
            return byteArray;
        } finally {
            IOUtils.close(inputStream);
            IOUtils.close(outputStream);
            if (isDelete) {
                file.delete();
            }
        }
    }

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号