ftp下载与上传

package com.zhirong.utils;


import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;


import javax.servlet.http.HttpServletResponse;


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


 
public class FtpUtil{  
    /** 
     * Description: 从FTP服务器下载文件 
     *  
     * @Version1.0 
     * @param url 
     *            FTP服务器hostname 
     * @param port 
     *            FTP服务器端口 
     * @param username 
     *            FTP登录账号 
     * @param password 
     *            FTP登录密码 
     *  
     * @param remotePath 
     *            FTP服务器上的相对路径 
     *  
     * @param fileName 
     *            要下载的文件名 
     * @return 
     */  
private static String LOCAL_CHARSET = "GBK";
    public static boolean downFtpFile(String url, int port, String username,  
            String password, String remotePath, String fileName,OutputStream os,
            HttpServletResponse response) {  
        boolean success = false;
        FTPClient ftp = new FTPClient();  
        try {  
            int reply;  
            ftp.connect(url, port);
            //开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
            if (FTPReply.isPositiveCompletion(ftp.sendCommand("OPTS UTF8", "ON"))) {
            LOCAL_CHARSET = "UTF-8";
            }
            ftp.setControlEncoding(LOCAL_CHARSET);
            FTPClientConfig conf;
            if("UTF-8".equals(LOCAL_CHARSET)) {
            conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);  
            }else{
            conf = new FTPClientConfig( FTPClientConfig.SYST_NT);
            }
            conf.setServerLanguageCode("zh");
            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
            ftp.login(username, password);// 登录  
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            //判断连接是否成功
            reply = ftp.getReplyCode();  
            if (!FTPReply.isPositiveCompletion(reply)) {  
                ftp.disconnect();  
                return success;  
            }
            //设置访问被动模式
            ftp.setRemoteVerificationEnabled(false);
            ftp.enterLocalPassiveMode();
            //转移到FTP服务器目录
            ftp.changeWorkingDirectory(remotePath);  
            FTPFile[] fs = ftp.listFiles(); 
            for (int i = 0; i < fs.length; i++) {  
                FTPFile ff = fs[i]; 
             if (ff.getName().equals(fileName)) {
            response.setCharacterEncoding("UTF-8");
                 response.setHeader("Content-disposition","attachment;filename="  
                + URLEncoder.encode(fileName, "utf-8"));
                 if("UTF-8".equals(LOCAL_CHARSET)) {
                ftp.retrieveFile(new String(ff.getName().getBytes("utf-8"),"ISO-8859-1"), os);   
                 }else{
                ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), os); 
                 }
            
            os.flush();
            os.close();
            ftp.logout();  
            success = true;
             }
            }
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (ftp.isConnected()) {  
                try {  
                    ftp.disconnect();  
                } catch (IOException ioe) {  
                }  
            }  
        }  
        return success;  
    }  
      
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值