`

java ftp工具类

    博客分类:
  • java
阅读更多
package com.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

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

/**
 * 
 * 类 <code>FtpUtil</code>ftp工具类
 * 
 * @author suqun
 * @version 2014-4-10
 */
public class FtpUtil {
	private static FTPClient ftpClient = new FTPClient();  
    private static String encoding = System.getProperty("file.encoding"); 
    
    /** 
     * Description: 向FTP服务器上传文件 
     *  
     * @Version1.0 
     * @param url 
     *            FTP服务器hostname 
     * @param port 
     *            FTP服务器端口 
     * @param username 
     *            FTP登录账号 
     * @param password 
     *            FTP登录密码 
     * @param ftpPath 
     *            FTP服务器上存放路径 
     * @param filePath 
     *            商城文件路径 
     * @param filename 
     *            上传到FTP服务器上的文件名  
     * @return 成功返回true,否则返回false 
     */  
    public static boolean uploadFile(String url, int port, String username,  
            String password,  String ftpPath, String filePath, String filename) {  
        boolean result = false;  
  
        try {  
        	ftpClient.setControlEncoding(encoding);  
            ftpClient.connect(url, port); 
            // 登录  
            ftpClient.login(username, password);  
            ftpClient.setControlEncoding(encoding);  
            // 设置文件传输类型为二进制  
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
            //设置为被动模式登陆
            ftpClient.enterLocalPassiveMode();
            // 获取ftp登录应答代码  
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {  
                System.out.println("连接失败");  
                ftpClient.disconnect();  
                return result;  
            }  
  
            // 转移工作目录至指定目录下  
            boolean change = ftpClient.changeWorkingDirectory(ftpPath);  
            FileInputStream input = new FileInputStream(new File(filePath+"/"+filename));
            if (change) {  
                result = ftpClient.storeFile(new String(filename.getBytes(encoding),"iso-8859-1"), input);  
                if (result) {  
                    System.out.println("上传成功!");  
                }  
            }  
            input.close();  
            ftpClient.logout();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (ftpClient.isConnected()) {  
                try {  
                    ftpClient.disconnect();  
                } catch (IOException ioe) {  
                }  
            }  
        }  
        return result;  
    }  
    
    /** 
     * Description: 向FTP服务器上传文件 
     *  
     * @Version1.0 
     * @param url 
     *            FTP服务器hostname 
     * @param port 
     *            FTP服务器端口 
     * @param username 
     *            FTP登录账号 
     * @param password 
     *            FTP登录密码 
     * @param ftpPath 
     *            FTP服务器上存放路径 
     * @param filePath 
     *            商城文件路径 
     * @param filename1
     *            上传到FTP服务器上的文件名 
     * @param filename2
     *            上传到FTP服务器上的文件名  
     *            
     * @return 成功返回true,否则返回false 
     */  
	public static boolean uploadTwoFile(String url, int port, String username,
			String password, String ftpPath, String filePath, String filename1,
			String filename2) {  
        boolean result1 = false;  
        boolean result2 = false;
        try {  
            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
            ftpClient.setControlEncoding(encoding);  
            ftpClient.connect(url, port); 
            // 登录  
            ftpClient.login(username, password);  
            ftpClient.setControlEncoding(encoding);  
            // 设置文件传输类型为二进制  
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
            //设置为被动模式登陆
            ftpClient.enterLocalPassiveMode();
            // 获取ftp登录应答代码  
            int reply = ftpClient.getReplyCode();  
            if (!FTPReply.isPositiveCompletion(reply)) {  
                System.out.println("连接失败");  
                ftpClient.disconnect();  
                return false;  
            }  
  
            // 转移工作目录至指定目录下  
            boolean change = ftpClient.changeWorkingDirectory(ftpPath);  
            FileInputStream in1 = new FileInputStream(new File(filePath+"/"+filename1));
            FileInputStream in2 = new FileInputStream(new File(filePath+"/"+filename2)); 
            if (change) { 
                result1 = ftpClient.storeFile(new String(filename1.getBytes(encoding),"iso-8859-1"), in1);  
                result2 = ftpClient.storeFile(new String(filename2.getBytes(encoding),"iso-8859-1"), in2);  
                if (result1 && result2) {  
                    System.out.println("上传成功!");  
                }  
            }  
            in1.close();
            in2.close();  
            ftpClient.logout();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (ftpClient.isConnected()) {  
                try {  
                    ftpClient.disconnect();  
                } catch (IOException ioe) {  
                }  
            }  
        }  
        return result1&&result2;  
    }  
  
    /** 
     * Description: 从FTP服务器下载文件 
     *  
     * @Version1.0 
     * @param url 
     *            FTP服务器hostname 
     * @param port 
     *            FTP服务器端口 
     * @param username 
     *            FTP登录账号 
     * @param password 
     *            FTP登录密码 
     * @param remotePath 
     *            FTP服务器上的相对路径 
     * @param fileName 
     *            要下载的文件名 
     * @param localPath 
     *            下载后保存到本地的路径 
     * @return 
     */  
    public static boolean downFile(String url, int port, String username,  
            String password, String remotePath, String fileName,  
            String localPath) {  
        boolean result = false;  
        try {  
            int reply;  
            ftpClient.setControlEncoding(encoding);  
            ftpClient.connect(url, port);  
            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
            ftpClient.login(username, password);// 登录  
            // 设置文件传输类型为二进制  
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
            //设置为被动模式登陆
            ftpClient.enterLocalPassiveMode();
            // 获取ftp登录应答代码  
            reply = ftpClient.getReplyCode();  
            // 验证是否登陆成功  
            if (!FTPReply.isPositiveCompletion(reply)) {  
                ftpClient.disconnect();  
                System.err.println("FTP server refused connection.");  
                return result;  
            }  
            // 转移到FTP服务器目录至指定的目录下  
            ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));  
            // 获取文件列表  
            FTPFile[] fs = ftpClient.listFiles();  
            System.out.println(fs.length);
            for (FTPFile ff : fs) {  
            	File localFile = new File(localPath + "/" + ff.getName());  
            	OutputStream is = new FileOutputStream(localFile);  
            	ftpClient.retrieveFile(ff.getName(), is);  
            	is.close();  
            }  
            // 关闭连接
            ftpClient.logout();  
            result = true;  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (ftpClient.isConnected()) {  
                try {  
                    ftpClient.disconnect();  
                } catch (IOException ioe) {  
                }  
            }  
        }  
        return result;  
    }  
    
    /** 
     * Description: 从FTP服务器下载文件  根据文件名称下载
     *  
     * @Version1.0 
     * @param url 
     *            FTP服务器hostname 
     * @param port 
     *            FTP服务器端口 
     * @param username 
     *            FTP登录账号 
     * @param password 
     *            FTP登录密码 
     * @param remotePath 
     *            FTP服务器上的相对路径 
     * @param fileName 
     *            要下载的文件名 
     * @param localPath 
     *            下载后保存到本地的路径 
     * @param fileNameList 文件名称List
     * @return 
     */  
    public static boolean downFileByName(String url, int port, String username,  
            String password, String remotePath, String fileName,  
            String localPath,List fileNameList) {  
        boolean result = false;  
        try {  
            int reply;  
            ftpClient.setControlEncoding(encoding);  
            ftpClient.connect(url, port);  
            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
            ftpClient.login(username, password);// 登录  
            // 设置文件传输类型为二进制  
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
            //设置为被动模式登陆
            ftpClient.enterLocalPassiveMode();
            // 获取ftp登录应答代码  
            reply = ftpClient.getReplyCode();  
            // 验证是否登陆成功  
            if (!FTPReply.isPositiveCompletion(reply)) {  
                ftpClient.disconnect();  
                System.err.println("FTP server refused connection.");  
                return result;  
            }  
            // 转移到FTP服务器目录至指定的目录下  
            ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));  
            // 获取文件列表  
            FTPFile[] fs = ftpClient.listFiles();  
            System.out.println(fs.length); 
            for (FTPFile ff : fs) {  
            	if(fileNameList.contains(ff.getName())){
            		File localFile = new File(localPath + "/" + ff.getName());  
            		OutputStream is = new FileOutputStream(localFile);  
            		ftpClient.retrieveFile(ff.getName(), is);  
            		is.close();  
            	}
            }  
            // 关闭连接
            ftpClient.logout();  
            result = true;  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (ftpClient.isConnected()) {  
                try {  
                    ftpClient.disconnect();  
                } catch (IOException ioe) {  
                }  
            }  
        }  
        return result;  
    }  


    /** 
     * 将FTP服务器上文件下载到本地 
     *  
     */  
    public void testDownFile() {  
        try {  
            boolean flag = downFile("116.236.252.102", 30075, "stock",  
                    "stock", "/home/stock/107/WZT/998310010000003/download/", "", "D:/bill/download");  
            System.out.println(flag);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

    /** 
     * 将本地文件上传到FTP服务器上 
     *  
     */  
    public void testUpLoadFromDisk() {  
        try {  
            FileInputStream in = new FileInputStream(new File("E:/号码.txt"));  
//            boolean flag = uploadTwoFile("127.0.0.1", 21, "","", "/", "哈哈.txt","");  
//            System.out.println(flag);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
    }  
  
      
    public static void main(String[] args) {  
        FtpUtil fa = new FtpUtil();  
        fa.testDownFile();  
    }
    
   
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics