android 下的ftp注意问题

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

 

	public void doWork(){
		
		
		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 doWork(){
		
		
		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();
		}
		
		
	}

 终于工作了...

你可能感兴趣的:(android,工作,Google,F#)