最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Java手寫一個(gè)好用的FTP操作工具類

 更新時(shí)間:2022年04月25日 09:09:42   作者:洛陽泰山  
網(wǎng)上百度了很多FTP的java?工具類,發(fā)現(xiàn)文章代碼都比較久遠(yuǎn),且代碼臃腫,即使搜到了代碼寫的還可以的,封裝的常用操作方法不全面。所以本文將手寫一個(gè)好用的Java?FTP操作工具類,需要的可以參考一下

前言

網(wǎng)上百度了很多FTP的java 工具類,發(fā)現(xiàn)文章代碼都比較久遠(yuǎn),且代碼臃腫,即使搜到了代碼寫的還可以的,封裝的常用操作方法不全面,于是自己花了半天實(shí)現(xiàn)一個(gè)好用的工具類。最初想用java自帶的FTPClient 的jar 去封裝,后來和apache的jar工具包對(duì)比后,發(fā)現(xiàn)易用性遠(yuǎn)不如apache,于是決定采用apache的ftp的jar 封裝ftp操作類。

windows服務(wù)器搭建FTP服務(wù)

打開控制版面,圖示win 10為例。

點(diǎn)擊程序

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

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

打開 IIS管理器

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

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

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

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

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

工具類方法

  • 賬戶密碼登錄方法
  • 無賬號(hào)密碼登錄方法
  • 字符轉(zhuǎn)碼方法
  • 判斷文件目錄是否存在方法
  • 獲取文件列表方法
  • 上傳文件方法
  • 下載文件方法
  • 上傳文件夾方法
  • 下載文件夾方法
  • 刪除文件方法
  • 刪除文件夾方法
  • 創(chuàng)建文件夾方法
  • 文件重命名方法

代碼展示

pom文件引入依賴關(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>

工具類完整代碼

 
import org.apache.commons.net.ftp.*;
 
import java.io.*;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Java FTP工具類
 */
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ù)器 匿名登錄無密碼
     */
    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è)置文件類型
        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手寫一個(gè)好用的FTP操作工具類的文章就介紹到這了,更多相關(guān)Java FTP操作工具類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

蒲城县| 拉孜县| 怀集县| 息烽县| 双峰县| 巴林左旗| 尤溪县| 东兴市| 滨州市| 乃东县| 岳阳县| 洛隆县| 开江县| 仙桃市| 襄樊市| 辽中县| 平陆县| 南京市| 房产| 马鞍山市| 卓资县| 彩票| 桐柏县| 潮州市| 当阳市| 乌审旗| 清水县| 石泉县| 安远县| 泗阳县| 乐至县| 根河市| 慈利县| 托里县| 清流县| 思南县| 景东| 桃江县| 疏勒县| 湘乡市| 水富县|