1.配置文件conf/vsftpd.propertiesjava
(我是單獨寫了一個配置文件,你能夠直接寫在application中)git
vsftpd.ip=192.168.**.** vsftpd.user=wangwei vsftpd.pwd=123456 vsftpd.port=21
#ftp服務器根路徑
vsftpd.remote.base.path=/var/ftp/wangwei
#ftp服務器上的相對路徑【文件路徑 =/var/ftp/wangwei/images】
vsftpd.remote.file.path=/images
#文件下載到本地的目錄位置
vsftpd.local.file.path=F:/ftp/
2.操做類FtpUtil.javagithub
package com.ch.evaluation.common.vsftpd.util; import org.apache.commons.lang3.StringUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.apache.log4j.chainsaw.Main; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.Properties; /** * @author wangwei * @date 2018/11/04 * ftp服務器文件上傳下載 */ public class FtpUtil { private static Logger LOGGER = LoggerFactory.getLogger(FtpUtil.class); private static String host; private static String port; private static String username; private static String password; private static String basePath; private static String filePath; private static String localPath; /** *讀取配置文件信息 * @return */ public static void getPropertity(){ Properties properties = new Properties(); ClassLoader load = Main.class.getClassLoader(); InputStream is = load.getResourceAsStream("conf/vsftpd.properties"); try { properties.load(is); host=properties.getProperty("vsftpd.ip");//通用 port=properties.getProperty("vsftpd.port");//通用 username=properties.getProperty("vsftpd.user");//通用 password=properties.getProperty("vsftpd.pwd");//通用 basePath=properties.getProperty("vsftpd.remote.base.path");//服務器 基路徑 filePath=properties.getProperty("vsftpd.remote.file.path");//服務器 文件路徑 localPath=properties.getProperty("vsftpd.local.file.path");//本地 下載到本地的目錄 } catch (IOException e) { e.printStackTrace(); } } /** * 上傳重載 * @param filename 上傳到服務器端口重命名 * @param sourceFilePathName 源文件【路徑+文件】 * @return */ public static boolean uploadFile(String filename, String sourceFilePathName) { getPropertity(); return uploadFile( host, port, username, password, basePath, filePath, filename, sourceFilePathName); } /** * 上傳重載 * @param filePath 上傳到服務器的相對路徑 * @param filename 上傳到服務器端口重命名 * @param sourceFilePathName 源文件【路徑+文件】 * @return */ public static boolean uploadFile(String filePath,String filename, String sourceFilePathName) { getPropertity(); return uploadFile( host, port, username, password, basePath, filePath, filename, sourceFilePathName); } /** *下載重載 * @param fileName 要下載的文件名 * @return */ public static boolean downloadFile(String fileName) { getPropertity(); return downloadFile( host, port, username, password, basePath, filePath, fileName, localPath, null); } /** *下載重載 * @param filePath 要下載的文件所在服務器的相對路徑 * @param fileName 要下載的文件名 * @return */ public static boolean downloadFile(String filePath,String fileName) { getPropertity(); return downloadFile( host, port, username, password, basePath, filePath, fileName, localPath, null); } /** * 下載重載 * @param fileName 要下載的文件名 * @param filePath 要下載的文件所在服務器的相對路徑 * @param rename 下載後重命名[null或空不進行重命名] * @return */ public static boolean downloadFile(String filePath,String fileName, String rename) { getPropertity(); return downloadFile( host, port, username, password, basePath, filePath, fileName, localPath, rename); } /** * 下載重載 * @param fileName 要下載的文件名 * @param localPath 下載時,到本地的路徑 * @param filePath 要下載的文件所在服務器的相對路徑 * @param rename 下載後重命名[null或空不進行重命名] * @return */ public static boolean downloadFile(String filePath,String fileName,String localPath, String rename) { getPropertity(); return downloadFile( host, port, username, password, basePath, filePath, fileName, localPath, rename); } /** * Description: 向FTP服務器上傳文件 * @param host FTP服務器hostname * @param port FTP服務器端口 * @param username FTP登陸帳號 * @param password FTP登陸密碼 * @param basePath FTP服務器基礎目錄 * @param filePath FTP服務器文件存放路徑。例如分日期存放:/2015/01/01。文件的路徑爲basePath+filePath * @param filename 上傳到FTP服務器上的文件名 * @return 成功返回true,不然返回false */ public static boolean uploadFile(String host, String port, String username, String password, String basePath, String filePath, String filename, String sourceFilePathName) { boolean result = false; FTPClient ftp = new FTPClient(); try { int portNum = Integer.parseInt(port); int reply; ftp.connect(host, portNum);// 鏈接FTP服務器 // 若是採用默認端口,能夠使用ftp.connect(host)的方式直接鏈接FTP服務器 ftp.login(username, password);// 登陸 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } //切換到上傳目錄 if (!ftp.changeWorkingDirectory(basePath+filePath)) { //若是目錄不存在建立目錄 String[] dirs = filePath.split("/"); String tempPath = basePath; for (String dir : dirs) { if (null == dir || "".equals(dir)) continue; tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } } } } //爲了加大上傳文件速度,將InputStream轉成BufferInputStream , InputStream input FileInputStream input = new FileInputStream(new File(sourceFilePathName)); BufferedInputStream in = new BufferedInputStream(input); //加大緩存區 ftp.setBufferSize(1024*1024); //設置上傳文件的類型爲二進制類型 ftp.setFileType(FTP.BINARY_FILE_TYPE); //上傳文件 if (!ftp.storeFile(filename, in)) { return result; } in.close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /** * Description: 從FTP服務器下載文件 * @param host FTP服務器hostname * @param port FTP服務器端口 * @param username FTP登陸帳號 * @param password FTP登陸密碼 * @param basePath FTP服務器上的相對路徑 * @param fileName 要下載的文件名 * @param localPath 下載後保存到本地的路徑 * @return */ public static boolean downloadFile(String host, String port, String username, String password, String basePath, String filePath, String fileName, String localPath, String rename) { boolean result = false; FTPClient ftp = new FTPClient(); try { int portNum = Integer.parseInt(port); int reply; ftp.connect(host, portNum); // 若是採用默認端口,能夠使用ftp.connect(host)的方式直接鏈接FTP服務器 ftp.login(username, password);// 登陸 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } //ftp.changeWorkingDirectory(basePath);// 轉移到FTP服務器目錄 if (!ftp.changeWorkingDirectory(basePath+filePath)) { LOGGER.info("服務器端目錄不存在..."); } FTPFile[] fs = ftp.listFiles(); boolean flag = true; for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { flag = false; rename = StringUtils.isEmpty(rename)?ff.getName():rename; File localFile = new File(localPath + "/" + rename); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } if(flag) LOGGER.info("服務器端文件不存在..."); ftp.logout(); result = true; } catch (IOException e) { LOGGER.info(e.getMessage()); e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } }
3.測試apache
import com.ch.evaluation.common.vsftpd.util.FtpUtil; public class ftpController { public static void main(String[] args) { /* boolean flag = FtpUtil.uploadFile("192.168.**.**", "21", "wangwei", "123456", "/var/ftp/wangwei","/images", "handsome3.jpg", "F:\\ftp\\handSome.JPG");*/ /* boolean flag = FtpUtil.uploadFile("/images1", "handsome.jpg", "F:\\ftp\\handSome.JPG");*/ /* boolean flag = FtpUtil.uploadFile("handsome.jpg", "F:\\ftp\\handSome.JPG");*/ /* boolean flag = FtpUtil.downloadFile("192.168.**.**", "21", "wangwei", "123456", "/var/ftp/wangwei/images", "handsome2.jpg", "F:/ftp/","");*/
boolean flag = FtpUtil.downloadFile("handsome2.jpg", "F:/ftp/"); System.out.println(flag); } }
附gitHub源碼:https://github.com/UncleWW/Shop緩存