前端时间有个项目遇到从远程的ftp服务器下载文件,是利用的jsch框架实现的,现对相关功能做一下总结。
需要引入依赖:
<dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.55</version>
</dependency>
package com.lsl.exam.utils;import com.jcraft.jsch.*;import java.io.*;public class FtpUtil {/*** 获取ftp的session* @param ip ftp服务器ip* @param user ftp服务器用户名* @param pwd ftp服务器密码* @param port ftp服务器端口* @return* @throws Exception*/public static Session getSession(String ip,String user ,String pwd,int port) throws Exception{Session session = null;JSch jsch = new JSch();if (port<0){//未指定端口,采用默认端口连接session = jsch.getSession(user,ip);}else {session = jsch.getSession(user,ip,port);}if (session == null){throw new Exception("sftp session is null");}session.setPassword(pwd);//计算机的密匙发生了变化,就拒绝连接。session.setConfig("StrictHostKeyChecking","no");//设置超时时间,单位为秒session.setTimeout(1800);//设置登录超时时间session.connect(30000);return session;}/*** 从ftp服务器指定目录下载文件到本地* @param ip ftp服务器ip* @param user ftp服务器用户名* @param pwd ftp服务器密码* @param port ftp服务器端口* @param remoteFileName ftp服务器文件的名字* @param remoteFilePath ftp服务器文件所在目录* @param localFile 本地文件绝对位置(包括文件名)* @throws Exception*/public static void sftpFileDownload(String ip,String user ,String pwd,int port,String remoteFileName,String remoteFilePath,String localFile) throws Exception{Session session = getSession(ip,user,pwd,port);Channel channel = null;File file = null;OutputStream output = null;try {file = new File(localFile);//如果本地文件不存在,则创建一个本地空fileif (!file.exists()){file.createNewFile();}output = new FileOutputStream(file);channel = session.openChannel("sftp");channel.connect(10000);ChannelSftp channelSftp = (ChannelSftp)channel;channelSftp.cd(remoteFilePath);//进入ftp服务器的指的目录channelSftp.get(remoteFileName,output);//获取ftp服务器目录下文件} catch (JSchException e) {e.printStackTrace();}finally {if (output != null){output.close();}}}/*** 把本地文件上传到ftp服务器指定目录下并指定文件名* @param ip ftp服务器ip* @param user ftp服务器用户名* @param pwd ftp服务器密码* @param port ftp服务器端口* @param localFile 本地文件绝对位置(包括文件名) F:\AA\aa.txt* @param newName 新文件名* @param remoteFoldPath ftp服务器指定目录* @throws Exception*/public static void ftpFileUpload(String ip,String user ,String pwd,int port,String localFile, String newName, String remoteFoldPath) throws Exception{Session session = getSession(ip,user,pwd,port);Channel channel = null;InputStream input = null;try {input = new FileInputStream(new File(localFile));channel = session.openChannel("sftp");channel.connect(10000);ChannelSftp channelSftp = (ChannelSftp)channel;channelSftp.cd(remoteFoldPath);channelSftp.put(input,newName);} catch (Exception e) {e.printStackTrace();} finally {if (input != null){input.close();}}}public static void main(String[] args) {String ip = "10.125.126.127";int port = 22;String user = "testuser";String pwd = "pwd@123";String remoteFilepath = "root/aa/bb/";String remoteFilename = "abc.txt";String localFile = "F:\\aa\\bb\\test.txt";try {sftpFileDownload(ip,user,pwd,port,remoteFilename,remoteFilepath,localFile);ftpFileUpload(ip,user,pwd,port,localFile,"newtext.txt",remoteFilepath);} catch (Exception e) {e.printStackTrace();}}
}