2011-07-19 26 views
5

me sale este error después de que he puesto INTERNET_ACCESS y etc ...FTP cargar y descargar en Android

private class AsyncUpload extends AsyncTask<String, Void, Void>{  
    public void ftpUpload(){ 
     FTPClient con = new FTPClient(); 
     try 
     { 

      con.connect("ftp.194.90.81.149"); //here i recieve exception 
       //the exception is java.unknownhostexception 
       //java.net.UnknownHostException: Unable to resolve host "ftp.194.90.81.149": No address associated with hostname 
      if (con.login("username", "password")) 
      { 
       con.enterLocalPassiveMode(); 
       String data = "test data"; 
       ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); 
       boolean result = con.storeFile("/test.jpg", in); 
       in.close(); 
       if (result) Log.v("upload result", "succeeded"); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 


     try 
     { 
      con.logout(); 
      con.disconnect(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected Void doInBackground(String... params) { 
     ftpUpload(); 
     return null; 
    } 
} 

aquí es otra parte del código que he probado y siguen recibiendo esa excepción

public class FTPFactory { 
private FTPClient _ftpClient = null; 

public boolean Connect(String host, String userName, String password, int port) throws IOException 
{ 
    try { 

     _ftpClient = new FTPClient(); 

     // connecting to the host   
     _ftpClient.connect(host, port); 

     // now check the reply code, if positive mean connection success 
     if (FTPReply.isPositiveCompletion(_ftpClient.getReplyCode())) {    
      // login using username & password 
      boolean status = _ftpClient.login(userName, password); 
      return status; 
     } 

    } catch(IOException e) { 
     throw e; 
    }  
    return false; 
} 

public boolean Disconnect() 
{ 
    try { 
     _ftpClient.logout(); 
     _ftpClient.disconnect(); 
     return true; 
    } catch (Exception e) { 

    } 

    return false; 
} 

public boolean ChangeDirectory(String directoryPath) 
{ 
    try { 
     _ftpClient.changeWorkingDirectory(directoryPath); 
    } catch(Exception e) { 

    } 

    return false; 
} 


public String GetCurrentWorkingDirectory() 
{ 
    try { 
     String workingDir = _ftpClient.printWorkingDirectory(); 
     return workingDir; 
    } catch(Exception e) { 

    } 

    return null; 
} 

public void PrintFilesList(String dirPath) 
{ 
    try { 
     FTPFile[] ftpFiles = _ftpClient.listFiles(dirPath); 
     int length = ftpFiles.length; 

     for (int i = 0; i < length; i++) { 
      String name = ftpFiles[i].getName(); 
      boolean isFile = ftpFiles[i].isFile(); 

      if (isFile) { 

      } 
      else { 

      } 
     } 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

public boolean MakeDirectory(String newDirPath) 
{ 
    try { 
     boolean status = _ftpClient.makeDirectory(newDirPath); 
     return status; 
    } catch(Exception e) { 

    } 
    return false; 
} 


public boolean RemoveDirectory(String dirPath) 
{ 
    try { 
     boolean status = _ftpClient.removeDirectory(dirPath); 
     return status; 
    } catch(Exception e) { 

    } 
    return false; 
} 

public boolean RemoveFile(String filePath) 
{ 
    try { 
     boolean status = _ftpClient.deleteFile(filePath); 
     return status; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return false; 
} 

public boolean RenameFile(String from, String to) 
{ 
    try { 
     boolean status = _ftpClient.rename(from, to); 
     return status; 
    } catch (Exception e) { 

    } 
    return false; 
} 


/** 
* mFTPClient: FTP client connection object (see FTP connection example) 
* srcFilePath: path to the source file in FTP server 
* desFilePath: path to the destination file to be saved in sdcard 
*/ 
public boolean Download(String srcFilePath, String desFilePath) 
{ 
    boolean status = false; 
    try { 
     FileOutputStream desFileStream = new FileOutputStream(desFilePath);; 
     status = _ftpClient.retrieveFile(srcFilePath, desFileStream); 
     desFileStream.close(); 

     return status; 
    } catch (Exception e) { 

    } 

    return status; 
} 

/** 
* mFTPClient: FTP client connection object (see FTP connection example) 
* srcFilePath: source file path in sdcard 
* desFileName: file name to be stored in FTP server 
* desDirectory: directory path where the file should be upload to 
*/ 
public boolean Upload(String srcFilePath, String desFileName, String desDirectory) 
{ 
    boolean status = false; 
    try { 
     FileInputStream srcFileStream = new FileInputStream(srcFilePath); 

     // change working directory to the destination directory 
     if (ChangeDirectory(desDirectory)) { 
      status = _ftpClient.storeFile(desFileName, srcFileStream); 
     } 

     srcFileStream.close(); 
     return status; 
    } catch (Exception e) { 

    } 

    return status; 
}  

}

+0

Pensé que el formato URL de FTP no estaba en el camino correcto, compruébalo una vez – sunriser

+0

Deberías publicar el stacktrace de tu problema. Pero parece que le has dado una mala cadena de conexión a tu FtpClient. –

+0

He probado todas las variantes posibles ¿me pueden ayudar, por favor? – IamStalker

Respuesta

7
 con.connect("ftp.194.90.81.149"); //here i recieve exception 
      //the exception is java.unknownhostexception 
      //java.net.UnknownHostException: Unable to resolve host "ftp.194.90.81.149": No address associated with hostname 

El hecho de que esté recibiendo una excepción UnknownHost significa que ftp.194.90.81.149 no es un nombre de host real en DNS. Sospecho que la parte numérica de eso es lo que realmente quieres. Es decir, tratar de cambiar esa línea para

con.connect("194.90.81.149"); 
+0

He probado esta variante y todavía no hay ningún resultado. ¿Cómo puedo saber si estoy haciendo todo bien? – IamStalker

+0

¿Aparece un nuevo error? –

+0

Ohh Don en primer lugar muchas gracias por su ayuda, Y sobre el error que recibo el java.net ... .UnkownHostException .... tal vez te envíe la aplicación y puedes ver lo que soy haciendo mal? – IamStalker

0

excepción Host desconocido significa o bien se echa en falta el permiso de Internet o no está conectado a Internet, o puede ser el URL de FTP es erróneo.

+0

Eso es implícito. – IamStalker

+0

¿Se resolvió su problema? – sandy

+0

Tengo que buscar algunos videos de ftp locaton y mi enlace parece "ftp: ///xx.xx.xx" en lugar de ftp.xx.xx. – sandy

Cuestiones relacionadas