java實現ftp上傳下載功能

jopen 11年前發布 | 28K 次閱讀 文件上傳 Java

  common-net(http://commons.apache.org/proper/commons-net/)是一個比較實用的開源組件,可以基于common-net實現許多比較實用的功能,比如說ssh、ftp、smtp 等等,先來說說比較簡單的ftp功能的實現。

    基于common-net實現ftp的功能,需要先獲取FTPClient對象,利用FTPClient對象的public boolean retrieveFile(String remote, OutputStream local)方法實現文件下載功能,利用public boolean storeFile(String remote, InputStream local)方法實現文件上傳功能。

    下面一個簡單的demo:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
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;

public class FtpTest {

public FtpTest(String userName,String password,String ip){
    this.userName = userName;
    this.password = password;
    this.ip = ip;
}

/**
 * 上傳文件
 * 
 * @param File f 要上傳的文件
 * @param String uploadDir 上傳文件的路徑
 * 
 * @return boolean b 上傳結果
 * */
public boolean putFile(File f,String uploadDir) {
    InputStream instream = null;
    boolean result = false;
    try{
        try{
            ftpClient.changeWorkingDirectory(uploadDir);
            instream = new BufferedInputStream(new FileInputStream(f));
            result = ftpClient.storeFile(f.getName(), instream);
        }finally{
            if(instream!=null){
                instream.close();
            }
        }
    }catch(IOException e){
        e.printStackTrace();
    }

    return result;
}

/**
 * 從ftp服務器下載文件
 * 
 * @param File f 要獲取的ftp服務器上的文件
 * @param String localPath 本地存放的路徑
 * 
 * @return boolean 文件下載是否成功
 * */
public boolean getFile(File f  , String localPath){
    OutputStream outStream = null;
    boolean result = false;
    try{
        try{
            outStream = new BufferedOutputStream(new FileOutputStream(new File(localPath)));
            String path = f.getPath();
            path = path.replaceAll("\\\\", "/");
            String filepath = path.substring(0, path.lastIndexOf("/")+1)+"";
            String fileName = path.substring(path.lastIndexOf("/")+1)+"";
            boolean b = ftpClient.changeWorkingDirectory(filepath);
            if(b){
                result = ftpClient.retrieveFile(fileName, outStream);
            }
        }finally{
            if(outStream != null){
                outStream.close();
            }
        }
    }catch(IOException e){
        e.printStackTrace();
    }
    return result;
}
/**
 * 獲取ftp鏈接
 * 
 * @return ftpClient
 * */
public FTPClient getFTPClient(){

    try {
        ftpClient = new FTPClient();

        ftpClient.connect(ip);
        ftpClient.login(userName, password);
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ftpClient;
}

/**
 *  關閉ftpClient鏈接
 *  
 *  @param FTPClient 要關閉的ftpClient對象
 *  
 * */
public void closeFTPClient(FTPClient ftpClient){
    try {
        try{
            ftpClient.logout();
        }finally{
            ftpClient.disconnect();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/*ftp用戶名*/
private String userName;
/*ftp密碼*/
private String password;
/*ftp服務器ip*/
private String ip;
private FTPClient ftpClient;

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!