当前位置:网站首页>基于FTP协议的文件上传与下载
基于FTP协议的文件上传与下载
2022-08-08 05:58:00 【[email protected]】
一、什么是FTP?
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。在FTP的使用当中,用户经常遇到两个概念:下载(Download)和上传(Upload)。"下载"文件就是从远程主机拷贝文件至自己的计算机上;"上传"文件就是将文件从自己的计算机中拷贝至远程主机上。用Internet语言来说,用户可通过客户机程序向(从)远程主机上传(下载)文件。
二、FTP服务器

使用ftp服务器进行文件传输前,要先对ftp权限进行设置

网页界面如下:

Java连接FTP服务器需要导入jar包

三、上传
package com.apesource.demo;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
public class Upload {
public static void main(String[] args) {
FTPClient ftpClient = new FTPClient();
// 创建文件输入流,用于读取本地的文件
try (FileInputStream localIn = new FileInputStream("E:\\test\\run\\yy1.jpg")) {
// step1:连接服务器
ftpClient.connect("192.168.254.159",21);
// step2:登录
ftpClient.login("qmy", "qmy");
// step3:操作
// 切换至指定位置
boolean isChange = ftpClient.changeWorkingDirectory("qmy");
System.out.println("切换工作目录1:"+isChange);
if(!isChange) {
ftpClient.makeDirectory("qmy");
isChange = ftpClient.changeWorkingDirectory("qmy");
}
System.out.println("切换工作目录2:"+isChange);
// 存储文件(将本地文件上传至ftp服务器
// 参数1:ftp服务器的存储位置(目录+文件夹)
// 参数2:本地的文件输入流
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);// 存储前设置文件类型
ftpClient.storeFile("dir01/stu/qmy.jpg", localIn);// 存储
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果:
上传结果
四、下载
package com.apesource.demo;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
public class Download {
public static void main(String[] args) {
String downloadFileName = "yy1.jpg";
FTPClient ftpClient = new FTPClient();
try (FileOutputStream out = new FileOutputStream("E:\\test\\run\\qmy\\"+downloadFileName)) {
ftpClient.connect("192.168.254.159",21);
ftpClient.login("qmy", "qmy");
ftpClient.changeWorkingDirectory("qmy");// 切换目录
// 下载文件(获取FTP服务器指定目录(qmy)的文件
// 参数1:服务器指定文件
// 参数2:本地输出流(负责下载后写入)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
boolean isRetrieve = ftpClient.retrieveFile(downloadFileName, out);
System.out.println("下载成功?"+isRetrieve);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}运行结果:
下载结果:

版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_49194330/article/details/126198023
边栏推荐
猜你喜欢

Unity鼠标光标使用学习

Redis set to start automatically at boot

Connect two tables to update the third table (updata) in postgresql

Database sub-database sub-table, when?How to divide?

【MySQL】——事务的基本概念

Runtime——KVC,KVO原理

毕设——基于人脸表情的桌面交互精灵设计(分享一下成果,附上人脸表情的数据集和自己训练出来yolov5模型以及基于PYQT5运行yolov5的交互界面)

apifox使用文档之环境变量 / 全局变量 / 临时变量附apifox学习路线图

Mail online cobalstrike fishing

oracle的插入sql错误
随机推荐
Rust development - Struct usage example
为什么互联网大厂一边疯狂裁员,一边不停招聘?
The tests that need to be done in the development of medical device products
《公共管理学》考试重点及答案
Web attack log analysis: a guide for beginners
线程使用、控制、通信
不知道取什么名字
Redis 的内存策略
0 dictionary tree/string medium LeetCode676. Implement a magic dictionary
Educational Codeforces Round 133 (Rated for Div. 2) C补题
验证的计划
Week 8 Transformer Language Models and Implications
Runtime——KVC,KVO原理
76. The minimum cover substring
Distributed Transactions: A Reliable Message Eventual Consistency Scheme
请问学习MySQL应该安装哪个版本,现在哪个版本使用最多?
日常bug小结:
Hundreds of billions, large-scale: performance tuning practice of Tencent's super-large Apache Pulsar cluster
Several postman features worth collecting will help you do more with less!
神经网络参数量和计算量,神经网络是参数模型吗