我的代码库-Java8实现FTP与SFTP文件上传下载-程序员宅基地

技术标签: java  运维  

有网上的代码,也有自己的理解,代码备份

  一般连接windows服务器使用FTP,连接linux服务器使用SFTP。linux都是通过SFTP上传文件,不需要额外安装,非要使用FTP的话,还得安装FTP服务(虽然刚开始我就是这么干的)。

  另外就是jdk1.8和jdk1.7之前的方法有些不同,网上有很多jdk1.7之前的介绍,本篇是jdk1.8的

 

添加依赖Jsch-0.1.54.jar 

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>

 

 FTP上传下载文件例子

  1 import sun.net.ftp.FtpClient;
  2 import sun.net.ftp.FtpProtocolException;
  3 
  4 import java.io.*;
  5 import java.net.InetSocketAddress;
  6 import java.net.SocketAddress;
  7 
  8 /**
  9  * Java自带的API对FTP的操作
 10  */
 11 public class Test {
 12     private FtpClient ftpClient;
 13 
 14     Test(){
 15          /*
 16         使用默认的端口号、用户名、密码以及根目录连接FTP服务器
 17          */
 18         this.connectServer("192.168.56.130", 21, "jiashubing", "123456", "/home/jiashubing/ftp/anonymous/");
 19     }
 20 
 21     public void connectServer(String ip, int port, String user, String password, String path) {
 22         try {
 23             /* ******连接服务器的两种方法*******/
 24             ftpClient = FtpClient.create();
 25             try {
 26                 SocketAddress addr = new InetSocketAddress(ip, port);
 27                 ftpClient.connect(addr);
 28                 ftpClient.login(user, password.toCharArray());
 29                 System.out.println("login success!");
 30                 if (path.length() != 0) {
 31                     //把远程系统上的目录切换到参数path所指定的目录
 32                     ftpClient.changeDirectory(path);
 33                 }
 34             } catch (FtpProtocolException e) {
 35                 e.printStackTrace();
 36             }
 37         } catch (IOException ex) {
 38             ex.printStackTrace();
 39             throw new RuntimeException(ex);
 40         }
 41     }
 42 
 43     /**
 44      * 关闭连接
 45      */
 46     public void closeConnect() {
 47         try {
 48             ftpClient.close();
 49             System.out.println("disconnect success");
 50         } catch (IOException ex) {
 51             System.out.println("not disconnect");
 52             ex.printStackTrace();
 53             throw new RuntimeException(ex);
 54         }
 55     }
 56 
 57     /**
 58      * 上传文件
 59      * @param localFile 本地文件
 60      * @param remoteFile 远程文件
 61      */
 62     public void upload(String localFile, String remoteFile) {
 63         File file_in = new File(localFile);
 64         try(OutputStream os = ftpClient.putFileStream(remoteFile);
 65             FileInputStream is = new FileInputStream(file_in)) {
 66             byte[] bytes = new byte[1024];
 67             int c;
 68             while ((c = is.read(bytes)) != -1) {
 69                 os.write(bytes, 0, c);
 70             }
 71             System.out.println("upload success");
 72         } catch (IOException ex) {
 73             System.out.println("not upload");
 74             ex.printStackTrace();
 75         } catch (FtpProtocolException e) {
 76             e.printStackTrace();
 77         }
 78     }
 79 
 80     /**
 81      * 下载文件。获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
 82      * @param remoteFile 远程文件路径(服务器端)
 83      * @param localFile 本地文件路径(客户端)
 84      */
 85     public void download(String remoteFile, String localFile) {
 86         File file_in = new File(localFile);
 87         try(InputStream is = ftpClient.getFileStream(remoteFile);
 88             FileOutputStream os = new FileOutputStream(file_in)){
 89 
 90             byte[] bytes = new byte[1024];
 91             int c;
 92             while ((c = is.read(bytes)) != -1) {
 93                 os.write(bytes, 0, c);
 94             }
 95             System.out.println("download success");
 96         } catch (IOException ex) {
 97             System.out.println("not download");
 98             ex.printStackTrace();
 99         }catch (FtpProtocolException e) {
100             e.printStackTrace();
101         }
102     }
103 
104     public static void main(String agrs[]) {
105         Test fu = new Test();
106 
107         //下载测试
108         String filepath[] = {"aa.xlsx","bb.xlsx"};
109         String localfilepath[] = {"E:/lalala/aa.xlsx","E:/lalala/bb.xlsx"};
110         for (int i = 0; i < filepath.length; i++) {
111             fu.download(filepath[i], localfilepath[i]);
112         }
113 
114         //上传测试
115         String localfile = "E:/lalala/tt.xlsx";
116         String remotefile = "tt.xlsx";                //上传
117         fu.upload(localfile, remotefile);
118         fu.closeConnect();
119 
120     }
121 
122 }
View Code

 

