2011-07-19 8 views
5

पर मैं के बाद मैं INTERNET_ACCESS सेट और आदि है इस त्रुटि मिलती है ...एफ़टीपी अपलोड और डाउनलोड एंड्रॉयड

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; 
    } 
} 

यहाँ कोड का एक और हिस्सा मैं परीक्षण किया है और अभी भी है कि अपवाद

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

मैंने सोचा कि एफ़टीपी यूआरएल प्रारूप सही तरीके से नहीं है, इसे एक बार जांचें – sunriser

+0

आपको अपनी समस्या का स्टैकट्रैक पोस्ट करना चाहिए। लेकिन ऐसा लगता है कि आपने अपने FtpClient को एक खराब कनेक्शन स्ट्रिंग दी है। –

+0

मैंने हर प्रकार का परीक्षण किया है, क्या आप कृपया मेरी मदद कर सकते हैं? – IamStalker

उत्तर

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 

तथ्य यह है कि आपको अज्ञातहोस्टएक्सप्शन प्राप्त हो रहा है इसका अर्थ है कि ftp.194.90.81.149 DNS में वास्तविक होस्टनाम नहीं है। मुझे संदेह होगा कि इसका संख्यात्मक हिस्सा वह है जो आप वास्तव में चाहते हैं। यानी, का मतलब है

con.connect("194.90.81.149"); 
+0

मैंने इस संस्करण का परीक्षण किया है और अभी भी कोई परिणाम नहीं है। मुझे कैसे पता चलेगा कि मैं हर चीज सही कर रहा हूं? – IamStalker

+0

क्या आपको कोई नई त्रुटि मिलती है? –

+0

ओह डॉन सबसे पहले आपकी मदद के लिए बहुत बहुत धन्यवाद, और त्रुटि के बारे में मुझे java.net प्राप्त होता है ...। UnkownHostException .... शायद मैं आपको एप्लिकेशन भेजूंगा और आप देख सकते हैं कि मैं क्या कर रहा हूं गलत कर रहे हो – IamStalker

0

अज्ञात होस्ट अपवाद है कि लाइन को बदलने के लिए या तो आप इंटरनेट अनुमति याद कर रहे हैं या आप इंटरनेट से कनेक्ट नहीं कर रहे हैं या हो सकता है आपके FTP URL गलत है की कोशिश करो।

+0

यह स्वयं निहित है। – IamStalker

+0

क्या आपकी समस्या हल हो गई है? – sandy

+0

मुझे ftp locaton से कुछ वीडियो लाने होंगे और मेरा लिंक ftp.xx.xx के बजाय "ftp: ///xx.xx.xx" जैसा प्रतीत होता है। – sandy

संबंधित मुद्दे