基于Java手寫(xiě)一個(gè)好用的FTP操作工具類(lèi)
前言
網(wǎng)上百度了很多FTP的java 工具類(lèi),發(fā)現(xiàn)文章代碼都比較久遠(yuǎn),且代碼臃腫,即使搜到了代碼寫(xiě)的還可以的,封裝的常用操作方法不全面,于是自己花了半天實(shí)現(xiàn)一個(gè)好用的工具類(lèi)。最初想用java自帶的FTPClient 的jar 去封裝,后來(lái)和apache的jar工具包對(duì)比后,發(fā)現(xiàn)易用性遠(yuǎn)不如apache,于是決定采用apache的ftp的jar 封裝ftp操作類(lèi)。
windows服務(wù)器搭建FTP服務(wù)
打開(kāi)控制版面,圖示win 10為例。

點(diǎn)擊程序

選擇 啟用或者關(guān)閉Windows 功能

勾選啟用 Internet Information Services 下FTP相關(guān)服務(wù)和 IIS 管理控制平臺(tái)還有萬(wàn)維網(wǎng)服務(wù) 后,點(diǎn)擊確定。

打開(kāi) IIS管理器

選中網(wǎng)站,鼠標(biāo)右鍵 ,添加 FTP 站點(diǎn)

添加 網(wǎng)站名稱(chēng),選擇本地物理路徑 ,設(shè)置完畢,點(diǎn)擊。

填寫(xiě)自己的內(nèi)網(wǎng)ip,選擇 無(wú) SSL,點(diǎn)擊下一步。

勾選匿名 (訪問(wèn)時(shí)候不需要賬戶(hù)密碼驗(yàn)證),允許所有用戶(hù) ,選擇 讀取 和寫(xiě)入權(quán)限(根據(jù)自己需求選擇),點(diǎn)擊完成。

同一內(nèi)網(wǎng)的任何電腦的文件夾 內(nèi)輸入 自己設(shè)置的ip和端口 ftp://ip:port ,即可訪問(wèn)。