SFTP上传下载文件例子

  1 import com.jcraft.jsch.*;
  2 
  3 import java.util.Properties;
  4 
  5 /**
  6  * 解释一下SFTP的整个调用过程,这个过程就是通过Ip、Port、Username、Password获取一个Session,
  7  * 然后通过Session打开SFTP通道(获得SFTP Channel对象),再在建立通道(Channel)连接,最后我们就是
  8  * 通过这个Channel对象来调用SFTP的各种操作方法.总是要记得,我们操作完SFTP需要手动断开Channel连接与Session连接。
  9  * @author jiashubing
 10  * @since 2018/5/8
 11  */
 12 public class SftpCustom {
 13 
 14     private ChannelSftp channel;
 15     private Session session;
 16     private String sftpPath;
 17 
 18     SftpCustom() {
 19          /*
 20          使用端口号、用户名、密码以连接SFTP服务器
 21          */
 22         this.connectServer("192.168.56.130", 22, "jiashubing", "123456", "/home/ftp/");
 23     }
 24 
 25     SftpCustom(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword, String sftpPath) {
 26         this.connectServer(ftpHost, ftpPort, ftpUserName, ftpPassword, sftpPath);
 27     }
 28 
 29     public void connectServer(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword, String sftpPath) {
 30         try {
 31             this.sftpPath = sftpPath;
 32 
 33             // 创建JSch对象
 34             JSch jsch = new JSch();
 35             // 根据用户名,主机ip,端口获取一个Session对象
 36             session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
 37             if (ftpPassword != null) {
 38                 // 设置密码
 39                 session.setPassword(ftpPassword);
 40             }
 41             Properties configTemp = new Properties();
 42             configTemp.put("StrictHostKeyChecking", "no");
 43             // 为Session对象设置properties
 44             session.setConfig(configTemp);
 45             // 设置timeout时间
 46             session.setTimeout(60000);
 47             session.connect();
 48             // 通过Session建立链接
 49             // 打开SFTP通道
 50             channel = (ChannelSftp) session.openChannel("sftp");
 51             // 建立SFTP通道的连接
 52             channel.connect();
 53         } catch (JSchException e) {
 54             //throw new RuntimeException(e);
 55         }
 56     }
 57 
 58     /**
 59      * 断开SFTP Channel、Session连接
 60      */
 61     public void closeChannel() {
 62         try {
 63             if (channel != null) {
 64                 channel.disconnect();
 65             }
 66             if (session != null) {
 67                 session.disconnect();
 68             }
 69         } catch (Exception e) {
 70             //
 71         }
 72     }
 73 
 74     /**
 75      * 上传文件
 76      *
 77      * @param localFile  本地文件
 78      * @param remoteFile 远程文件
 79      */
 80     public void upload(String localFile, String remoteFile) {
 81         try {
 82             remoteFile = sftpPath + remoteFile;
 83             channel.put(localFile, remoteFile, ChannelSftp.OVERWRITE);
 84             channel.quit();
 85         } catch (SftpException e) {
 86             //e.printStackTrace();
 87         }
 88     }
 89 
 90     /**
 91      * 下载文件
 92      *
 93      * @param remoteFile 远程文件
 94      * @param localFile  本地文件
 95      */
 96     public void download(String remoteFile, String localFile) {
 97         try {
 98             remoteFile = sftpPath + remoteFile;
 99             channel.get(remoteFile, localFile);
100             channel.quit();
101         } catch (SftpException e) {
102             //e.printStackTrace();
103         }
104     }
105 
106     public static void main(String[] args) {
107         SftpCustom sftpCustom = new SftpCustom();
108         //上传测试
109         String localfile = "E:/lalala/tt.xlsx";
110         String remotefile = "/home/ftp/tt2.xlsx";
111         sftpCustom.upload(localfile, remotefile);
112 
113         //下载测试
114         sftpCustom.download(remotefile, "E:/lalala/tt3.xlsx");
115 
116         sftpCustom.closeChannel();
117     }
118 
119 }
View Code

 

