首页 > 代码库 > ftp上传与下载

ftp上传与下载

在企业级应用系统中,文件上传与下载是最常用的功能之一.当然,这些文件也是有存储周期,根据业务需要而定.

驾驶员培训管理系统(简称驾培系统)需要判断学员学时的有效性,通过学时记录仪,采集学员在教练车中里照片,

上传到web服务器中,由web服务器上传到文件服务器,,比便做人脸比对..为保证学时的实时性,这期间会产生多次比对,当

然也会产生许多学员人脸照片.为方便管理这些照片,我们会采用分层级目录来存放照片.   

这里主要介绍基于commons-net实现ftp创建文件夹、上传、下载功能.


创建文件夹

连接服务器

private void connect(FTPClient ftp) throws Exception{
		//连接服务器
		ftp.connect( server, port);
		int reply = ftp.getReplyCode();
		//是否连接成功
		if ( !FTPReply.isPositiveCompletion( reply ) )         {
		    throw new ConnectException( server+" 服务器拒绝连接" );
		}
		//登陆
		if (!ftp.login(username, password)) {
			 throw new ConnectException( "用户名或密码错误" );
		}
	}

设置属性

/**
	 * @param ftp
	 * @throws Exception 
	 */
	private void setProperty(FTPClient ftp) throws Exception {
		ftp.enterLocalPassiveMode();
		//二进制传输,默认为ASCII
		ftp.setFileType(FTP.BINARY_FILE_TYPE);
	}


创建文件夹

/**
	 * @param ftp
	 * @param remotePathName
	 */
	private void mkdir(FTPClient ftp, String remotePathName) throws Exception{
		ftp.makeDirectory(remotePathName);
	}

退出

/**
	 * @param ftp
	 */
	private void logout(FTPClient ftp) throws Exception{
		ftp.noop();
		ftp.logout();
	}


上传

/**
	 * @param ftp
	 * @param remoteFileName
	 * @param locaFileName
	 */
	private void upload(FTPClient ftp, String remoteFileName,
			String locaFileName) throws Exception{
		//上传
		InputStream input;

		input = new FileInputStream(locaFileName);

		ftp.storeFile(remoteFileName, input);

		input.close();
	}


下载

/**
	 * @param ftp
	 * @param remoteFileName
	 * @param locaFileName
	 */
	private void download(FTPClient ftp, String remoteFileName,
			String locaFileName) throws Exception{
		OutputStream output=null;
		output = new FileOutputStream(locaFileName);
		ftp.retrieveFile(remoteFileName, output); 
		output.close();
	}



源码

FtpClientUtil

package com.demo.ftp;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ConnectException;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
 * @author wobendiankun 2015-1-8 下午08:11:08
 */
public class FtpClientUtil {
	/**
	 * ftp服务器地址
	 */
	private  String server;
	/**
	 * 端口
	 */
	private  int port;
	/**
	 * 用户名
	 */
	private  String username;
	/**
	 * 
	 */
	private  String password;
	
	public FtpClientUtil () {
		server = "localhost";
		port = 21;
		username = "kun";
		password = "111111";
	}
	
	public FtpClientUtil(String server, int port, String username,
			String password) {
		this.server = server;
		this.port = port;
		this.username = username;
		this.password = password;
	}
	/**上传文件
	 * @param remoteFileName 远程文件名称
	 * @param locaFileName 本地文件名称
	 */
	public void upload(String remoteFileName,String locaFileName){
		FTPClient ftp=null; 
		try {
			ftp = new FTPClient();
			ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) );
			//连接ftp服务器
			connect( ftp );
			//设置属性
			setProperty( ftp );
			//上传文件
			upload( ftp, remoteFileName, locaFileName );
			//退出
			logout( ftp );
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
				}
			}
		}
		
	}
	/**上传文件
	 * @param remoteFileName 远程文件名称
	 * @param locaFileName 本地文件名称
	 */
	public void download(String remoteFileName,String locaFileName){
		FTPClient ftp=null; 
		try {
			ftp = new FTPClient();
			ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) );
			//连接ftp服务器
			connect( ftp );
			//设置属性
			setProperty( ftp );
			//下载文件
			download( ftp, remoteFileName, locaFileName );
			//退出
			logout( ftp );
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
				}
			}
		}
	}
	/**创建文件夹
	 * @param remotePathName 远程文件夹名称
	 */
	public void mkdir(String remotePathName) {
		FTPClient ftp=null; 
		try {
			ftp = new FTPClient();
			ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) );
			//连接ftp服务器
			connect( ftp );
			//设置属性
			setProperty( ftp );
			//创建文件夹
			mkdir( ftp, remotePathName);
			//退出
			logout( ftp );
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
				}
			}
		}
	}
	/**
	 * @param ftp
	 * @param remotePathName
	 */
	private void mkdir(FTPClient ftp, String remotePathName) throws Exception{
		ftp.makeDirectory(remotePathName);
	}

	/**
	 * @param ftp
	 * @param remoteFileName
	 * @param locaFileName
	 */
	private void download(FTPClient ftp, String remoteFileName,
			String locaFileName) throws Exception{
		OutputStream output=null;
		output = new FileOutputStream(locaFileName);
		ftp.retrieveFile(remoteFileName, output); 
		output.close();
	}

	/**
	 * @param ftp
	 * @throws Exception 
	 */
	private void setProperty(FTPClient ftp) throws Exception {
		ftp.enterLocalPassiveMode();
		//二进制传输,默认为ASCII
		ftp.setFileType(FTP.BINARY_FILE_TYPE);
	}

	/**
	 * @param ftp
	 */
	private void logout(FTPClient ftp) throws Exception{
		ftp.noop();
		ftp.logout();
	}

	/**
	 * @param ftp
	 * @param remoteFileName
	 * @param locaFileName
	 */
	private void upload(FTPClient ftp, String remoteFileName,
			String locaFileName) throws Exception{
		//上传
		InputStream input;

		input = new FileInputStream(locaFileName);

		ftp.storeFile(remoteFileName, input);

		input.close();
	}

	/**
	 * @param ftp
	 */
	private void connect(FTPClient ftp) throws Exception{
		//连接服务器
		ftp.connect( server, port);
		int reply = ftp.getReplyCode();
		//是否连接成功
		if ( !FTPReply.isPositiveCompletion( reply ) )         {
		    throw new ConnectException( server+" 服务器拒绝连接" );
		}
		//登陆
		if (!ftp.login(username, password)) {
			 throw new ConnectException( "用户名或密码错误" );
		}
	}
}

FtpTest

package com.demo.ftp;

/**
 * @author wobendiankun 2015-1-8 下午08:34:36
 */
public class FtpTest {

	public static void main(String[] args) {
		//上传文件
		//upload();
		//下载文件
		//download();
		//创建文件夹
		mkdir();
	}

	/**
	 * 
	 */
	private static void mkdir() {
		FtpClientUtil clientUtil=new FtpClientUtil();
		clientUtil.mkdir("test");
	}

	/**
	 * 
	 */
	private static void download() {
		String remoteFileName="./111.txt";
		String locaFileName="F:\\test\\txt\\3333.txt";
		FtpClientUtil clientUtil=new FtpClientUtil();
		clientUtil.download(remoteFileName, locaFileName);
	}

	/**
	 * 
	 */
	private static void upload() {
		String remoteFileName="./111.txt";
		String locaFileName="F:\\test\\txt\\aaa.txt";
		FtpClientUtil clientUtil=new FtpClientUtil();
		clientUtil.upload(remoteFileName, locaFileName);
	}
}



ftp上传与下载