工具類(lèi)方法
- 賬戶(hù)密碼登錄方法
- 無(wú)賬號(hào)密碼登錄方法
- 字符轉(zhuǎn)碼方法
- 判斷文件目錄是否存在方法
- 獲取文件列表方法
- 上傳文件方法
- 下載文件方法
- 上傳文件夾方法
- 下載文件夾方法
- 刪除文件方法
- 刪除文件夾方法
- 創(chuàng)建文件夾方法
- 文件重命名方法
代碼展示
pom文件引入依賴(lài)關(guān)系 commons-net jar
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>工具類(lèi)完整代碼
import org.apache.commons.net.ftp.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Java FTP工具類(lèi)
*/
public class FTPUtil {
private static FTPClient ftp;
/**
* 方法描述: 轉(zhuǎn)碼
*/
private static String transcode(String text){
try {
return new String(text.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING);
} catch (UnsupportedEncodingException e) {
return null;
}
}
/**
* 方法描述: 連接 ftp服務(wù)器 匿名登錄無(wú)密碼
*/
public static void connectServer(String ip, int port) throws IOException {
connectServer(ip,port,"anonymous",null);
}
/**
* 方法描述: 連接 ftp服務(wù)器
*/
public static void connectServer(String ip, int port, String user, String password) throws IOException {
// 連接ftp服務(wù)器
ftp = new FTPClient();
ftp.connect(ip, port);
// 登錄ftp服務(wù)器
ftp.login(user, password);
//設(shè)置編碼
ftp.setControlEncoding("GBK");
//設(shè)置文件類(lèi)型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
/**
* 關(guān)閉連接
*/
public static void closeServer() throws IOException {
if (ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
}
/**
* 判斷目錄是否存在
*/
public static boolean existDirectory(String pathname) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftp.listFiles(pathname);
for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory() && ftpFile.getName().equalsIgnoreCase(pathname)) {
flag = true;
break;
}
}
return flag;
}
/*
* 獲取文件列表
*/
public static List<String> listFiles(String path) throws IOException {
FTPFile[] ftpFiles = ftp.listFiles(path);
List<String> retList = new ArrayList<String>();
for (FTPFile ftpFile : ftpFiles) {
retList.add(ftpFile.getName());
}
return retList;
}
/**
* 上傳文件
*/
public static boolean uploadFile(String remote,String local) throws IOException {
InputStream is=new FileInputStream(local);
return ftp.storeFile(transcode(remote),is);
}
/**
* 下載文件
*/
public static boolean downloadFile(String remote,String local) throws IOException {
OutputStream out=new FileOutputStream(local);
return ftp.retrieveFile(transcode(remote),out);
}
/**
* 刪除文件
*/
public static boolean deleteFile(String remote) throws IOException {
return ftp.deleteFile(transcode(remote));
}
/**
* 刪除文件夾
*/
public static void deleteFolder(String remote) throws IOException {
FTPFile[] ftpFiles=ftp.listFiles(transcode(remote));
for (FTPFile ftpFile : ftpFiles) {
if(ftpFile.isDirectory()){
deleteFolder(remote+"/"+ftpFile.getName());
ftp.removeDirectory(transcode(remote+"/"+ftpFile.getName()));
}else{
deleteFile(ftpFile.getName());
}
}
ftp.removeDirectory(transcode(remote));
}
/**
* 上傳文件夾到ftp服務(wù)器
*/
public static void uploadFolder(String remote,String local) throws IOException {
File localFile=new File(local);
if(localFile.isDirectory()){
String remoteDir=remote+"/"+localFile.getName();
makeDirectory(remoteDir);
File[] partFiles=localFile.listFiles();
for (File file : partFiles) {
if(file.isDirectory()){
uploadFolder(remoteDir+"/"+file.getName(),local+"/"+file.getName());
}else {
uploadFile(remoteDir+"/"+file.getName(),local+"/"+file.getName());
}
}
}
}
/**
* 下載文件夾到本地
*/
public static void downloadFolder(String remote,String local) throws IOException {
File localFile=new File(local);
if(!localFile.exists()){
localFile.mkdirs();
}
FTPFile[] ftpFiles=ftp.listFiles(transcode(remote));
for (FTPFile ftpFile : ftpFiles) {
if(ftpFile.isDirectory()){
downloadFolder(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName());
}else {
downloadFile(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName());
}
}
}
/**
* 創(chuàng)建文件夾
*/
public static void makeDirectory(String remote) throws IOException {
if(remote.startsWith("/")){
remote=remote.substring(1);
}
String[] dirNames = remote.split("/");
String tempPath="";
for (String dirName : dirNames) {
tempPath=tempPath+"/"+dirName;
ftp.makeDirectory(transcode(tempPath));
}
}
/**
* 重命名
*/
public static boolean rename(String from, String to) throws IOException {
return ftp.rename(transcode(from),transcode(to));
}
}使用示例
public static void main(String[] args) throws IOException {
//匿名免密碼登錄
FTPUtil.connectServer("172.16.10.201",19001,"anonymous",null);
//下載ftp根目錄所有文件到本地文件夾
FTPUtil.downloadFolder("/","D://ftp");
//刪除文件夾以及文件
FTPUtil.deleteFolder("tarzan");
//創(chuàng)建文件夾
FTPUtil.makeDirectory("tarzan/cms");
//文件夾或文件重命名
FTPUtil.rename("泰山","泰山123");
//上傳文件夾
FTPUtil.uploadFolder("software","D:\\Git");
}到此這篇關(guān)于基于Java手寫(xiě)一個(gè)好用的FTP操作工具類(lèi)的文章就介紹到這了,更多相關(guān)Java FTP操作工具類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity詳解整合JWT實(shí)現(xiàn)全過(guò)程
JWT作為一個(gè)開(kāi)放的標(biāo)準(zhǔn)(?RFC?7519?),定義了一種簡(jiǎn)潔的,自包含的方法用于通信雙方之間以Json對(duì)象的形式安全的傳遞信息。接下來(lái)通過(guò)本文給大家介紹springSecurity+jwt實(shí)現(xiàn)互踢功能,需要的朋友可以參考下2022-07-07
Java利用序列化實(shí)現(xiàn)對(duì)象深度clone的方法
這篇文章主要介紹了Java利用序列化實(shí)現(xiàn)對(duì)象深度clone的方法,實(shí)例分析了java序列化及對(duì)象克隆的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Spring整合Quartz分布式調(diào)度的示例代碼
本篇文章主要介紹了Spring整合Quartz分布式調(diào)度的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
SpringBoot利用redis集成消息隊(duì)列的方法
這篇文章主要介紹了SpringBoot利用redis集成消息隊(duì)列的方法,需要的朋友可以參考下2017-08-08
java中對(duì)list分頁(yè)并顯示數(shù)據(jù)到頁(yè)面實(shí)例代碼
這篇文章主要介紹了java中對(duì)list分頁(yè)并顯示數(shù)據(jù)到頁(yè)面實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Mybatis-Plus根據(jù)自定義注解實(shí)現(xiàn)自動(dòng)加解密的示例代碼
我們把數(shù)據(jù)存到數(shù)據(jù)庫(kù)的時(shí)候,有些敏感字段是需要加密的,從數(shù)據(jù)庫(kù)查出來(lái)再進(jìn)行解密,如果我們使用的是Mybatis框架,那就跟著一起探索下如何使用框架的攔截器功能實(shí)現(xiàn)自動(dòng)加解密吧,需要的朋友可以參考下2024-06-06
mybatis連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)雙表查詢(xún)
本文主要介紹了mybatis連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)雙表查詢(xún),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09

