java FTP推送文件

该博客介绍了一段Java代码,通过JSch库实现SFTP文件上传。在尝试使用FTPClient失败后,作者转向使用JSch库,并成功地连接到FTP服务器,上传文件。代码中包含了连接设置、会话配置、文件上传及资源关闭等关键步骤。
摘要由CSDN通过智能技术生成

一开始百度搜了,用的 FTPClient ftpClient = new FTPClient(); 不是530就是连接异常,我以为是FTP设置的原因,看了用户ftp关于用户黑白名单也没错,端口号与IP也能ping通

后来从从别的项目里面复制的代码 ftpName(ftp用户名),ftpConnect(ftpip地址),ftpPassWord(ftp密码),ftpfile(ftp文件路径),就当保存下次也许用得上

public static void uploadFile( String fileName,InputStream input) {
   JSch jsch = new JSch();
   Session session = null;
   ChannelSftp sftp = null;
   Channel channel = null;
   try{
      session = jsch.getSession(ftpName, ftpConnect, 22);
      session.setPassword(ftpPassWord);
      Properties config = new Properties();
      config.put("StrictHostKeyChecking", "no");
      session.setConfig(config);
      session.connect();
      channel = session.openChannel("sftp");
      channel.connect();
      sftp = (ChannelSftp) channel;
      logger.info("开始上传");
      sftp.put(input, ftpfile+ fileName , ChannelSftp.OVERWRITE);
      logger.info("上传成功");
   }catch (Exception e){
      e.printStackTrace();
   } finally {
      if (session != null) {
         if (session.isConnected()) {
            session.disconnect();
         }
      }
      if (sftp != null) {
         if (sftp.isConnected()) {
            sftp.disconnect();
         }
      }
      if (channel != null) {
         if (channel.isConnected()) {
            channel.disconnect();
         }
      }
   }
}
要在Spring Boot中实现FTP推送,你需要使用一个FTP客户端库,如Apache Commons Net或Spring Integration FTP。下面是一个使用Apache Commons Net的示例代码: 1. 首先,在你的Maven或Gradle项目中添加Apache Commons Net的依赖: Maven: ``` <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> ``` Gradle: ``` compile group: 'commons-net', name: 'commons-net', version: '3.6' ``` 2. 创建一个FTP客户端类,用于连接FTP服务器和上传文件: ``` import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class FtpClient { @Value("${ftp.server}") private String server; @Value("${ftp.port}") private int port; @Value("${ftp.username}") private String username; @Value("${ftp.password}") private String password; public void uploadFile(File file, String remoteDir) throws IOException { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); String remoteFile = remoteDir + "/" + file.getName(); FileInputStream inputStream = new FileInputStream(file); boolean uploaded = ftpClient.storeFile(remoteFile, inputStream); if (uploaded) { System.out.println("File uploaded successfully."); } else { System.out.println("File upload failed."); } } finally { ftpClient.logout(); ftpClient.disconnect(); } } } ``` 3. 在你的应用程序中使用FtpClient类来上传文件: ``` @Autowired private FtpClient ftpClient; public void pushFileToFTP() { File file = new File("path/to/local/file"); String remoteDir = "/path/to/remote/directory"; try { ftpClient.uploadFile(file, remoteDir); } catch (IOException e) { e.printStackTrace(); } } ``` 这样,你就可以在Spring Boot应用程序中使用FTP客户端库来实现文件推送FTP服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值