FTP实现上传与下载

/**
* Title:FTPUtils
* Description: ftp上传下载工具类;
* Company:
* @author wangmin
* @since 2017年8月14日-下午10:22:18
* @version V1.0
*/
public class FTPUtils {

/**
 * 方法描述:向ftp服务器上传文件;
 * @param host         FTP服务器hostname
 * @param port         FTP服务器端口
 * @param username     FTP服务器登录账号
 * @param password     FTP服务器登录密码
 * @param basePath     FTP服务器基础目录
 * @param filePath     FTP服务器文件存放路径,例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
 * @param filename     上传到FTP服务器的文件名称
 * @param is           输入流
 * @return             成功返回true,失败返回false
 */
public static boolean uploadFile(String host, int port, String username, String password, String basePath,
        String filePath, String filename, InputStream is){
    boolean result = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        /**连接ftp服务器,如果采用默认的端口,直接使用ftp.connect(host)连接就可以了*/
        ftp.connect(host, port);
        /**登录*/
        ftp.login(username, password);
        reply = ftp.getReplyCode();
        if(!FTPReply.isPositiveCompletion(reply)){
            ftp.disconnect();
            return result;
        }
        /**切换到上传目录*/
        if(!ftp.changeWorkingDirectory(basePath + filePath)){
            /**如果目录不存在就创建目录*/
            String[] dirs = filePath.split("/");
            String tempPath = basePath;
            for (String dir : dirs) {
                if (null == dir || "".equals(dir)) {
                    continue;
                }
                tempPath += "/" + dir;
                if (!ftp.changeWorkingDirectory(tempPath)) {
                    if (!ftp.makeDirectory(tempPath)) {
                        return result;
                    } else {
                        ftp.changeWorkingDirectory(tempPath);
                    }
                }
            }
        }
        /**将上传文件的类型设置成二进制文件类型*/
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        /**上传文件*/
        if (ftp.storeFile(filename, is)) {
            return result;
        }
        /**上传完毕,关闭输入流*/
        is.close();
        /**注销ftp*/
        ftp.logout();
        result = true;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(ftp.isConnected()){
            try {
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

/**
 * 方法描述:从ftp服务器上下载文件;
 * @param host           FTP服务器hostname
 * @param port           FTP服务器端口
 * @param username       FTP服务器登录账号
 * @param password       FTP服务器登录密码
 * @param remotePath     FTP服务器上的相对路径
 * @param fileName       要下载的文件名
 * @param localPath      下载后保存到本地的路径
 * @return
 */
public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
        String fileName, String localPath){
    boolean result = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        /**连接FTP服务器,如果使用默认的端口,就直接调用ftp.connect(host)方法连接就可以了*/
        ftp.connect(host, port);
        /**登录*/
        ftp.login(username, password);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return result;
        }
        /**转移到FTP服务器目录*/
        ftp.changeWorkingDirectory(remotePath);
        FTPFile[] fs = ftp.listFiles();
        for (FTPFile file : fs) {
            if (file.getName().equals(fileName)) {
                File localFile = new File(localPath + "/" + file.getName());
                OutputStream os = new FileOutputStream(localFile);
                ftp.retrieveFile(file.getName(), os);
                os.close();
            }
        }
        ftp.logout();
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

}

猜你喜欢

转载自blog.csdn.net/wm_zq/article/details/77200191