我在springboot应用程序中有此方法,该应用程序在custom_users目录内生成3个CSV文件(与Employee,Customer和Building有关),并在其名称后附加了时间戳,如下所示。 以下代码对我来说很好用。
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();
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);
}
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
}
}
我的问题:
我试图以sucn方式修改CSVWriter
的构造函数,以使CSV数据作为ZipEntry对象直接进入ZIP存档。
因此,我在上面的代码中进行了更改(在下面的代码的ResultSetMetaData rsmd = rsDemo.getMetaData();
行之后可见)。现在,我只是想将一个文件放入ZIP存档进行测试,但我注意到以下错误:
第173行错误指出: 构造函数ZipEntry(Path)未定义。
第175行错误表示: 此行有多个标记(2个错误中有1个是未定义构造函数CSVWriter(ZipOutputStream))。
我应该如何处理ZipEntry和CSVWriter的构造函数部分?
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);
zos.putNextEntry(entry);
try (CSVWriter writer = new CSVWriter(zos)) {
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
}
}