0

我经历了很多关于从 ftp 服务器上传文件的讨论。我认为有两种连接和上传方式:

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 的初学者,我只知道基础知识,实现相同目标的正确方法是什么?

4

1 回答 1

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 库后使用此代码。关联

于 2013-04-05T08:21:11.857 回答