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,这样就可以了。

posted @ 2014-12-12 09:05  Follow-your-heart  阅读(429)  评论(0编辑  收藏  举报