一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java編寫ftp下載工具

java編寫ftp下載工具

2019-12-13 10:48hebedich JAVA教程

本文給大家介紹的是如何一步步實現使用java編寫FTP下載工具,而且是在Linux環境下使用javac編譯的,在運行和編譯上有些不同之處,有需要的小伙伴們參考下吧。

需要用到 java 寫一個 ftp 的工具,因為只有一點點 java 基礎,但是由于好幾年不用,幾乎算是不會了,只好一點點來搞,還好能撿起來。

不過因為是在 Linux 下使用 javac 編譯,不是在 WIN 下使用 IDE 來做這些事情,所以在運行和編譯上又費了一些時間,不過正是因為這樣對 JAVA 的一些編譯、運行的知識又了解了一些。

對于 ftp 下載工具,代碼如下:

 

復制代碼代碼如下:

import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.net.SocketException;   
import org.apache.commons.net.ftp.FTPClient;   
import org.apache.commons.net.ftp.FTPReply;   
public class FtpClient {
    private String         host;   
    private int            port;   
    private String         username;   
    private String         password;   
    private boolean        binaryTransfer = true;   
    private boolean        passiveMode    = true;   
    private String         encoding       = "UTF-8";   
    private int            clientTimeout  = 3000;   
    private boolean flag=true;
    private FTPClient ftpClient = null;
    public String getHost() {   
        return host;   
    }   
    public void setHost(String host) {   
        this.host = host;   
    }   
    public int getPort() {   
        return port;   
    }   
    public void setPort(int port) {   
        this.port = port;   
    }   
    public String getUsername() {   
        return username;   
    }   
    public void setUsername(String username) {   
        this.username = username;   
    }   
    public String getPassword() {   
        return password;   
    }   
    public void setPassword(String password) {   
        this.password = password;   
    }   
    public boolean isBinaryTransfer() {   
        return binaryTransfer;   
    }   
    public void setBinaryTransfer(boolean binaryTransfer) {   
        this.binaryTransfer = binaryTransfer;   
    }   
    public boolean isPassiveMode() {   
        return passiveMode;   
    }   
    public void setPassiveMode(boolean passiveMode) {   
        this.passiveMode = passiveMode;   
    }   
    public String getEncoding() {   
        return encoding;   
    }   
    public void setEncoding(String encoding) {   
        this.encoding = encoding;   
    }   
    public int getClientTimeout() {   
        return clientTimeout;   
    }   
    public void setClientTimeout(int clientTimeout) {   
        this.clientTimeout = clientTimeout;   
    }   
    public FtpClient(String Host) {
        this.username = "anonymous";
        this.encoding = "utf-8";
        this.binaryTransfer = true;
        this.binaryTransfer = true;
        this.port = 21;
        this.host = Host;
        try {
            this.ftpClient = getFTPClient();
        } catch (Exception e) {
            System.out.println("Create FTPClient error!");
        }
    }
    private FTPClient getFTPClient() throws IOException {   
        FTPClient ftpClient = new FTPClient(); 
        ftpClient.setControlEncoding(encoding);
        connect(ftpClient);
        if (passiveMode) {   
            ftpClient.enterLocalPassiveMode();   
        }   
        setFileType(ftpClient); 
        try {   
            ftpClient.setSoTimeout(clientTimeout);   
        } catch (SocketException e) {   
            throw new IOException("Set timeout error.", e);   
        }   
        return ftpClient;   
    }   
    private void setFileType(FTPClient ftpClient) throws IOException {   
        try {   
            if (binaryTransfer) {   
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);   
            } else {   
                ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);   
            }   
        } catch (IOException e) {   
            throw new IOException("Could not to set file type.", e);   
        }   
    }   
    public boolean connect(FTPClient ftpClient) throws IOException {   
        try {   
            ftpClient.connect(host, port);   
            int reply = ftpClient.getReplyCode();   
            if (FTPReply.isPositiveCompletion(reply)) {   
                if (ftpClient.login(username, password)) {   
                    setFileType(ftpClient);   
                    return true;   
                }   
            } else {   
                this.ftpClient.disconnect();   
                throw new IOException("FTP server refused connection.");   
            }   
        } catch (IOException e) {   
            if (this.ftpClient.isConnected()) {   
                try {   
                    this.ftpClient.disconnect();
                } catch (IOException e1) {   
                    throw new IOException("Could not disconnect from server.", e);   
                }   
            }   
            throw new IOException("Could not connect to server.", e);   
        }   
        return false;   
    }   
    private void disconnect() throws IOException {   
        try {   
            this.ftpClient.logout();   
        } catch (IOException e) {   
            System.out.println("logout may timeout!");
        } finally {
            if (this.ftpClient.isConnected()) {   
                this.ftpClient.disconnect();   
            }   
        }  
    }   
    public InputStream getStream(String serverFile) throws IOException {
        InputStream inStream = null;
        try {
            inStream = this.ftpClient.retrieveFileStream(serverFile);
            System.out.println("inStream get over!");
            return inStream;
        } catch (IOException e) {
            System.out.println("get stream exception");
            return null;
        }
    }
    public boolean writeStream(InputStream input, String localFile) throws IOException {
        FileOutputStream fout = new FileOutputStream(localFile);
        int ch = 0;
        if(input == null){
            System.out.println("input is null");
            return false;
        }
        try {
            ch = input.read();
            while(ch != -1){
                fout.write(ch);
                ch = input.read();
            }
            System.out.println("write over!");
            return flag;
        } catch (IOException e) {
            throw new IOException("Couldn't get file from server.", e);
        } 
    }
    public boolean isExist(String remoteFilePath)throws IOException{
        try{
            File file=new File(remoteFilePath);
            String remotePath=remoteFilePath.substring(0,(remoteFilePath.indexOf(file.getName())-1));
            String[] listNames = this.ftpClient.listNames(remotePath);   
            System.out.println(remoteFilePath);
            for(int i=0;i<listNames.length;i++){
                System.out.println(listNames[i]);
                if(remoteFilePath.equals(listNames[i])){
                    flag=true;
                    System.out.println("file:"+file.getName()+" existed");
                    break;
                }else {
                    flag=false;
                }
            }
        } catch (IOException e) {   
            throw new IOException("FILE EXCEPTION", e);   
        } 
        return flag;
    }
    //main for testing
    public static void main(String[] args) throws IOException {   
        String hostname = "cp01-testing-ps7130.cp01.baidu.com";
        String serverFile="/home/work/check_disk.sh";
        String localFile="/home/work/workspace/project/dhc2-0/dhc/base/ftp/task_get";
        FtpClient ftp = new FtpClient(hostname);   
        System.out.println(ftp.isExist(serverFile));
        ftp.writeStream(ftp.getStream(serverFile), localFile);
        ftp.disconnect();
    }   
}

 

