2013-08-06 49 views
0

我想上传捕获的图像从android手机到Ftp服务器我已经写了下面的代码即时通讯面对这条线的错误在这条线后的ftp.store(远程,输入) 即时获取replycode = 550文件unavialable即时通讯使用Apache公用网3.3库我已经尝试
与.net中的WebClient相同它工作正常,但在Java它给了我错误(550)我搜索了许多网站,但没有帮助,我甚至称为托管服务,他们说,从那里边没问题,我从最近1天PLZ stucked纠正我,我是缺少在这里......上传捕获的图像到FTP使用ftpClient在android

我的代码:

private void UploadImage(){ 

    FTPClient ftp = new FTPClient(); 

    try { 
     ftp.connect("hostname",21);// Using port no=21 
     int reply = ftp.getReplyCode(); //here i m getting 220 

     ftp.login("abc", "abc"); 

     reply = ftp.getReplyCode();  //here i m getting 230 

     ftp.setFileType(FTP.BINARY_FILE_TYPE); 



     reply = ftp.getReplyCode(); 

     ftp.enterLocalPassiveMode(); 

     reply = ftp.getReplyCode(); 
     File sfile = new File(PhotoPath); //here i m getting /mnt/sdcard/DCIM/CameraSample/abc769708880.jpg 

     String filename = sfile.getName(); 
     FileInputStream fis = new FileInputStream(sfile); 
     boolean aRtn=ftp.storeFile("ftpPath"+sfile.getName(), fis);// return true if successful 

     reply = ftp.getReplyCode(); // here im getting 550 

     if(aRtn == true) 
     { 
      Toast toast= Toast.makeText(getApplicationContext(), 
        "Successfull", Toast.LENGTH_LONG); 
        toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER, 0, 0); 
        toast.show(); 
     } 
     fis.close();  
     ftp.disconnect(); 

    } catch (SocketException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


} 

回答

0

当您尝试从主线程执行网络操作(如FTP)时,该异常似乎会引发。由于性能方面的原因,这是不允许的(这样应用程序在执行可能需要一段时间的操作时似乎不锁定用户)。假设您使用的是Honeycomb或更高版本,则需要将使连接成为其自己的子线程的代码。

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

http://developer.android.com/guide/practices/design/responsiveness.html

+0

I M使用的AsyncTask上载图片我没有得到这个错误NetworkOnMainThreadException ..... –