当前位置:网站首页>File upload and download based on FTP protocol
File upload and download based on FTP protocol
2022-08-07 16:16:00 【I love iced tea】
FTP Protocol
FTP protocol is built on top of TCP protocol and is a common protocol for file transfer services. It adopts C/S mode and is an application layer protocol for file transfer between client and server on computer network.
(For TCP protocol, please refer to: http://t.csdn.cn/6a78D)
Simple use of FTP server
Before using, we need to install an FTP server (easyftp server) on the computer and open it as shown below:

Then we need to create an account, set the login name and password, and set the FTP server in the F:\\run\\FTP directory, so that the files we upload later will be displayed in this directory, and the download will alsoFiles will be looked for in this directory.This directory is our server location.
FTP server connection
1. Create FTP client object
2. Connect through the server's IP address and port number
3. Login with username and password
4. Disconnect after the FTP server is used up
import java.io.FileInputStream;import java.io.IOException;import org.apache.commons.net.ftp.FTPClient;public class Test {public static void main(String[] args) {//FTP client objectFTPClient ftpClient = new FTPClient();//step1: connect to FTP serverftpClient.connect("192.168.254.185", 21);//step2: loginftpClient.login("admin", "admin");} catch (IOException e) {e.printStackTrace();}finally {try {//After use, you need to disconnect the FTP serverftpClient.disconnect();} catch (IOException e) {e.printStackTrace();}}}}File upload in FTP server
1. Connect to FTP server
2. Create an input stream of a local file and read a local file
3. Set the file type to set the file to binary
4. Upload ftpClient.storeFile("zym.jpg", localIn ), pass in the file name and the file stream to be uploaded
5. Disconnect
import java.io.FileInputStream;import java.io.IOException;import org.apache.commons.net.ftp.FTPClient;public class Test {public static void main(String[] args) {//FTP client objectFTPClient ftpClient = new FTPClient();try(FileInputStream localIn = new FileInputStream("E:\\run\\IO stream\\image\\tj.jpg")){//step1: connect to FTP serverftpClient.connect("192.168.254.185", 21);//step2: loginftpClient.login("admin", "admin");//step3: Operation//Check if the directory existsboolean isChange = ftpClient.changeWorkingDirectory("zym");System.out.println("Switch working directory 1:" + isChange);if(!isChange) {//create the directory if it doesn't existftpClient.makeDirectory("zym");isChange = ftpClient.changeWorkingDirectory("zym");}System.out.println("Switch working directory 2:" + isChange);//Store files (upload local files to FTP server)//Parameter 1: The storage location of the ftp server (directory + file name)//parameter 2: local file input streamftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//Set the file type before savingftpClient.storeFile("zym.jpg",localIn );//Store} catch (IOException e) {e.printStackTrace();}finally {try {ftpClient.disconnect();} catch (IOException e) {e.printStackTrace();}}}}File download from FTP server
1. Connect to FTP server
2. Create an output stream and determine the download location
3. Set the file type to set the file to binary
4. Download ftpClient.retrieveFile(downlaodFileName, download), pass in the download file name, and the path to be downloaded
5. Disconnect
import java.io.FileOutputStream;import java.io.IOException;import java.net.SocketException;import org.apache.commons.net.ftp.FTPClient;public class FtpDownLoad {public static void main(String[] args) {//Create FTP clientFTPClient ftpClient = new FTPClient();// file to downloadString downlaodFileName = "tj.jpg";try (FileOutputStream download = new FileOutputStream("E:\\run\\test\\a.jpg")){//connect and log inftpClient.connect("192.168.254.181", 21);ftpClient.login("root", "root");ftpClient.changeWorkingDirectory("image");//Switch directory//download file//parameter 1: server specified file//Parameter 2: Local output stream (responsible for writing after downloading)ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);boolean isRetrieve = ftpClient.retrieveFile(downlaodFileName, download);System.out.println("Successful download?"+isRetrieve);} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {ftpClient.disconnect();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}边栏推荐
猜你喜欢
随机推荐
873. 欧拉函数
jmter正则表达式提取器
LeetCode Daily 2 Questions 01: Delete Duplicates in Sorted Arrays (both 1200 questions)
全部内置函数详细认识(上篇)
MySQL:基础架构与存储引擎
PWA 应用 Service Worker 缓存的一些可选策略和使用场景
手机开户股票开户安全吗 股票开户流程具体是什么
【渣渣彭-错误记录】专门记录本人的撒币问题
分组背包问题
简记_如何使用立创商城的原理图符号及封装
1712: HDU ACboy needs your help please grouping knapsack problem
845. Eight Numbers
zabbix监控华为路由器
JVM
Chapter 3 Mobile Terminal - Elementary Classroom Online Education System Effect Demonstration and Technical Preparation
"Short Video" Gansu Province introduced the "14th Five-Year" cold chain logistics high-quality development plan to create a 1-hour fresh agricultural product logistics circle
肖sir___面试就业课__非技术面试题
初识微信小程序开发
A brief description of the function stack
ceph集群
![leetcode: 636. Exclusive time of function [stack simulation]](/img/14/f10a1beba2d32cdf1152e07d2e7dc3.png)








