我在springboot应用程序中有此方法,该应用程序在custom_users目录内生成3个CSV文件(与Employee,Customer和Building有关),并在其名称后附加了时间戳,如下所示。
现在,它仅生成两个CSV文件(与公司和建筑物相关),因为我正在测试employee_custom_file
的与zip文件相关的转换,如下所示。
有人能告诉我使用CSVWriter
(来自opencsv
)将与员工相关的内容转换为zip文件时我在做什么吗?我原本希望一个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
}
}
答案 0 :(得分:0)
您是否打了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 {
答案 1 :(得分:0)
我知道了。我这行代码FileOutputStream fos = new FileOutputStream("your_files.zip");
造成了问题,因为我没有在任何地方使用dir
变量。因此,我必须像这样使用它才能使其工作:
OutputStream fos = Files.newOutputStream(dir.resolve("your_files.zip"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);