zoukankan      html  css  js  c++  java
  • Android 平台下Ftp 使用模拟器需要注意的问题

    以下代码在pc上测试通过,可是在android模拟器上就不工作,不过还可以链接到服务器但不能得到文件 纠结了半天,原来是模式的问题,具体请Google 模拟器中采用建议被动模式

    public void getWorkMessage(){  
          
          
        FTPClient ftp = new FTPClient();  
          
        try {  
            ftp.connect(server);  
              
            System.out.println("Connected to " + server);  
            System.out.print(ftp.getReplyString());  
      
            reply = ftp.getReplyCode();  
              
            if(!FTPReply.isPositiveCompletion(reply)) {  
                ftp.disconnect();  
                System.err.println("FTP server refused connection.");  
                System.exit(1);  
             }  
            ftp.login(username, password);  
              
            FTPFile[] files = ftp.listFiles("/");  
              
            if(files!=null&&files.length>0){  
                  
                for(FTPFile f:files){  
                      
                    System.out.println("user:>>>"+f.getUser()+" name:>>>"+f.getName()+" size:>>>"+f.getSize()+" link:>>>"+f.getLink());  
                      
                }  
            }  
        } catch (SocketException e) {  
              
            e.printStackTrace();  
        } catch (IOException e) {  
              
            e.printStackTrace();  
        }  
          
          
    }  

     加上被动模式之后,代码如下:

    public void getWorkMessage(){  
              
              
            FTPClient ftp = new FTPClient();  
              
            try {  
                ftp.connect(server);  
                  
                System.out.println("Connected to " + server);  
                System.out.print(ftp.getReplyString());  
      
                reply = ftp.getReplyCode();  
                  
                if(!FTPReply.isPositiveCompletion(reply)) {  
                    ftp.disconnect();  
                    System.err.println("FTP server refused connection.");  
                    System.exit(1);  
                 }  
                ftp.login(username, password);  
                 
                //设置为被动模式  
               ftp.enterLocalPassiveMode();  
                  
                FTPFile[] files = ftp.listFiles("/");  
                  
                if(files!=null&&files.length>0){  
                      
                    for(FTPFile f:files){  
                          
                        System.out.println("user:>>>"+f.getUser()+" name:>>>"+f.getName()+" size:>>>"+f.getSize()+" link:>>>"+f.getLink());  
                          
                    }  
                }  
            } catch (SocketException e) {  
                  
                e.printStackTrace();  
            } catch (IOException e) {  
                  
                e.printStackTrace();  
            }  
              
              
        }  

    OK,这样就可以了。

  • 相关阅读:
    django从零开始-模板
    django从零开始-模型
    django从零开始-视图
    web基础
    django从零开始-入门
    Pycharm自动添加文件头注释
    django后台密码错误
    module 'sign.views' has no attribute 'search_name'
    TypeError: __init__() got an unexpected keyword argument 't_command'
    pycharm 中的 全局搜索(ctrl+shift+f) 功能无法使用的原因
  • 原文地址:https://www.cnblogs.com/ToFlying/p/4158997.html
Copyright © 2011-2022 走看看