這個工具是為了配合另外一個 Hadoop 工具做 集群上傳用的,所以里面的把 input 和 output 流分開了,也是為了方便另外一個工具使用。

補充一點,如何在 linux 配置運行:

如果這樣的代碼需要在 linux 下環境運行,首先要配置好響應的包,例如

 

復制代碼代碼如下:

import org.apache.commons.net.ftp.FTPClient; 

 

這個包在 apache 的網站上直接下載就行,解壓后找到對應的 jar 包,在編譯的時候進行引用:

 

復制代碼代碼如下:

export FTPPATH="${路徑}/xxx.jar"
javac -classpath $CLASSPATH:$FTPPATH FtpClient.java

 

同樣,在運行的時候也要指定 classpath:

 

復制代碼代碼如下:

java -classpath $CLASSPATH:$FTPPATH FtpClient

 

建議不要把$FTPPATH 包含在 CLASSPATH 中,用什么包就引用什么環境變量就行了,沒必要一股腦都添加進去,就像我們沒必要 import 所有的包一樣。

以上所述就是本文的全部內容了,希望能夠對大家學習java有所幫助。

請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲乱码尤物193yw在线播放 | 国产成人yy精品1024在线 | 我的绝色岳每雯雯 | 国产欧美日韩图片一区二区 | 脱女学小内内摸出水网站免费 | 9久热这里只有精品视频在线观看 | 91短视频版高清在线观看免费 | 男同巨黄gay小说好爽 | 久久久久久久尹人综合网亚洲 | 描写细腻的高h肉 | 视频污版 | 欧美美女一区二区三区 | 国产资源在线视频 | 国产欧美一区二区三区免费 | 日本不卡在线视频高清免费 | 亚洲天堂视频在线观看 | 纲手被鸣人插 | jk制服白丝超短裙流白浆 | 亚洲无限观看 | 午夜精品亚洲 | 5g影院天天5g天天爽大陆 | 日韩精品亚洲一级在线观看 | 脱了白丝校花的内裤猛烈进入 | 国产成年人网站 | 都市后宫小说 | 国产精品一级香蕉一区 | 欧美又硬又粗又长又大 | 日本高清免费不卡在线播放 | 四虎永久免费地址在线观看 | 精品无人区一区二区三区 | 亚洲欧美韩国日产综合在线 | 男男gaygays黑人 | 视频国产91 | 婷婷色综合网 | 日韩在线视精品在亚洲 | 亚洲国内精品久久 | 国产精品秒播无毒不卡 | 免费网站看v片在线成人国产系列 | 国产主播福利在线观看 | 国产3p在线| 99久久精品6在线播放 |