FTP工具类

时间:2019-11-08
本文章向大家介绍FTP工具类,主要包括FTP工具类使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

public class FTPTool {

    private FTPClient ftp;

    /**
     *
     * @param path
     *            上传到ftp服务器哪个路径下
     * @param addr
     *            地址
     * @param port
     *            端口号
     * @param username
     *            用户名
     * @param password
     *            密码
     * @return
     * @throws Exception
     */
    public synchronized boolean connect(String addr, int port, String path, String username, String password) {
        boolean result = false;
        try {
            ftp = new FTPClient();
            int reply;
//            ftp.setDataTimeout(30000);       //设置传输超时时间为60秒
            ftp.setConnectTimeout(30000);
            ftp.connect(addr, port);
            ftp.login(username, password);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(path);
            result = true;
        } catch (Exception e) {
            return false;
        }
        return result;
    }

    /**
     *
     * @param file
     *            上传的文件或文件夹
     * @throws Exception
     */
    public boolean upload(File file, String fileName) throws Exception {
        try {
            if (file.isDirectory()) {
                ftp.makeDirectory(file.getName());
                ftp.changeWorkingDirectory(file.getName());
                String[] files = file.list();
                for (int i = 0; i < files.length; i++) {
                    File file1 = new File(file.getPath() + "\\" + files[i]);
                    if (file1.isDirectory()) {
                        upload(file1, fileName);
                        ftp.changeToParentDirectory();
                    } else {
                        File file2 = new File(file.getPath() + "\\" + files[i]);
                        FileInputStream input = new FileInputStream(file2);
                        ftp.enterLocalPassiveMode();
                        boolean isStoreFlag = ftp.storeFile(file2.getName(), input);
                        input.close();
                        return isStoreFlag;
                    }
                }
            } else {
                File file2 = new File(file.getPath());
                FileInputStream input = new FileInputStream(file2);
                ftp.enterLocalPassiveMode();
                boolean isStoreFlag = ftp.storeFile(fileName, input);
                input.close();
                return isStoreFlag;
            }
            ftp.logout(); 
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(ftp.isConnected()) {  
                try {  
                  ftp.disconnect();  
                } catch(IOException ioe) {  
                    ioe.printStackTrace();
                }  
              }  
        }
        return false;
    }

    public StringBuilder readFile(String fileName) {
        StringBuilder sb = null;
        if (null != fileName) {
            try {
                ftp.enterLocalPassiveMode();
                InputStream in = ftp.retrieveFileStream(fileName);
                BufferedReader  reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                String line= "";
                sb = new StringBuilder(150);
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb;
    }

    public boolean deleteFile(String path, String fileName) {
        boolean result = false;
        try {
            ftp.deleteFile(path + "/" + fileName);
            result = true;
        } catch (Exception e) {
            return false;
        }
        return result;
    }
    
    
    public  boolean renameFile(String srcFname,String targetFname){    
        boolean flag = false;    
        if( ftp!=null ){    
            try {    
                flag = ftp.rename(srcFname,targetFname);    
            } catch (IOException e) {    
                e.printStackTrace();      
            }    
        }    
        return flag;    
    }    

    public static void main(String[] args) throws Exception {
        FTPTool t = new FTPTool();
        System.out.println(t.connect("172.25.xx.xx", 21, "/", "user", "pwd"));
        File file = new File("/Users/jun/Downloads/test/media.ts");
        t.upload(file, "media.ts");
    }
}

原文地址:https://www.cnblogs.com/julian-chang/p/11818303.html