以前做的一個項目,用到了文件上傳下載至ftp服務器,現在對其進行一下複習,比較簡單,一下就能看明白。
環境:首先,先安裝ftp服務器,我是在win8本地用IIS配置的, 百度一下就可以找到安裝文檔。
1.在你的項目目錄下建立ftp配置文件,目錄如下圖
01 ftpconfig.properties:
ftpIp=10.73.222.29
ftpPort=21
ftpUser=WP
ftpPwd=04143114wp
ftpRemotePath=d://share
02 讀取ftpconfig.properties中的具體內容的類:
package com.java.core.util;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** * @author wangpei * @version 創建時間:2017年5月6日下午9:42:40 讀取ftp文件的配置文件*/public class ReadFtpProperties { private InputStream is; private Properties properties; public ReadFtpProperties() { is = this.getClass().getResourceAsStream("/ftpconfig.properties");// 將配置文件讀入輸入流中properties = new Properties(); try { properties.load(is); } catch (IOException e) { System.out.println("配置文件不存在.."); e.printStackTrace(); } finally { if (null != is) { try { is.close(); } catch (IOException e) { System.out.println("關閉流失敗.."); e.printStackTrace(); } } } } public String getIp() {// 獲取ftp服務器的ip地址return properties.getProperty("ftpIp"); } public String getPort() {// 獲取ftp服務器的端口return properties.getProperty("ftpPort"); } public String getUser() {// 獲取ftp登錄用戶名return properties.getProperty("ftpUser"); } public String getPwd() {// 獲取ftp服務器的登錄密碼return properties.getProperty("ftpPwd"); } public String getRemotePath() {// 獲取ftp服務器的存放文件的目錄return properties.getProperty("ftpRemotePath"); }}03 文件上傳下載的接口類
package com.java.web.service;import java.io.InputStream;import org.apache.commons.net.ftp.FTPClient;import com.java.core.util.ReadFtpProperties;/** * @author wangpei * @version 創建時間:2017年5月6日下午6:39:03 * 文件上傳下載業務邏輯接口層*/public interface FtpService { /* * 登錄至FTP */ public boolean loginFTP(FTPClient client, ReadFtpProperties rfp); /* * 退出ftp */ public boolean logout(FTPClient client);// /* * 上傳文件到remotePath,其在ftp上的名字為inputStream */ public boolean uploadFile(FTPClient client, String remotePath, String fileNewName, InputStream inputStream, ReadFtpProperties rfp); /* * 從目錄remotePath,下載文件fileName */ public InputStream downFileByFtp(FTPClient client, String remotePath, String fileName); /* * 刪除ftp上的目錄為pathName的文件*/ public boolean delFile(FTPClient client, String pathName);}04 文件上傳下載的接口實現類
package com.java.web.service.serviceImpl;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.SocketException;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import com.java.core.util.ReadFtpProperties;import com.java.web.service.FtpService;/** * @author wangpei * @version 創建時間:2017年5月6日下午10:02:28 類說明*/public class FtpServiceImpl implements FtpService { public boolean loginFTP(FTPClient client, ReadFtpProperties rfp) { String ftpIp = rfp.getIp(); String ftpPort = rfp.getPort(); String ftpUser = rfp.getUser(); String ftpPwd = rfp.getPwd(); // String fgtpRemotePath = rfp.getRemotePath(); boolean b = false; try { client.connect(ftpIp, Integer.parseInt(ftpPort)); } catch (NumberFormatException e) { System.out.println("無法連接到ftp"); return false; } catch (SocketException e) { System.out.println("無法連接到ftp"); return false; } catch (IOException e) { System.out.println("無法連接到ftp"); return false; } client.setControlEncoding("uft-8"); try { b = client.login(ftpUser, ftpPwd); } catch (IOException e) { System.out.println("登錄ftp出錯"); logout(client);// 退出/斷開FTP服務器鏈接return false; } return b; } public boolean logout(FTPClient client) { boolean b = false; try { b = client.logout();// 退出登錄client.disconnect();// 斷開連接} catch (IOException e) { return false; } return b; } public boolean uploadFile(FTPClient client, String remotePath, String fileNewName, InputStream inputStream, ReadFtpProperties rfp) { boolean b = false; try { client.setFileType(FTPClient.BINARY_FILE_TYPE); client.enterLocalPassiveMode(); if (remotePath != null && !"".equals(remotePath.trim())) { String[] pathes = remotePath.split("/"); for (String onepath : pathes) { if (onepath == null || "".equals(onepath.trim())) { continue; } onepath = new String(onepath.getBytes("utf-8"), "iso-8859-1"); System.out.println("onepath=" + onepath); if (!client.changeWorkingDirectory(onepath)) { client.makeDirectory(onepath);// 創建FTP服務器目錄client.changeWorkingDirectory(onepath);// 改變FTP服務器目錄} else { System.out.println("文件單路徑"); } } } b = client.storeFile(new String(fileNewName.getBytes("utf-8"), "iso-8859-1"), inputStream); } catch (UnsupportedEncodingException e) { return false; } catch (IOException e) { return false; } return b; } public InputStream downFileByFtp(FTPClient ftpClient, String remotePath, String fileName) { FTPFile[] fs; InputStream is = null; try { // 設置被動模式ftpClient.enterLocalPassiveMode(); // 設置以二進制流的方式傳輸ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 設置編輯格式ftpClient.setControlEncoding("utf-8"); remotePath = remotePath.substring(0, remotePath.lastIndexOf(fileName)); fs = ftpClient.listFiles(remotePath);// 遞歸目標目錄for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) {// 查找目標文件is = ftpClient.retrieveFileStream(new String( (remotePath + fileName).getBytes("utf-8"), "iso-8859-1")); break; } } } catch (IOException e) { e.printStackTrace(); } return is; } public boolean delFile(FTPClient ftpClient, String pathName) { boolean b = false; try { b = ftpClient.deleteFile(pathName); return b; } catch (Exception e) { return false; } finally { logout(ftpClient);// 退出/斷開FTP服務器鏈接} }}代碼很好理解,看一遍應該就可以理解,在這兒就不具體分析了,主要看代碼中的註釋。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。