java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器
以前做的一個(gè)項(xiàng)目,用到了文件上傳下載至ftp服務(wù)器,現(xiàn)在對其進(jìn)行一下復(fù)習(xí),比較簡單,一下就能看明白。
環(huán)境:首先,先安裝ftp服務(wù)器,我是在win8本地用IIS配置的, 百度一下就可以找到安裝文檔。
1.在你的項(xiàng)目目錄下建立ftp配置文件,目錄如下圖
01 ftpconfig.properties:
ftpIp=10.73.222.29
ftpPort=21
ftpUser=WP
ftpPwd=04143114wp
ftpRemotePath=d://share
02 讀取ftpconfig.properties中的具體內(nèi)容的類:
package com.java.core.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * @author wangpei * @version 創(chuàng)建時(shí)間: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("關(guān)閉流失敗.."); e.printStackTrace(); } } } } public String getIp() {// 獲取ftp服務(wù)器的ip地址 return properties.getProperty("ftpIp"); } public String getPort() {// 獲取ftp服務(wù)器的端口 return properties.getProperty("ftpPort"); } public String getUser() {// 獲取ftp登錄用戶名 return properties.getProperty("ftpUser"); } public String getPwd() {// 獲取ftp服務(wù)器的登錄密碼 return properties.getProperty("ftpPwd"); } public String getRemotePath() {// 獲取ftp服務(wù)器的存放文件的目錄 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 創(chuàng)建時(shí)間:2017年5月6日 下午6:39:03 * 文件上傳下載業(yè)務(wù)邏輯接口層 */ 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 文件上傳下載的接口實(shí)現(xiàn)類
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 創(chuàng)建時(shí)間: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出錯(cuò)"); logout(client);// 退出/斷開FTP服務(wù)器鏈接 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);// 創(chuàng)建FTP服務(wù)器目錄 client.changeWorkingDirectory(onepath);// 改變FTP服務(wù)器目錄 } 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 { // 設(shè)置被動模式 ftpClient.enterLocalPassiveMode(); // 設(shè)置以二進(jìn)制流的方式傳輸 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 設(shè)置編輯格式 ftpClient.setControlEncoding("utf-8"); remotePath = remotePath.substring(0, remotePath.lastIndexOf(fileName)); fs = ftpClient.listFiles(remotePath);// 遞歸目標(biāo)目錄 for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) {// 查找目標(biāo)文件 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服務(wù)器鏈接 } } }
代碼很好理解,看一遍應(yīng)該就可以理解,在這兒就不具體分析了,主要看代碼中的注釋。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springMVC4之強(qiáng)大類型轉(zhuǎn)換器實(shí)例解析
本篇文章主要介紹了springMVC4之強(qiáng)大類型轉(zhuǎn)換器實(shí)例解析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04java實(shí)現(xiàn)文件夾上傳功能實(shí)例代碼(SpringBoot框架)
在web項(xiàng)目中上傳文件夾現(xiàn)在已經(jīng)成為了一個(gè)主流的需求,下面這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)文件夾上傳功能(springBoot框架)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04解決Request.getParameter獲取不到特殊字符bug問題
這篇文章主要介紹了解決Request.getParameter獲取不到特殊字符bug問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot統(tǒng)一數(shù)據(jù)返回的方法實(shí)現(xiàn)
在前后端交互過程中,為了便于數(shù)據(jù)處理,后端數(shù)據(jù)需要進(jìn)行統(tǒng)一封裝返回給前端,這種做法不僅方便前后端溝通,降低了溝通成本,還有助于項(xiàng)目的統(tǒng)一維護(hù)和后端技術(shù)部門的規(guī)范制定,本文就來介紹一下2024-10-10如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能
最近在研究mybatis,然后就去找簡化mybatis開發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個(gè)比較好的可是使用,可是經(jīng)過對比發(fā)現(xiàn)還是mybatis-plus比較好,下面這篇文章主要給大家介紹了關(guān)于如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能的相關(guān)資料,需要的朋友可以參考下2022-06-06詳解Idea 2019.2 安裝lombok插件失效問題解決
這篇文章主要介紹了詳解Idea 2019.2 安裝lombok插件失效問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10關(guān)于SpringBoot的異?;貪L和事務(wù)的使用詳解
這篇文章主要介紹了關(guān)于SpringBoot的異?;貪L和事務(wù)的使用詳解,Spring中 @Transactional 注解,默認(rèn)情況下,只對拋出的RuntimeException 異常,才會事務(wù)回滾,需要的朋友可以參考下2023-05-05IntelliJ IDEA 詳細(xì)圖解最常用的配置(適合剛剛用的新人)
這篇文章主要介紹了IntelliJ IDEA 詳細(xì)圖解最常用的配置,本篇教程非常適合剛剛用的新人,本文圖文并茂給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Java多線程之JUC(java.util.concurrent)的常見類(多線程編程常用類)
這篇文章主要給大家介紹了關(guān)于Java多線程之JUC(java.util.concurrent)的常見類(多線程編程常用類)的相關(guān)資料,Java中的JUC(java.util.concurrent)包提供了一些并發(fā)編程中常用的類,這些類可以幫助我們更方便地實(shí)現(xiàn)多線程編程,需要的朋友可以參考下2024-02-02