FtpClient

来源:互联网 发布:淘宝动漫周边网店 编辑:程序博客网 时间:2024/05/18 01:01

依赖jar:commons-net-3.3.jar

package com.xxx.utils;import net.sf.json.JSONObject;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;import java.io.*;public class FtpClientUtil {    /**     * 上传文件(可供Action/Controller层使用)     * @param path  FTP服务器完整路径     * @param file 待上传的文件     * @return     */    public static JSONObject upload(File file,String path) throws Exception{        FTPClient ftpClient = new FTPClient();        ftpClient.setControlEncoding("UTF-8");        try {            String hostname = PropertiesUtil.getProperty("ftp_hostname");//FTP服务器地址            int port = Integer.parseInt(PropertiesUtil.getProperty("ftp_post"));//FTP服务器端口号            String username = PropertiesUtil.getProperty("ftp_username");//FTP登录帐号            String password = PropertiesUtil.getProperty("ftp_password");//FTP登录密码            String filePath = path.substring(0, path.lastIndexOf("/"));            String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());            //连接FTP服务器            ftpClient.connect(hostname, port);            //登录FTP服务器            ftpClient.login(username, password);            //是否成功登录FTP服务器            int replyCode = ftpClient.getReplyCode();            if(!FTPReply.isPositiveCompletion(replyCode)){                return ResultUtil.error("上传失败");            }            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);            ftpClient.makeDirectory(filePath);            ftpClient.changeWorkingDirectory(filePath);            InputStream inputStream = new FileInputStream(file);            ftpClient.storeFile(fileName, inputStream);            inputStream.close();            ftpClient.logout();            return ResultUtil.success("上传成功");        } catch (Exception e) {            throw e;        } finally{            if(ftpClient.isConnected()){                try {                    ftpClient.disconnect();                } catch (IOException e) {                    throw e;                }            }        }    }    /**     * 下载文件     * @param path 服务器上存放文件的完整路径     * @param localPath 下载后的文件路径     * @return     */    public static JSONObject download(String path,String localPath) throws Exception{        FTPClient ftpClient = new FTPClient();        try {            String hostname = PropertiesUtil.getProperty("ftp_hostname");//FTP服务器地址            int port = Integer.parseInt(PropertiesUtil.getProperty("ftp_post"));//FTP服务器端口号            String username = PropertiesUtil.getProperty("ftp_username");//FTP登录帐号            String password = PropertiesUtil.getProperty("ftp_password");//FTP登录密码            String filePath = path.substring(0, path.lastIndexOf("/"));            String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());            //连接FTP服务器            ftpClient.connect(hostname, port);            //登录FTP服务器            ftpClient.login(username, password);            //验证FTP服务器是否登录成功            int replyCode = ftpClient.getReplyCode();            if(!FTPReply.isPositiveCompletion(replyCode)){                return ResultUtil.error("下载失败");            }            //切换FTP目录            ftpClient.changeWorkingDirectory(filePath);            FTPFile[] ftpFiles = ftpClient.listFiles();            for(FTPFile file : ftpFiles){                if(fileName.equalsIgnoreCase(file.getName())){                    File localFile = new File(localPath + "/" + file.getName());                    OutputStream os = new FileOutputStream(localFile);                    ftpClient.retrieveFile(file.getName(), os);                    os.close();                }            }            ftpClient.logout();            return ResultUtil.success("下载成功");        } catch (Exception e) {            throw e;        } finally{            if(ftpClient.isConnected()){                try {                    ftpClient.logout();                } catch (IOException e) {                    throw e;                }            }        }    }}
原创粉丝点击