本教程介绍了如何在Java struts框架中,无需在本地创建实际文件,直接基于ArrayList数据动态生成csv文件,并通过ByteArrayOutputStream和ByteArrayInputStream将其转换为InputStream,最终上传至FTP服务器。这种方法避免了磁盘I/O操作,提高了效率,并简化了文件处理流程。
动态生成CSV数据流
传统的做法是先在本地磁盘创建一个CSV文件,然后将数据写入该文件,最后再读取该文件并上传到FTP服务器。这种方法存在一些缺点,例如需要进行磁盘I/O操作,效率较低,并且需要在服务器上维护临时文件。
一个更高效的方法是直接在内存中生成CSV数据,然后将其转换为InputStream,再上传到FTP服务器。这样可以避免磁盘I/O操作,提高效率,并且不需要在服务器上维护临时文件。
以下代码展示了如何使用ByteArrayOutputStream和ByteArrayInputStream来实现这一目标:
立即学习“Java免费学习笔记(深入)”;
import java.io.*; import java.util.ArrayList; import java.util.List; public class CSVGenerator { public static InputStream generateCSVStream(String[] business, String[] position) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (Writer writer = new OutputStreamWriter(bos)) { // CSV Header (Optional) writer.write("Business,Positionn"); // CSV Data for (int i = 0; i < Math.max(business.length, position.length); i++) { String businessValue = (i < business.length) ? business[i] : ""; String positionValue = (i < position.length) ? position[i] : ""; writer.write(businessValue + "," + positionValue + "n"); } } return new ByteArrayInputStream(bos.toByteArray()); } public static void main(String[] args) throws IOException { String[] business = {"Business1", "Business2"}; String[] position = {"Position1", "Position2", "Position3"}; InputStream inputStream = generateCSVStream(business, position); // Example: Print the stream content (for testing) try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } // At this point, you would pass the inputStream to your FTP upload method } }
代码解释:
- ByteArrayOutputStream: ByteArrayOutputStream 用于在内存中创建一个可增长的字节数组,所有写入到 OutputStreamWriter 的数据都会被写入到这个字节数组中。
- OutputStreamWriter: OutputStreamWriter 是一个字符流,它将字符转换为字节,并将其写入到 ByteArrayOutputStream 中。 这里使用它来方便地写入字符串数据。
- 写入CSV数据: 将业务数据和职位数据写入到 OutputStreamWriter 中,并使用逗号分隔。 注意这里添加了一个CSV Header作为示例。
- ByteArrayInputStream: ByteArrayInputStream 从 ByteArrayOutputStream 中读取数据,将其转换为 InputStream。
- 返回InputStream: 返回生成的 InputStream,供FTP上传方法使用。
将InputStream上传到FTP服务器
有了InputStream之后,就可以将其传递给FTP上传方法。以下是一个示例FTP上传方法的代码片段:
private boolean fileUpload(InputStream isUploadFile, String dirName, String loggedInUser, String fileName){ boolean storeRetVal = false; String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); storeRetVal = SISSFTPManager.getInstance().put(isUploadFile, dirName, fileName); if (storeRetVal) { try { if (fileType.trim().equalsIgnoreCase("csv")){ ICSAPI.getInstance().getSIMSOrderManager().createFileAudit(loggedInUser, fileName); } else { } } catch (RemoteException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } catch (ApplicationException e) { e.printStackTrace(); } logger.info("BulkUploadAction:fileUpload SFTP Transfer file successfully!"); } else { logger.error("BulkUploadAction:fileUpload SFTP Transfer file FAILED!"); } return storeRetVal; }
注意事项:
- 确保SISSFTPManager.getInstance().put()方法能够正确处理InputStream。
- 在上传完成后,不要忘记关闭InputStream,以释放资源。 虽然 ByteArrayInputStream 内部基于内存,没有文件句柄,但是良好的编程习惯仍然建议关闭它。 在上面的 generateCSVStream 方法中,try-with-resources 语句已经自动处理了 writer 的关闭。
Struts Action中的应用
将以上代码集成到Struts Action中,可以简化代码如下:
public ActionForward generatePayroll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { SessionInfoForm _form = (SessionInfoForm) form; SisTransactionsSession _config = _form.getSisTransactionsSession(); String loggedInUser = _form.getLoggedinEmployeeDVO().getLoginId().toUpperCase(); String[] business = request.getParameterValues("selectedBusinessValues"); String[] position = request.getParameterValues("selectedPositionValues"); String fileName = "PAYROLL_PRM.csv"; //Define file name InputStream isFixedValue = null; try { isFixedValue = CSVGenerator.generateCSVStream(business, position); fileUpload(isFixedValue, "your_directory", loggedInUser, fileName); //Replace "your_directory" } catch (IOException e) { e.printStackTrace(); } finally { if (isFixedValue != null) { try { isFixedValue.close(); } catch (IOException e) { e.printStackTrace(); } } } return mapping.findForward("success"); }
总结:
使用ByteArrayOutputStream和ByteArrayInputStream可以有效地在Java Struts中动态生成CSV文件并上传到FTP服务器,而无需创建实际的本地文件。 这种方法提高了效率,简化了代码,并减少了服务器上的文件管理负担。 记得处理异常,并确保正确关闭InputStream。