1.构建maven依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
2.构建ftpClient,写上传文件和下载文件的工具类
package com.jia.utils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.net.SocketException;
public class ftpUtils {
/**
* 获取FTPClient对象
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return ftpClient
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
System.out.println("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/**
* Description: 向FTP服务器上传文件
* @param ftpClient FTPClient
* @param ftpPath FTP服务器中文件所在路径
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(FTPClient ftpClient,String ftpPath,
String fileName,InputStream input) {
boolean success = false;
try {
int reply;
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return success;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.makeDirectory(ftpPath);
ftpClient.changeWorkingDirectory(ftpPath);
ftpClient.storeFile(fileName, input);
input.close();
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* Description: 从FTP服务器下载文件
* @param ftpClient FTPClient
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param localPath 下载到本地的位置 格式:H:/download
* @param fileName 文件名称
*/
public static void downloadFtpFile(FTPClient ftpClient, String ftpPath, String localPath,
String fileName) {
try {
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(fileName, os);
os.close();
ftpClient.logout();
} catch (FileNotFoundException e) {
System.out.println("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误。");
e.printStackTrace();
}
}
}
3.使用实例:
3.1上传一个文件:
package com.jia;
import com.jia.utils.ftpUtils;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
FTPClient ftpClient = ftpUtils.getFTPClient("192.168.199.100","cc"
,"123",21);
try {
FileInputStream in = new FileInputStream(new File("C:\\Users\\jww\\Desktop\\test\\121.zip"));
ftpUtils.uploadFile(ftpClient,"/local_model/upload","1211.zip",in);
}catch (IOException e){
e.printStackTrace();
}
}
}
3.2下载一个文件:
package com.jia;
import com.jia.utils.ftpUtils;
import org.apache.commons.net.ftp.FTPClient;
public class Test2 {
public static void main(String[] args) {
FTPClient ftpClient = ftpUtils.getFTPClient("192.168.199.100","cc"
,"123",21);
ftpUtils.downloadFtpFile(ftpClient,"/local_model/tools/999960/model"
,"C:\\Users\\jww\\Desktop\\test","1.obj");
}
}