添加依赖Jsch-0.1.54.jar
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version></version> </dependency>
FTP上传下载文件例子
import sun.net.ftp.FtpClient; import sun.net.ftp.FtpProtocolException; import java.io.*; import java.net.InetSocketAddress; import java.net.SocketAddress; /** * Java自带的API对FTP的操作 */ public class Test { private FtpClient ftpClient; Test(){ /* 使用默认的端口号、用户名、密码以及根目录连接FTP服务器 */ , ", "/home/jiashubing/ftp/anonymous/"); } public void connectServer(String ip, int port, String user, String password, String path) { try { /* ******连接服务器的两种方法*******/ ftpClient = FtpClient.create(); try { SocketAddress addr = new InetSocketAddress(ip, port); ftpClient.connect(addr); ftpClient.login(user, password.toCharArray()); System.out.println("login success!"); ) { //把远程系统上的目录切换到参数path所指定的目录 ftpClient.changeDirectory(path); } } catch (FtpProtocolException e) { e.printStackTrace(); } } catch (IOException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } /** * 关闭连接 */ public void closeConnect() { try { ftpClient.close(); System.out.println("disconnect success"); } catch (IOException ex) { System.out.println("not disconnect"); ex.printStackTrace(); throw new RuntimeException(ex); } } /** * 上传文件 * @param localFile 本地文件 * @param remoteFile 远程文件 */ public void upload(String localFile, String remoteFile) { File file_in = new File(localFile); try(OutputStream os = ftpClient.putFileStream(remoteFile); FileInputStream is = new FileInputStream(file_in)) { ]; int c; ) { os.write(bytes, , c); } System.out.println("upload success"); } catch (IOException ex) { System.out.println("not upload"); ex.printStackTrace(); } catch (FtpProtocolException e) { e.printStackTrace(); } } /** * 下载文件。获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。 * @param remoteFile 远程文件路径(服务器端) * @param localFile 本地文件路径(客户端) */ public void download(String remoteFile, String localFile) { File file_in = new File(localFile); try(InputStream is = ftpClient.getFileStream(remoteFile); FileOutputStream os = new FileOutputStream(file_in)){ ]; int c; ) { os.write(bytes, , c); } System.out.println("download success"); } catch (IOException ex) { System.out.println("not download"); ex.printStackTrace(); }catch (FtpProtocolException e) { e.printStackTrace(); } } public static void main(String agrs[]) { Test fu = new Test(); //下载测试 String filepath[] = {"aa.xlsx","bb.xlsx"}; String localfilepath[] = {"E:/lalala/aa.xlsx","E:/lalala/bb.xlsx"}; ; i < filepath.length; i++) { fu.download(filepath[i], localfilepath[i]); } //上传测试 String localfile = "E:/lalala/tt.xlsx"; String remotefile = "tt.xlsx"; //上传 fu.upload(localfile, remotefile); fu.closeConnect(); } }
SFTP上传下载文件例子
package com; import com.jcraft.jsch.*; import java.util.Properties; /** * 解释一下SFTP的整个调用过程,这个过程就是通过Ip、Port、Username、Password获取一个Session, * 然后通过Session打开SFTP通道(获得SFTP Channel对象),再在建立通道(Channel)连接,最后我们就是 * 通过这个Channel对象来调用SFTP的各种操作方法.总是要记得,我们操作完SFTP需要手动断开Channel连接与Session连接。 * @author jiashubing * @since 2018/5/8 */ public class SftpCustom { private ChannelSftp channel; private Session session; private String sftpPath; SftpCustom() { /* 使用端口号、用户名、密码以连接SFTP服务器 */ , ", "/home/ftp/"); } SftpCustom(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword, String sftpPath) { this.connectServer(ftpHost, ftpPort, ftpUserName, ftpPassword, sftpPath); } public void connectServer(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword, String sftpPath) { try { this.sftpPath = sftpPath; // 创建JSch对象 JSch jsch = new JSch(); // 根据用户名,主机ip,端口获取一个Session对象 session = jsch.getSession(ftpUserName, ftpHost, ftpPort); if (ftpPassword != null) { // 设置密码 session.setPassword(ftpPassword); } Properties configTemp = new Properties(); configTemp.put("StrictHostKeyChecking", "no"); // 为Session对象设置properties session.setConfig(configTemp); // 设置timeout时间 session.setTimeout(); session.connect(); // 通过Session建立链接 // 打开SFTP通道 channel = (ChannelSftp) session.openChannel("sftp"); // 建立SFTP通道的连接 channel.connect(); } catch (JSchException e) { //throw new RuntimeException(e); } } /** * 断开SFTP Channel、Session连接 */ public void closeChannel() { try { if (channel != null) { channel.disconnect(); } if (session != null) { session.disconnect(); } } catch (Exception e) { // } } /** * 上传文件 * * @param localFile 本地文件 * @param remoteFile 远程文件 */ public void upload(String localFile, String remoteFile) { try { remoteFile = sftpPath + remoteFile; channel.put(localFile, remoteFile, ChannelSftp.OVERWRITE); channel.quit(); } catch (SftpException e) { e.printStackTrace(); } } /** * 下载文件 * * @param remoteFile 远程文件 * @param localFile 本地文件 */ public void download(String remoteFile, String localFile) { try { remoteFile = sftpPath + remoteFile; channel.get(remoteFile, localFile); channel.quit(); } catch (SftpException e) { e.printStackTrace(); } } public static void main(String[] args) { SftpCustom sftpCustom = new SftpCustom(); //上传测试 String localfile = "E:/logs/catalina.out"; String remotefile = "catalina.out"; sftpCustom.upload(localfile, remotefile); //下载测试 // sftpCustom.download(remotefile, "E:/lalala/catalina.out"); // sftpCustom.closeChannel(); } }