2013-04-05 107 views
0

服务器我经历了许多讨论了有关从FTP上传文件server.I认为有2种方式来连接和上传:如何连接到FTP从Android的

1:我必须使用Apache库,并使用它的类像ftpclient。

2:像这样的代码的URLConnection:

public void getFile(URL u) throws IOException { 
     // url de type "http://www.monsite.com/monrep/mavideo.wmv" 
     String FileName = u.getFile(); 
     FileName = FileName.substring(FileName.lastIndexOf('/') + 1); 
     URLConnection uc = u.openConnection(); 
     int FileLenght = uc.getContentLength(); 
     if (FileLenght == -1) { 
      monView2.setText("Fichier non valide:"+ FileName); 
     } 

     try 
     { 
      InputStream myInput = uc.getInputStream(); 
      String outFileName = "/sdcard/GPTO/"+ NomParcours + "/" + FileName; 
      FileOutputStream myOutPut = new FileOutputStream(outFileName); 
      byte[]buff = new byte[1024]; 
      int l = myInput.read(buff); 

      while(l>0) 
      { 
       myOutPut.write(buff, 0, l); 
       l = myInput.read(buff); 
      } 
      myOutPut.flush(); 
      myOutPut.close(); 
     } 
     catch(Exception e) 
     { 
     monView2.setText(e.toString()); 
     } 
} 

请直接与我在正确的道路,我在Android的初学者,我知道刚才基础知识,什么是达到同样的路吗?

+0

不硬编码sdcard路径'String outFileName =“/ sdcard'。使用'Environment'类获取有效路径 – 2013-04-05 08:23:30

回答

0
ftpClient.connect(InetAddress.getByName(server)); 
ftpClient.login(user, password); 
ftpClient.changeWorkingDirectory(serverRoad); 
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
BufferedInputStream buffIn=null; 
buffIn=new BufferedInputStream(new FileInputStream(file)); 
ftpClient.enterLocalPassiveMode(); 
ftpClient.storeFile("mysampletext.txt", buffIn); 
buffIn.close(); 
ftpClient.logout(); 
ftpClient.disconnect(); 

导入ftp库后使用此代码。 link

+0

hy Arun C Thomas,我用你的例子但是方法ftpclient.login()返回false为什么? – 2013-04-17 09:10:48