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

  • 相关阅读:
    Interview with BOA
    Java Main Differences between HashMap HashTable and ConcurrentHashMap
    Java Main Differences between Java and C++
    LeetCode 33. Search in Rotated Sorted Array
    LeetCode 154. Find Minimum in Rotated Sorted Array II
    LeetCode 153. Find Minimum in Rotated Sorted Array
    LeetCode 75. Sort Colors
    LeetCode 31. Next Permutation
    LeetCode 60. Permutation Sequence
    LeetCode 216. Combination Sum III
  • 原文地址:https://www.cnblogs.com/ToFlying/p/4158997.html
Copyright © 2020-2023  润新知