码迷,mamicode.com
首页 > Web开发 > 详细

FTP文件上传下载

时间:2019-02-15 11:50:33      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:local   close   oca   url   mode   文件的   err   port   lag   

在开发上传和下载FTP上的文件的时候,能登录上ftp服务器,但是上传下载总是报错,后面在操作前加上了FTPClient.enterLocalPassiveMode()方法。成功。
注:调用FTPClient.enterLocalPassiveMode();这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据,但是在linux上,由于安全限制,可能某些端口没有开启,所以就出现阻塞。

上传下载代码如下:


public class FtpApche {
    private static FTPClient ftpClient = new FTPClient();
    private static String encoding = System.getProperty("file.encoding");

/**
??* Description: 从FTP服务器下载文件
??* 
??* @Version1.0
??* @param url
??*?? FTP服务器hostname
??* @param port
??*?? FTP服务器端口
??* @param username
??*?? FTP登录账号
??* @param password
??*?? FTP登录密码
??* @param remotePath
??*?? FTP服务器上的相对路径
??* @param fileName
??*?? 要下载的文件名
??* @param localPath
??*?? 下载后保存到本地的路径
??* @return
 */
    public static boolean downFile(String url, int port, String username,
    String password, String remotePath, String fileName,String localPath) {
    boolean result = false;
    try {
    int reply;
    ftpClient.setControlEncoding(encoding);
    ftpClient.connect(url, port);
    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    ftpClient.login(username, password);// 登录
    // 设置文件传输类型为二进制
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
    // 获取ftp登录应答代码
    reply = ftpClient.getReplyCode();
    // 验证是否登陆成功
    if (!FTPReply.isPositiveCompletion(reply)) {
    ftpClient.disconnect();
    System.err.println("FTP server refused connection.");
    return result;
    }
    // 转移到FTP服务器目录至指定的目录下
    ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
    ftpClient.enterLocalPassiveMode(); 
    // 获取文件列表
    FTPFile[] fs = ftpClient.listFiles();
    for (FTPFile ff : fs) {
    if (ff.getName().equals(fileName)) {
    File localFile = new File(localPath + "/" + ff.getName());
    OutputStream is = new FileOutputStream(localFile);
    ftpClient.retrieveFile(ff.getName(), is);
    is.close();
        }
    }
    ftpClient.logout();
    result = true;
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (ftpClient.isConnected()) {
    try {
    ftpClient.disconnect();
    } catch (IOException ioe) {
    }
    }
    }
    return result;
    }
    /**
??  * 将FTP服务器上文件下载到本地
??  * 
??  */
    public void testDownFile() {
    try {
    boolean flag = downFile("132.122.237.xxx", 21, "xxx","xxx", "/home/apache-tomcat-7.0.61_2/webapps/crm/", "test.wav", "E:/");
    System.out.println(flag);
    }catch(Exception e){
        e.printStackTrace();
    }
}
    public static void main(String[] args) {

    FtpApche fa = new FtpApche();
    //下载
    fa.testDownFile();
    //上传
    //fa.testUpLoadFromDisk();
    }

     public static boolean uploadFile(String url, int port, String username,
               String password, String path, String filename, InputStream input) {
              boolean result = false;
              try {
               int reply;
               // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
               ftpClient.connect(url, port);
               // 登录
               ftpClient.login(username, password);
               ftpClient.setControlEncoding(encoding);
               // 检验是否连接成功
               reply = ftpClient.getReplyCode();
               if (!FTPReply.isPositiveCompletion(reply)) {
                System.out.println("连接失败");
                ftpClient.disconnect();
                return result;
               }
               // 转移工作目录至指定目录下
               boolean change = ftpClient.changeWorkingDirectory(path);
               ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
               if (change) {
                ftpClient.enterLocalPassiveMode(); 
                result = ftpClient.storeFile(new String(filename.getBytes(encoding),"iso-8859-1"), input);
                if (result) {
                 System.out.println("上传成功!");
                }
               }
               input.close();
               ftpClient.logout();
              } catch (IOException e) {
               e.printStackTrace();
              } finally {
               if (ftpClient.isConnected()) {
                try {
                 ftpClient.disconnect();
                } catch (IOException ioe) {
                }
               }
              }
              return result;
             }
             /**
              * 将本地文件上传到FTP服务器上
              * 
              */
             public void testUpLoadFromDisk() {
              try {
               FileInputStream in = new FileInputStream(new File("E:/test22.wav"));
               boolean flag = uploadFile("132.122.237.xxx", 21, "xxx","xxx", "/home/apache-tomcat-7.0.61_2/webapps/crm/", "test22.txt", in);
               System.out.println(flag);
              } catch (FileNotFoundException e) {
               e.printStackTrace();
              }
             }
             }

FTP文件上传下载

标签:local   close   oca   url   mode   文件的   err   port   lag   

原文地址:http://blog.51cto.com/1008610086/2350399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!