
本教程介绍了如何在Java Struts框架中,无需在本地创建实际文件,直接基于ArrayList数据动态生成CSV文件,并通过ByteArrayOutputStream和ByteArrayInputStream将其转换为InputStream,最终上传至FTP服务器。这种方法避免了磁盘I/O操作,提高了效率,并简化了文件处理流程。
传统的做法是先在本地磁盘创建一个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,Position\n");
// 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
}
}代码解释:
有了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;
}注意事项:
将以上代码集成到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。
以上就是在Java Struts中动态创建CSV文件并上传至FTP服务器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号