如何将 FTP 从 Spring MVC 转换为 Spring Boot/Thymeleaf?

How to convert FTP from Spring MVC to Spring Boot/Thymeleaf?

所以我一直在努力从一个旧的 webapp 中获取逻辑并从中制作一个新的 Spring Boot 应用程序。关于 ftp 连接和呼叫,我遇到了困难。由于我没有这方面的经验,我很好奇是否有一种 better/more 现代方法可以使用 Spring Boot/Thymeleaf 来处理大多数 ftp 的东西以及继续进行设置的方法。任何 advice/guidance 都会很棒。

这是较旧的代码,我想对其进行一些现代化改造。

String serverName = getFtpServer();

// Connect to the server
try {
    ftp.connect(serverName);
    ftp.enterLocalPassiveMode();
    String replyText = ftp.getReplyString();
    System.out.println(replyText);
} catch (Exception e) {
    e.printStackTrace();
    return false;
}

// Login to the server
try {
    ftp.login(userName, password);
    String replyText = ftp.getReplyString();
    System.out.println(replyText);
} catch (Exception e) {
    e.printStackTrace();
    return false;
}

// Tell server that the file will have JCL records
try {
    ftp.site("filetype=jes");
    String replyText = ftp.getReplyString();
    System.out.println(replyText);
} catch (Exception e) {
    e.printStackTrace();
    return false;
}

// Submit and run the JCL
try {
    System.out.println("TRYING TO START MAINFRAME JCL");
    submitJcl(filename, serverName);

    String replyText = ftp.getReplyString();
    System.out.println(replyText);
} catch (Exception e) {
    String replyText = ftp.getReplyString();
    System.out.println(replyText);
    e.printStackTrace();
    return false;
}

// Quit the server
try {
    ftp.quit();
} catch (Exception e) {
    e.printStackTrace();
}

存储文件

private String submitJcl(String remoteFile, String serverName) throws IOException {
    String filePath = getFilePath();
    String result = "";
    String fileName = filePath + remoteFile;
    System.out.println("filePath = " + fileName);
    FileInputStream inputStream = new FileInputStream(fileName);
    ftp.storeFile(serverName, inputStream);

    return result;
}

为此,我发现可能有更好的方法可以将其更改为更新的 ftp 格式以用于 Spring 引导,但这仍然完全有效。

无论如何,我对它所做的更改:

  • 将 try/catch 个块合并为一个块。
  • 将 ftp 内容推入它自己的函数中,然后在 try/catch 块中调用它
  • 将所有 sys.out 更改为 info.debugs。
  • 将获取文件路径的方式更改为更多的相对路径,文件存储在系统中而不是用户文件中。