packagecom.tools.ftp;importorg.apache.commons.net.ftp.FTP;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPReply;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;import java.io.*;importjava.net.SocketException;/***@authoradmin
* @title: FtpUtils
* @projectName tools
* @description: FTP工具类
* @date 2020/3/1913:23*/@Componentpublic classFtpUtils {/**FTP IP*/@Value("${ftp.host}")privateString ftpHost;/**FTP用户名*/@Value("${ftp.userName}")privateString ftpUserName;/**FTP密码*/@Value("${ftp.password}")privateString ftpPassword;/**FTP 端口 默认21*/@Value("${ftp.port}")private int ftpPort =21;public static final String CHARSET_GBK = "GBK";public static final String CHARSET_UTF8 = "UTF-8";public static final String CHARSET_ISO = "iso-8859-1";/*** 获取FTPClient
*@return
*/
publicFTPClient getFTPClient() {
FTPClient ftpClient= newFTPClient();try{
ftpClient= newFTPClient();//连接FTP服务器
ftpClient.connect(ftpHost, ftpPort);//登陆FTP服务器
ftpClient.login(ftpUserName, ftpPassword);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的端口错误,请正确配置。");
}returnftpClient;
}/*** 下载文件
*@paramftpPath ftp文件存放物理路径
*@paramfileName 文件路径*/
public voiddownloadFile(String ftpPath, String localPath, String fileName) {
FTPClient ftpClient= null;try{
ftpClient=getFTPClient();//中文支持
ftpClient.setControlEncoding(CHARSET_UTF8);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile= new File(localPath + File.separatorChar +fileName);
OutputStream os= newFileOutputStream(localFile);
ftpClient.retrieveFile(fileName, os);
os.close();
ftpClient.logout();
}catch(FileNotFoundException e) {
System.out.println("没有找到" + ftpPath + "文件");
e.printStackTrace();
}catch(SocketException e) {
System.out.println("连接FTP失败.");
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
System.out.println("文件读取错误。");
e.printStackTrace();
}
}/*** 上传文件
*@paramftpPath ftp文件存放物理路径
*@paramfileName 文件路径
*@paraminput 文件输入流,即从本地服务器读取文件的IO输入流*/
public voiduploadFile(String ftpPath,String fileName,InputStream input){
FTPClient ftp=null;try{
ftp=getFTPClient();
ftp.changeWorkingDirectory(ftpPath);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
fileName=newString(fileName.getBytes(CHARSET_GBK),CHARSET_ISO);
ftp.storeFile(fileName, input);
input.close();
ftp.logout();
System.out.println("upload succes!");
}catch(Exception e) {
e.printStackTrace();
}
}publicString getFtpHost() {returnftpHost;
}public voidsetFtpHost(String ftpHost) {this.ftpHost =ftpHost;
}publicString getFtpUserName() {returnftpUserName;
}public voidsetFtpUserName(String ftpUserName) {this.ftpUserName =ftpUserName;
}publicString getFtpPassword() {returnftpPassword;
}public voidsetFtpPassword(String ftpPassword) {this.ftpPassword =ftpPassword;
}public intgetFtpPort() {returnftpPort;
}public void setFtpPort(intftpPort) {this.ftpPort =ftpPort;
}
}