程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java編寫ftp下載對象

java編寫ftp下載對象

編輯:關於JAVA

java編寫ftp下載對象。本站提示廣大學習愛好者:(java編寫ftp下載對象)文章只能為提供參考,不一定能成為您想要的結果。以下是java編寫ftp下載對象正文


須要用到 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.百度.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有所贊助。

請您花一點時光將文章分享給您的同伙或許留下評論。我們將會由衷感激您的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved