当前位置:网站首页>File upload and download based on FTP protocol
File upload and download based on FTP protocol
2022-08-07 17:26:00 【Erdan~】
目录
一、 什么是FTP?
FTP(文件传输协议)是一个用于在计算机网络上客户端和服务器之间进行文件传输的应用层协议.FTP客户首先发起建立1个与FTP服务器端口号21之间的TCP控制连接, 指定TCP作为传输层协议,客户在建立的控制连接上获得身份认证.在FTP的使用当中,Users often encounter two thoughts:"下载"(Download)和"上传"(Upload)."下载"A file is a copy of a file from a remote host to its own computer;"上传"文件就是将文件从自己的计算机中拷贝至远程主机上.用Internet语言来说,用户可通过客户机程序向(从)远程主机上传(下载)文件.
二、easyftp_server
1.启动easyftp_serverserver and create a user:

浏览器访问FTP服务器

三、文件的上传
1.创建FTP协议的客户端对象(所有的操作都基于FTPClient)
2.连接FTP服务器(FTP服务器的ip地址,端口号)
3.登录(账号,密码)
4.存储
(1)Switch to the specified storage directory(如果没有,则创建该目录)
(2)Set the file type before saving
(3)存储(ftpThe storage location of the server(目录+文件名),Local file input stream)
5.断开连接
package com.apesource.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
public class Upload {
public static void main(String[] args) {
// FTP协议的客户端对象
FTPClient ftpClient = new FTPClient();
try (FileInputStream localIn = new FileInputStream("D:\\test\\珠峰队长.jpg")){
// 连接FTP服务器
ftpClient.connect("192.168.xxx.xxx", 21);
// step2: 登录
ftpClient.login("xxx", "xxx");
// step3: 操作
// 切换至指定目录
boolean isChange = ftpClient.changeWorkingDirectory("xxx");
System.out.println("切换工作目录1:" + isChange);
if(!isChange) {
ftpClient.makeDirectory("xxx");
isChange = ftpClient.changeWorkingDirectory("xxx");
}
System.out.println("切换工作目录2:" + isChange);
// 存储文件(将本地文件上传至FTP协议)
// 参数1:ftpThe storage location of the server(目录+文件名)
// 参数2:Local file input stream
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // Set the file type before saving
ftpClient.storeFile("xx.jpg", localIn); // 存储
} catch (SocketException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ftpClient.disconnect(); // 断开连接
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、文件的下载
1.创建FTP协议的客户端对象
2.连接并登录
3.Switch to the specified storage directory
4.下载文件
(1)设置文件类型(二进制文件类型)
(2)下载文件
retrieveFile(服务器指定文件,本地输出流(Responsible for writing after download))
5.断开连接
package com.apesource.demo;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
public class Download {
public static void main(String[] args) {
String downloadFileName = "jay.jpg";
FTPClient ftpClient = new FTPClient();
try(FileOutputStream out = new FileOutputStream("D:\\test\\aaa\\" + downloadFileName)) {
// 连接并登录
ftpClient.connect("192.168.254.192", 21);
ftpClient.login("root", "root");
ftpClient.changeWorkingDirectory("xxx"); // 切换目录
// 下载文件(获取FTP服务器指定目录(xxx)的文件)
// 参数1:服务器指定文件
// 参数2:本地输出流(Responsible for writing after download)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 设置文件类型
boolean isRetrieve = ftpClient.retrieveFile(downloadFileName, out);
System.out.println("下载成功?" + isRetrieve);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ftpClient.disconnect(); // 断开连接
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
边栏推荐
- excel打印区域怎么调整 excel 打印区域重新设置
- Can a laptop connect to a mobile phone hotspot? Steps to connect a laptop to a mobile phone personal hotspot
- 我又又又把Kubernetes整理了一次
- After Effects 教程,如何在 After Effects 中添加模糊效果?
- How to handle the wireless network card of the laptop How to handle the wireless network card of the laptop
- 笔记本锁屏密码怎么设置 怎样给笔记本电脑设置锁屏密码
- win7系统电脑更换锁屏壁纸的方法
- JNI学习
- What to do if the game is not compatible with the win7 system
- Win7系统升级失败提示错误代码0X80070643如何解决
猜你喜欢
![Embedded System Driver Advanced [1] - Device Model](/img/aa/7b706905f8d74ce19a856459514ab5.png)
Embedded System Driver Advanced [1] - Device Model

图文教你Word2003中怎么添加水印

How to make ppt detailed steps computer how to make ppt template

怎样设置电脑无线网络 如何设置无线网络连接

电脑怎样安装读卡器驱动 电脑无法识别读卡器驱动安装步骤

数据分析面试(一)统计基础篇

win7系统怎么激活 win7怎么激活系统

How to set a laptop lock screen password How to set a laptop lock screen password

Common sense of single chip microcomputer

企鹅电竞登录鉴权系统架构与核心数据热备容灾方案
随机推荐
win7无法打开移动硬盘怎么回事_win7打不开移动硬盘的解决教程
OD-Model【7】:YOLOv3
DOM总结
How to set CAD printing in black and white
Does the laptop have bluetooth? How to turn on bluetooth on the laptop
tiup cluster reload
我又又又把Kubernetes整理了一次
设置开机项的步骤 怎么设置开机启动项
System design questions interview eight-part essay recitation version
构建指标体系
win7怎么调保护眼睛的电脑设置_win7保护眼睛的颜色设置方法
tiup cluster replay
Embedded System Driver Advanced [1] - Device Model
电脑上的省略号怎么打 电脑中省略号怎么打
Win7 64位旗舰版系统下提示默认网关不可用无法上网怎么办
基于FPGA温湿度采集(AHT10)
chrome滚动条样式修改怎么操作 chrome浏览器滚动条样式定制设置方法
怎样解开电脑密码 如何解锁电脑的密码登录
JNI学习
电脑开机显示器黑屏怎么回事_开机显示器黑屏怎么解决