java實現ftp上傳下載功能

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

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

    下面是我寫的一個簡單的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;
}
相關文章
相關標籤/搜索