Jsch-0.1.54.jar 学习了解

ChannelSftp类是JSch实现SFTP核心类,它包含了所有SFTP的方法,如: 

  文件上传put(),
  文件下载get(),
  进入指定目录cd().
  得到指定目录下的文件列表ls().
  重命名指定文件或目录rename().
  删除指定文件rm(),创建目录mkdir(),删除目录rmdir().

大家引用方法时可以快速参考一下,具体传参需参考源码~
最后还要提一下的就是JSch支持的三种文件传输模式:
  ● APPEND 追加模式,如果目标文件已存在,传输的文件将在目标文件后追加。
  ● RESUME 恢复模式,如果文件已经传输一部分,这时由于网络或其他任何原因导致文件传输中断,如果下一次传输相同的文件,则会从上一次中断的地方续传。
  ● OVERWRITE 完全覆盖模式,这是JSch的默认文件传输模式,即如果目标文件已经存在,传输的文件将完全覆盖目标文件,产生新的文件。

 

FTP和SFTP区别

  FTP是一种文件传输协议,一般是为了方便数据共享的。包括一个FTP服务器和多个FTP客户端。FTP客户端通过FTP协议在服务器上下载资源。而SFTP协议是在FTP的基础上对数据进行加密,使得传输的数据相对来说更安全。但是这种安全是以牺牲效率为代价的,也就是说SFTP的传输效率比FTP要低(不过现实使用当中,没有发现多大差别)。个人肤浅的认为就是:一;FTP要安装,SFTP不要安装。二;SFTP更安全,但更安全带来副作用就是的效率比FTP要低些。

建议使用sftp代替ftp。
主要因为:
  1、可以不用额外安装任何服务器端程序
  2、会更省系统资源。
  3、SFTP使用加密传输认证信息和传输数据,相对来说会更安全。
  4、也不需要单独配置,对新手来说比较简单(开启SSH默认就开启了SFTP)。

 

参考

[1].https://blog.csdn.net/g5628907/article/details/78281864
[2].https://www.cnblogs.com/xuliangxing/p/7120130.html
[3].https://jingyan.baidu.com/article/6079ad0e7feae028ff86dbf9.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34121282/article/details/85990629

智能推荐

c# 调用c++ lib静态库_c#调用lib-程序员宅基地

文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib

deepin/ubuntu安装苹方字体-程序员宅基地

文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang

html表单常见操作汇总_html表单的处理程序有那些-程序员宅基地

文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些

PHP设置谷歌验证器(Google Authenticator)实现操作二步验证_php otp 验证器-程序员宅基地

文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器

【Python】matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距-程序员宅基地

文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距

docker — 容器存储_docker 保存容器-程序员宅基地

文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器

随便推点

网络拓扑结构_网络拓扑csdn-程序员宅基地

文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn

JS重写Date函数,兼容IOS系统_date.prototype 将所有 ios-程序员宅基地

文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios

如何将EXCEL表导入plsql数据库中-程序员宅基地

文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql

Git常用命令速查手册-程序员宅基地

文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...

分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120-程序员宅基地

文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120

【C++缺省函数】 空类默认产生的6个类成员函数_空类默认产生哪些类成员函数-程序员宅基地

文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数

推荐文章

热门文章

相关标签