ftp文件传输

1.ftp

1.1 client

lib:commons-net-3.5.jar

package com.eplusing.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

import com.paic.beos.adapter.exception.BusinessException;

public class FtpUtil {
	public static final int BUFFER_CAPCITY = 5 * 1024 * 1024;
	public static final String CHARSET = "GBK";
	private static final Logger LOGGER = Logger.getLogger(FtpUtil.class);
	//文件上传目录为FTP用户的home目录
	public static final String IN_PATH = System.getProperty("file.separator");
	//文件下载目录为FTP用户的home目录
	public static final String OUT_PATH = System.getProperty("file.separator");
	public static final String FTP_IP = "10.11.1.12";
	public static final int FTP_PORT = 21;
	public static final String FTP_UID = "ftpusername";
	public static final String FTP_PWD = "ftppassword";
	
	/** 
	 * @param remotePath FTP服务器保存目录 
	 * @param fileName 上传到FTP服务器上的文件名 
	 * @param upContent 上传的字符串内容 
	 * @return 成功返回true,否则返回false 
	 * @throws BusinessException 
	 */  
	public static boolean uploadFile(String remotePath, String fileName, String upContent) throws Exception {  
	    boolean success = false;
	    FTPClient ftpClient = null;
	    try {  
	    	if(null != (ftpClient = connectServer())){
	    		InputStream input = IOUtils.toInputStream(upContent, CHARSET); 
	    		ftpClient.changeWorkingDirectory(remotePath); 
	    		ftpClient.storeFile(fileName, input); 
	    		
	    		input.close();  
	    		ftpClient.logout();  
	    		success = true;  
	    		LOGGER.info("上传文件成功");
	    	}
	    } catch (IOException e) {  
	    	LOGGER.error("FTP上传文件失败");
	    	throw new Exception("上传文件失败");
	    } finally {  
	        if (null != ftpClient && ftpClient.isConnected()) {  
	            try {  
	            	ftpClient.disconnect(); 
	            } catch (IOException ioe) { 
	            	LOGGER.error("关闭FTP连接失败");
	            }  
	        }  
	    }  
	    return success;  
	}
	
	/**
	 * @param remotePath FTP服务器上的相对路径 
	 * @param fileName 要下载的文件名 
	 * @return 
	 */  
	public static InputStream downFile(String remotePath, String fileName) {
	    String remoteFileTotalName = fileName;
	    InputStream in = null;
	    FTPClient ftpClient = null;
	    try {  
	    	if(null != (ftpClient = connectServer())){
	    		//转移到FTP服务器目录
	    		ftpClient.changeWorkingDirectory(remotePath); 
	    		ftpClient.setBufferSize(BUFFER_CAPCITY);
	    		ftpClient.setControlEncoding(CHARSET);
	    		ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
	    		//in = ftpClient.retrieveFileStream(new String(remoteFileTotalName.getBytes("GBK"), "UTF-8"));
	    		in = ftpClient.retrieveFileStream(remoteFileTotalName);
	    		String result = IOUtils.toString(in, CHARSET);
	    		in.close();
	    		ftpClient.completePendingCommand();
	    		
	    		LOGGER.info("从FTP服务器读取回盘文件成功"); 
	    		return IOUtils.toInputStream(result, CHARSET);
	    		
	    	}
	    } catch (IOException e) {  
	    	LOGGER.error("FTP下载文件失败");   
	    } finally {  
	        if (null != ftpClient && ftpClient.isConnected()) {  
	            try {  
	            	ftpClient.disconnect();  
	            } catch (IOException ioe) {  
	            	LOGGER.error("关闭FTP连接失败");   
	            }  
	        }  
	    }  
	    return in;  
	}
	
	/**  
     * 连接到服务器  
     * @return true 连接服务器成功,false 连接服务器失败  
     */   
    public static FTPClient connectServer() {     
        int reply;   
        FTPClient ftpClient = null;
        try {   
        	ftpClient = new FTPClient();   
        	//超时时间30秒
        	ftpClient.setDataTimeout(30000); 
        	ftpClient.connect(FTP_IP, FTP_PORT);   
        	ftpClient.login(FTP_UID, FTP_PWD);  
        	reply = ftpClient.getReplyCode(); 
        	
        	if (!FTPReply.isPositiveCompletion(reply)) {   
        		ftpClient.disconnect();   
        		LOGGER.error("FTP服务拒绝连接!ftp返回码{"+ reply + "}");   
        		return null;   
        	} 
        	
        	ftpClient.setControlEncoding(CHARSET);   
        	ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        	ftpClient.enterLocalPassiveMode(); 
        	LOGGER.info("登陆FTP服务器成功");
        } catch (SocketException e) {   
        	LOGGER.error("登录FTP服务器 " + FTP_IP + ":" + FTP_PORT + " 失败,连接超时!");   
        	ftpClient = null;   
        } catch (IOException e) {   
        	ftpClient = null;   
        	LOGGER.error("登录FTP服务器 " + FTP_IP + ":" + FTP_PORT + " 失败,FTP服务器无法打开!");   
        }  
        
        return ftpClient;   
    }  
}

 

 

1.1 server

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值