ssm单文件下载ftp服务器到本地

来源:互联网 发布:mac音频播放器 编辑:程序博客网 时间:2024/06/06 12:17

源码基本参照我的博客《ssm单文件下载ftp服务器到浏览器》,只是在FileUtils工具类做了一些修改。

    /**     * 下载ftp文件到本地     * @param response     * @param fileName 文件名称     * @param file  ftp文件夹路径  如:"upload/fjdz"     * @param localPath 本地文件夹路径     * @return     */    public static boolean downloadFile(String file, String fileName ,String localPath){        if (!new File(localPath) .exists()){                   mkdirs(localPath);           }        boolean success = false;        FTPClient ftp = new FTPClient();        try {            int reply;            ftp.connect(SERVER_IP);            ftp.login(USERNAME, PASSWORD);            reply = ftp.getReplyCode();            if (!FTPReply.isPositiveCompletion(reply)) {                ftp.disconnect();                return success;            }            ftp.setControlEncoding("UTF-8");            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);              ftp.enterLocalPassiveMode();              ftp.changeWorkingDirectory(file);            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();            success = true;        } catch (IOException e) {            e.printStackTrace();        } finally {            if (ftp.isConnected()) {                try {                    ftp.disconnect();                } catch (IOException ioe) {                }            }        }        return success;    }