java - 使用 Java 代码的 zip 文件中的 Blob 数据文件
问题描述
我正在尝试在 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;
}
}
请帮助修改此代码
解决方案
您的代码似乎过于复杂。它可以简化为:
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));
}
}
}
}
推荐阅读
- php - 在 php 中查询字符串闭包
- class - kotlin.reflect.KClass.isInstance(value: Any?) 不工作
- java - Java同时处理多个请求
- docker - 为什么删除未请求的包(docker build)?
- intellij-idea - Intellij 创建测试缺少 JUnit
- c# - asp net core: [FromRoute] 和模型绑定
- python - 使用 Fabric 运行本地命令
- c# - Mongodb 集合为动态
- javascript - 在 NPM 安装上触发准备
- sql - 无法将 plpgsql 中的自定义类型对象设置为 null。相反,该对象的字段变为空