其他分享
首页 > 其他分享> > 服务器上下载文件FTP

服务器上下载文件FTP

作者:互联网

具体分为分为两个步骤走
我传入的地址格式为//127.0.0.1//test/aa.gif

  1. 链接,根据IP,端口号(默认端口号可不填),用户名密码登录
 public static FTPClient getFTPClient(NASVo vo) {
        FTPSClient ftp = new FTPSClient();
        try {
            ftp.setAutodetectUTF8(true);
            ftp.setDataTimeout(6000000);       //设置传输超时时间为6000秒
            ftp.setConnectTimeout(6000000);
            ftp.connect(vo.getApi());
            // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
            ftp.login(vo.getName(), vo.getPassword());// 登录
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                logger.error("用户名或密码错误;");
                ftp.disconnect();
            } else {
                logger.info("连接成功");
            }
        } catch (SocketException e) {
            logger.error("检查IP地址是否正确",e);
        } catch (IOException e) {
            logger.error("检查端口是否正确}",e);
        }
        return ftp;
    }
  1. 为防止文件名和路径中文乱码,设置编码进行下载
  //下载
    public List<String> downloadFtpFileOld(String ftpPath, String localPath, String fileName, NASVo vo) {
        FTPClient ftpClient = null;
        List<String> list = new ArrayList<>();
        try {
            ftpClient = getFTPClient(vo);
            ftpClient.setControlEncoding("UTF-8"); // 中文支持
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
                    "OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
                LOCAL_CHARSET = "UTF-8";
            }
            ftpClient.setControlEncoding(LOCAL_CHARSET);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpPath = "/" + ftpPath;
            ftpClient.changeWorkingDirectory(ftpPath);
            fileName = fileName.substring(0, fileName.lastIndexOf("."));
            //之前配置了正式路径的nginx静态文件,所以我返回了地址
            //127.0.0.1替换成正式路径的地址
            list.add("http://127.0.0.1/test/"+fileName + ".gltf");
            FTPFile[] ftpFiles = ftpClient.listFiles();
            for (FTPFile file : ftpFiles) {
                String fileNames = file.getName();
                if (fileNames.contains(fileName)) {
                    File localfile = new File(localPath + "/" + fileNames);
                    FileOutputStream output = new FileOutputStream(localfile);//创建localfile文件
                    boolean aa = ftpClient.retrieveFile(fileNames, output);//从ftp服务器中检索文件,写入FileOutputStream中(只能写入FileOutputStream)
                    System.out.println("是否下载成功"+aa);
                    if (output != null) {
                        output.close();
                    }
                }
            }
        } catch (FileNotFoundException e) {
            logger.error("没有找到{}文件", localPath + "-----" + File.separatorChar + "-----" + fileName, e);
        } catch (SocketException e) {
            logger.error("连接FTP失败。", e);
        } catch (IOException e) {
            logger.error("文件读取错误。", e);
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return list;
    }

标签:FTP,ftp,fileName,下载,error,catch,服务器,logger,ftpClient
来源: https://blog.csdn.net/lession07/article/details/111879549