我的 springboot 应用程序中有这个方法,它在 custom_users 目录中生成 3 个 CSV 文件(与员工、客户和建筑物相关),其名称后附加了时间戳,如下所示。
目前,它仅生成两个 CSV 文件(公司和建筑相关),因为我正在测试 employee_custom_file
的 zip 文件相关转换,如下所示。
有人能告诉我在使用CSVWriter
(来自opencsv
)将员工相关内容转换为zip文件时做错了什么吗?我原本期望 1 个 zip 文件与另外 2 个 CSV 文件一起显示,但由于某种原因,只生成 2 个 CSV 文件。
基本上,下面代码中的这部分没有按我的预期工作:
ZipEntry entry = new ZipEntry(file.getFileName().toString());
zos.putNextEntry(entry);
try {
CSVWriter writer = new CSVWriter( new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(rsDemo, true);
}
================================================== =====================
public void sendMessage(String msg) throws DaoException {
DataSource ds = null;
Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement pstmtEmployee = null;
PreparedStatement pstmtCompany = null;
PreparedStatement pstmBuilding = null;
ResultSet rs = null;
ResultSet rsDemo = null;
ResultSet rsCompany = null;
ResultSet rsBuildings = null;
String[] parts = msg.split("#");
String requestID = parts[0].trim();
String userName = parts[1].trim();
String applicationName = parts[2].trim();
logger.info("Request ID "+requestID);
logger.info("User Name "+userName);
logger.info("Application Name "+applicationName);
try {
ds = jdbcTemplate.getDataSource();
conn = ds.getConnection();
/*===========================================================================*/
/* Code to generate a employee CSV file */
/*===========================================================================*/
pstmtEmployee = conn.prepareStatement(getPatientEmployeeSQL);
pstmtEmployee.setString(1, requestID);
rsDemo = pstmtEmployee.executeQuery();
ResultSetMetaData rsmd = rsDemo.getMetaData();
FileOutputStream fos = new FileOutputStream("your_files.zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);
Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);
Path file = dir.resolve("employee_custom_file" + unixTimestamp + ".csv");
/*try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(file))) {
writer.writeAll(rsDemo, true);
}*/
ZipEntry entry = new ZipEntry(file.getFileName().toString());
zos.putNextEntry(entry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(rsDemo, true);
}
logger.info("Employee File Generated");
/*===========================================================================*/
/* Code to generate a company CSV file */
/*===========================================================================*/
pstmtCompany = conn.prepareStatement(getCompanySQL);
pstmtCompany.setString(1, requestID);
rsCompany = pstmtCompany.executeQuery();
ResultSetMetaData rsmdFacts = rsCompany.getMetaData();
Path filecompany = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(filecompany))) {
writer.writeAll(rsCompany, true);
}
logger.info("Company CSV File Generated");
/*===========================================================================*/
/* Code to generate a building CSV file */
/*===========================================================================*/
pstmBuilding = conn.prepareStatement(getBuildingSQL);
pstmBuilding.setString(1, requestID);
rsBuildings = pstmBuilding.executeQuery();
ResultSetMetaData rsmdBuildings = rsBuildings.getMetaData();
Path fileBuildings = dir.resolve("building_custom_file_" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(fileBuildings))) {
writer.writeAll(rsBuildings, true);
}
logger.info("Buildings CSV File Generated");
}
catch(Throwable th) {
throw new DaoException(th.getMessage(), th);
}
finally {
//resource Closing statements
}
}
最佳答案
您是否调用了代码示例中省略的 closeEntry
?
请注意,对 zip 输出流的写入是针对支持输出流的任何内容。 “写入”条目会写入 zip 流,而不是磁盘,除非输出流是这样设置的。
来自ZipOutputStream
:
/**
* Closes the current ZIP entry and positions the stream for writing
* the next entry.
* @exception ZipException if a ZIP format error has occurred
* @exception IOException if an I/O error has occurred
*/
public void closeEntry() throws IOException {
关于java - 未生成 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56583127/