【问题标题】:Blob Data files in a zip file using java code使用 Java 代码的 zip 文件中的 Blob 数据文件
【发布时间】:2020-03-14 07:46:23
【问题描述】:

我正在尝试在 zip 文件中添加 blob 数据。但是文件在添加到 zip 文件时会损坏。下面的代码执行,但不压缩文件:

public class BlobDataExtract {
    static ZipOutputStream zos = null;
    private static ZipOutputStream zosFile;

    private static ZipOutputStream zipExtract() throws ClassNotFoundException, SQLException, IOException {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection(url, "user", "password");
        String sql = "SELECT ORIG_NM,DOC_EXT_NM,DOC_INDX_NB,DOC_BO FROM zgdt099_document";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        java.sql.Blob docBlob = null;
        DocumentTO dto = null;
        List<DocumentTO> test = new ArrayList<DocumentTO>();
        while (rs.next()) {
            dto = new DocumentTO();
            dto.setDocIndxNb(new Long(rs.getLong(AmpoDBConstants.DOC_INDX_NB)));
            dto.setOrigNm(rs.getString(AmpoDBConstants.D_ORIG_NM));
            dto.setDocExtNm(rs.getString(AmpoDBConstants.D_DOC_EXT_NM));
            docBlob = rs.getBlob(AmpoDBConstants.D_DOC_BO);
            // String filepathzipped =rs.getString(AmpoDBConstants.D_ORIG_NM) + ".zip";
            InputStream blobStream = docBlob.getBinaryStream();
            byte[] newFile = new byte[(int) docBlob.length()];
            blobStream.read(newFile);
            blobStream.close();
            String contentType = "";
            String extName = dto.getDocExtNm();
            if ("pdf".equalsIgnoreCase(extName))
                contentType = "application/pdf";
            else if ("html".equalsIgnoreCase(extName) || "htm".equalsIgnoreCase(extName)
                    || "stm".equalsIgnoreCase(extName) || "jpeg".equalsIgnoreCase(extName)
                    || "jpg".equalsIgnoreCase(extName) || "bmp".equalsIgnoreCase(extName)
                    || "gif".equalsIgnoreCase(extName))
                contentType = "text/html";
            else if ("xls".equalsIgnoreCase(extName) || "xla".equalsIgnoreCase(extName)
                    || "xlc".equalsIgnoreCase(extName) || "xlm".equalsIgnoreCase(extName)
                    || "xlw".equalsIgnoreCase(extName) || "csv".equalsIgnoreCase(extName)
                    || "xlt".equalsIgnoreCase(extName))
                contentType = "application/vnd.ms-excel";
            else if ("doc".equalsIgnoreCase(extName) || "rtf".equalsIgnoreCase(extName)
                    || "rtx".equalsIgnoreCase(extName))
                contentType = "application/msword";
            else if ("ppt".equalsIgnoreCase(extName) || "pps".equalsIgnoreCase(extName))
                contentType = "application/vnd.ms-powerpoint";
            else if ("mpp".equalsIgnoreCase(extName))
                contentType = "application/vnd.ms-project";
            else if ("txt".equalsIgnoreCase(extName))
                contentType = "text/plain";
            else if ("zip".equalsIgnoreCase(extName))
                contentType = "application/zip";
            else if ("ics".equalsIgnoreCase(extName))
                contentType = "text/calendar";
            else if ("snp".equalsIgnoreCase(extName))
                contentType = "application/octet-stream";
            else
                contentType = "text/html";
            FileContent fileCont = new FileContent(dto.getOrigNm(), newFile, contentType);
            System.out.println("fileCont-->" + fileCont);
            dto.setDocBO(fileCont);
            test.add(dto);
            try {
                File file = new File(filePath);
                FileOutputStream fos = new FileOutputStream("C:/Users/user/Desktop/test.zip");
                zos = new ZipOutputStream(fos);
                zos.putNextEntry(new ZipEntry(dto.getDocBO().getFullName()));
                byte[] bytes = Files.readAllBytes(Paths.get(filePath));
                zos.write(bytes, 0, bytes.length);
            } catch (FileNotFoundException ex) {
                System.err.println("A file does not exist: " + ex);
            } catch (IOException ex) {
                System.err.println("I/O error: " + ex);
            }
            zos.closeEntry();
            zos.close();
        }
        return zos;
    }
}

请帮忙修改这段代码

【问题讨论】:

  • “请帮忙修改此代码” 抱歉,我什至无法阅读该代码。也许如果您格式化代码以实现人类可读性,它可能是可行的。
  • 为什么要读取表中的所有记录,然后将每个返回的 blob 写入同一个 test.zip 文件,所以只有最后一条记录在文件中?
  • 为什么要将整个 Blob 读入内存,然后将其写入文件以再次读取?
  • contentType 的逻辑是怎么回事? Zip 文件条目没有内容类型,因此您不会将其用于任何用途。都是浪费代码?
  • 如果您想将整个 BLOB 放在 byte[] 中,请使用 getBytes(),而不是 getBlob()。 JDBC 驱动程序将为您将Blob 转换为byte[]。并使用 try-with-resources 正确关闭......好吧,一切!

标签: java file zip blob


【解决方案1】:

您的代码似乎过于复杂。它可以简化为:

public class BlobDataExtract {
    private static void zipExtract() throws SQLException, IOException {
        String sql = "SELECT ORIG_NM,DOC_EXT_NM,DOC_INDX_NB,DOC_BO FROM zgdt099_document";
        try (
            Connection conn = DriverManager.getConnection("url", "user", "password");
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/Users/user/Desktop/test.zip"));
        ) {
            while (rs.next()) {
                zos.putNextEntry(new ZipEntry(rs.getString(AmpoDBConstants.D_ORIG_NM)));
                zos.write(rs.getBytes(AmpoDBConstants.D_DOC_BO));
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    问题已通过以下代码解决,

    public class BlobDataExtract {
         static ZipOutputStream zos =null;
          static String url = "jdbc:oracle:thin:@hostname:1521:SID";    
          public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection conn = DriverManager.getConnection(url, "user", "password");     
                String sql="select Blob_Data,ORIG_NM from table";           
                PreparedStatement stmt = conn.prepareStatement(sql);
                ResultSet rs = stmt.executeQuery();       
                byte[] docBlob = null;
                String filename = null;         
                FileOutputStream fos = new FileOutputStream("C:/Users/test.zip");     
                      zos = new ZipOutputStream(fos);
                      while (rs.next()) {               
                            docBlob = rs.getBytes("Blob_Data");   
                            filename = rs.getString("ORIG_NM");
                                try {
                                    zos.putNextEntry(new ZipEntry(filename));
                                     zos.write(docBlob, 0, Blob_Data.length);    
                                }
                                catch (FileNotFoundException ex) {
                                     System.err.println("A file does not exist: " + ex);
                                 } catch (IOException ex) {
                                     System.err.println("I/O error: " + ex);
                                 }
                                zos.closeEntry();
                      }            
    
              }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多