FtpUtil 工具类

package xxxx;
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 org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FtpUtil {

    private final static String host = "192.168.xxx.xxxx";
    
    private final static String basePath = "/auth";
    
    private final static String username = "";
    
    private final static String password = "";
    
    public static boolean uploadFile(String localPath) throws IOException {
        File file = new File(localPath);
        FileInputStream fileInputStream = null;
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            //----------------------
            //尝试添加超时时间 解决阻塞问题
            ftp.setDefaultTimeout(60 * 1000);
            ftp.setConnectTimeout(60 * 1000);
            ftp.setDataTimeout(60 * 1000);
            //----------------------
            fileInputStream = new FileInputStream(file);
            String fileName = file.getName();
            int reply;
            ftp.connect(host);// 连接FTP服务器
            ftp.login(username, password);// 登录
            reply = ftp.getReplyCode();
            //230代表登录成功
            System.out.println(reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(basePath);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            //ftp.setBufferSize(1024);
            ftp.setControlEncoding("GBK");
            //设置被动模式
            ftp.enterLocalPassiveMode();
            //设置上传文件的类型为二进制类型
            //上传文件
            
            result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream);
            fileInputStream.close();
            ftp.logout();
//            result = true;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    
                }
            }
        }
        return result;
        
    }
    
    
    
    public static boolean uploadFile(File file) throws IOException {
        FileInputStream fileInputStream = null;
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            //----------------------
            //尝试添加超时时间 解决阻塞问题
            ftp.setDefaultTimeout(60 * 1000);
            ftp.setConnectTimeout(60 * 1000);
            ftp.setDataTimeout(60 * 1000);
            //----------------------
            fileInputStream = new FileInputStream(file);
            String fileName = file.getName();
            int reply;
            ftp.connect(host);// 连接FTP服务器
            ftp.login(username, password);// 登录
            reply = ftp.getReplyCode();
            //230代表登录成功
            System.out.println(reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(basePath);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            //ftp.setBufferSize(1024);
            ftp.setControlEncoding("GBK");
            //设置被动模式
            ftp.enterLocalPassiveMode();
            //设置上传文件的类型为二进制类型
            //上传文件
            
            result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream);
            fileInputStream.close();
            ftp.logout();
//            result = true;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    
                }
            }
        }
        return result;
        
    }
    
    public static boolean downloadFile(String remotePath, String fileName, String localPath) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host);
            // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
            ftp.login(username, password);// 登录
            reply = ftp.getReplyCode();
            System.out.println(reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
            ftp.enterLocalPassiveMode();
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                    File localFile = new File(localPath + "/" + ff.getName());

                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }

            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }

    
    /*public static void main(String[] args) {
        String localpath = "D:/ftp测试.txt";
        System.out.println(uploadFile(localpath));
        
        System.out.println(downloadFile("/auth", "ftp测试.txt", "D:/log"));
    }*/
}

使用的jar包

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>2.0</version>
</dependency>

 

posted on 2018-03-20 09:44  祥昊  阅读(216)  评论(0编辑  收藏  举报

导航