导读
由于项目需要,使用了ftp上传下载,这里记录下工具类。
使用
依赖
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!--FTPClient-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
工具类
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
/**
* FTP上传下载工具类
*/
public class FtpUtils {
public static void main(String[] args) {
uploadFtpFile("127.0.0.1", 21, "root", "123456", "/", "aa.exe", "F:\\", "_60_2.exe");
downloadFtpFile("127.0.0.1", 21, "root", "123456", "/", "aa.exe", "F:\\test\\", "bb.exe");
}
/**
* @param ftpHost 主机IP
* @param ftpPort 主机端口号
* @param ftpUser FTP用户名
* @param ftpPass FTP密码
* @return
*/
public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUser, String ftpPass) {
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort); //连接ftp服务器
if (ftpClient.login(ftpUser, ftpPass) && FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { //登录ftp服务器
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
System.out.println("FTP连接成功 ftpHost:" + ftpHost + ",ftpPort:" + ftpPort + ",ftpUser:" + ftpUser + ",ftpPass:" + ftpPass);
} else {
System.err.println("FTP连接失败 ftpHost:" + ftpHost + ",ftpPort:" + ftpPort + ",ftpUser:" + ftpUser + ",ftpPass:" + ftpPass);
}
} catch (Exception e) {
System.err.println("FTP连接失败 ftpHost:" + ftpHost + ",ftpPort:" + ftpPort + ",ftpUser:" + ftpUser + ",ftpPass:" + ftpPass);
e.printStackTrace();
ftpClient = null;
}
return ftpClient;
}
/**
* @param ftpHost 主机IP
* @param ftpPort 主机端口号
* @param ftpUser FTP用户名
* @param ftpPass FTP密码
* @param remotePath 远程路径
* @param remoteFileName 远程文件名
* @param localPath 本地路径
* @param localFileName 本地文件名
* @param remotePathFlag true 参考remotePath在localPath下创建路径 false直接将文件保存到localPath目录下
* @return
*/
public static boolean downloadFtpFile(String ftpHost, int ftpPort, String ftpUser, String ftpPass, String remotePath, String remoteFileName, String localPath, String localFileName, boolean remotePathFlag) {
FTPClient ftpClient = null;
OutputStream os = null;
try {
if (remotePathFlag) {
localPath = localPath + File.separator + remotePath;
}
File localFilePath = new File(localPath);
if (!localFilePath.exists()) {
localFilePath.mkdirs();
}
File localFile = new File(localPath + File.separator + localFileName);
if (localFile.exists()) {
return true;
}
ftpClient = getFTPClient(ftpHost, ftpPort, ftpUser, ftpPass);
if (null == ftpClient) {
return false;
}
ftpClient.setBufferSize(10 * 1024);
if (ftpClient.changeWorkingDirectory(remotePath)) {
os = new FileOutputStream(localFile);
if (ftpClient.retrieveFile(remoteFileName, os)) {
System.out.println("FTP文件下载成功remote:" + remotePath + File.separator + remoteFileName + ",local:" + localPath + File.separator + localFileName);
} else {
return false;
}
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
if (ftpClient != null) {
ftpClient.logout();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
/**
* @param ftpHost ip
* @param ftpPort 端口
* @param ftpUser 用户名
* @param ftpPass 密码
* @param remotePath 远程文件路径
* @param remoteFileName 远程文件名
* @param localPath 本地文件路径
* @param localFileName 本地文件名
* @return
*/
public static boolean downloadFtpFile(String ftpHost, int ftpPort, String ftpUser, String ftpPass, String remotePath, String remoteFileName, String localPath, String localFileName) {
return downloadFtpFile(ftpHost, ftpPort, ftpUser, ftpPass, remotePath, remoteFileName, localPath, localFileName, false);
}
/**
* @param ftpHost
* @param ftpPort
* @param ftpUser
* @param ftpPass
* @param remotePath
* @param remoteFileName
* @param localPath
* @param localFileName
* @return
*/
public static boolean uploadFtpFile(String ftpHost, int ftpPort, String ftpUser, String ftpPass, String remotePath, String remoteFileName, String localPath, String localFileName) {
FTPClient ftpClient = null;
InputStream is = null;
try {
ftpClient = getFTPClient(ftpHost, ftpPort, ftpUser, ftpPass);
if (null == ftpClient) {
return false;
}
ftpClient.setBufferSize(10 * 1024);
if (!ftpClient.changeWorkingDirectory(remotePath)) {
ftpClient.makeDirectory(remotePath);
ftpClient.changeWorkingDirectory(remotePath);
}
is = new FileInputStream(localPath + File.separator + localFileName);
if (ftpClient.storeFile(remoteFileName, is)) {
System.out.println("FTP文件上传成功remote:" + remotePath + File.separator + remoteFileName + ",local:" + localPath + File.separator + localFileName);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (is != null) {
is.close();
}
if (ftpClient != null) {
ftpClient.logout();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
public static FTPFile[] listFiles(String workDirectory, String ftpHost, int ftpPort, String ftpUser, String ftpPass) {
FTPFile[] listFiles = null;
FTPClient ftpClient = null;
try {
ftpClient = getFTPClient(ftpHost, ftpPort, ftpUser, ftpPass);
if (null == ftpClient) {
return null;
}
if (!ftpClient.changeWorkingDirectory(workDirectory)) {
ftpClient.makeDirectory(workDirectory);
ftpClient.changeWorkingDirectory(workDirectory);
}
listFiles = ftpClient.listFiles();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (ftpClient != null) {
ftpClient.logout();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return listFiles;
}
}
测试
服务器端
本地
END
搞定~