当前位置:网站首页>基于FTP协议的Excel文件上传与下载
基于FTP协议的Excel文件上传与下载
2022-08-08 15:53:00 【#0000FF格子衫】
1.关于FTP协议
FTP(文件传输协议)是TCP/IP协议组中的协议之一,作为网络共享文件的传输协议,在网络应用软件中具有广泛的应用。FTP协议的全称为File Transfer Protocol,FTP的目标是提高文件的共享性和可靠高效地传送数据。
2.FTP的工作模式
作为一个文件传输协议,是一个客户端/服务器系统。它的任务就是从一台计算机将文件传送到另一台计算机。既然要在不同的电脑上高效的传输文件,就必须在两台电脑之间建立起联系,那我们可以让一台电脑充当服务器端,另一台电脑充当客户端,两者有着相同的网络端口名称,并且处在同一个局域网内。
FTP的组成部分:FTP客户端、FTP服务器(存储文件)默认使用TCP端口中的20(用于传输数据)和21(用于传输控制信息)两个端口。
FTP工作方式支持两种模式:
Standard( Port方式,主动方式)和Passive(PASV,被动方式)。
(1)Porl(主动方式)
客户端连接成功后,主动告诉服务器数据连接的端口。
(2)PASV(被动模式)
客户端连接成功后,在进行数据传输时临时连接服务器的20端口,利用20端口进行数据的传输。
3.具体的JAVA代码参考案例
在应用市场中有很多开启电脑服务器的应用程序,大家可以选择一个中意的,并且设置一个用户,方便客户端的连接,并设置用户权限。
(1)上传Excel文件至FTP服务器
// 上传Excel文件至FTP服务器的uploadToFTP方法
//传入表格的列头部分和数据部分
public boolean uploadToFTP(Vector<String> colNameVector ,
Vector<Vector<Object>> dataVector) {
//生成一个临时的Excel文件
File tempExcelFile = new File("D:\\AAAAA\\" + System.currentTimeMillis() +".xlsx");
//将数据写入该Excel文件中
exportExcel(colNameVector, dataVector, tempExcelFile);
//上传
FTPClient ftpclient = new FTPClient();
try (FileInputStream in = new FileInputStream(tempExcelFile)) {
//服务器的IP地址,网络接口和登录账号密码
ftpclient.connect("***.***.***..***",21);
boolean isLogin = ftpclient.login("admin", "admin");
//提示登录成功
System.out.println("登陆成功:" + isLogin);
ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpclient.storeFile(tempExcelFile.getName(), in);
} catch (IOException e) {
e.printStackTrace();
return false;
}finally {
try {
ftpclient.disconnect();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
(2)实现FTP的下载功能的downloadFileFromFTP方法:
public boolean downloadFileFromFTP(String excelFiles) {
File uploadDir = new File("D:\\games");
if(!uploadDir.exists()) {
uploadDir.mkdir();
}
FTPClient ftpclient = new FTPClient();
try (FileOutputStream out = new FileOutputStream("D:\\FilesDownLoad\\" + excelFiles)) {
ftpclient.connect("***.***.***..***",21);
boolean isLogin = ftpclient.login("admin", "admin");
System.out.println("登陆成功:" + isLogin);
ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
boolean isretrieve = ftpclient.retrieveFile(excelFiles, out);
return isretrieve;
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ftpclient.disconnect();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return false;
}
边栏推荐
- 文档管理系统:攻克这3个痛点,解决80%企业文档管理难题
- CS231n:6 训练神经网络(二)
- Thoroughly understand the volatile keyword and application scenarios, and it is a must for interviews, and Xiaobai can understand it!
- 腾讯又一长达 8 年的服务下架。。。
- Dry goods: design high concurrency architecture from scratch
- bzoj3262 陌上花开
- 【Unity入门计划】Unity实例-C#如何通过封装实现对数据成员的保护
- 快速排序(C语言版)
- 京东T9纯手打688页神笔记,SSM框架整合Redis搭建高效互联网应用
- 消除游戏中宝石下落的原理和实现
猜你喜欢
Smobiler的复杂控件的由来与创造
(1)通过FlinkSQL将数据写入mysql demo
Dry goods: design high concurrency architecture from scratch
Guanghong Technology: The company provides manufacturing services for Xiaomi, Samsung, OPPO, Nokia and other products in India
分享这些2022设计师们决不能错过的Blender新插件
我分析30w条数据后发现,西安新房公摊最低的竟是这里?
[Online interviewer] How to achieve deduplication and idempotency
Mysql的分布式事务原理理解
sqoop连接MySQL跟本机不一致是为什么
Take you to play with the "Super Cup" ECS features and experiment on the pit [HUAWEI CLOUD is simple and far]
随机推荐
什么是低代码开发?大家都真的看好低代码开发吗?
sqoop连接MySQL跟本机不一致是为什么
国泰君安证券新手开户、有安全保障吗?
The origin and creation of Smobiler's complex controls
把酒言欢话聊天,基于Vue3.0+Tornado6.1+Redis发布订阅(pubsub)模式打造异步非阻塞(aioredis)实时(websocket)通信聊天系统
[Unity entry plan] Unity instance - how to protect data members through encapsulation in C#
文档管理系统:攻克这3个痛点,解决80%企业文档管理难题
一文读懂字节跳动“埋点验证平台”
【云原生】-MySQL压测神器HammerDB的部署及使用
程序发生run time error原因及解决方案
Install Update(Patches) on ESXi
Synergistic authors open source throttling, 2022 trend of technology foresight (asynchronous programming/container technology)
leetcode/number of palindromic substrings
Notes on the development of kindergarten enrollment registration system based on WeChat applet
‘xxxx‘ is declared but its value is never read.Vetur(6133)
firewall高级配置
在通达信开户安全不呢
10分钟快速入门RDS【华为云至简致远】
领域驱动设计系列贫血模型和充血模型
Streamsets Data Collector 3.12