2012-09-04 2 views
0

sdcard에서 ftp 서버로 오디오 파일을 업로드하려고합니다. 로그 캣은 다음과 같은 메시지 ..android : 파일을 FTP 서버에 업로드 할 수 없습니다.

 09-04 18:47:10.767: W/System.err(12584): java.net.ConnectException: failed to connect to /127.0.0.1 (port 21): connect failed: ECONNREFUSED (Connection refused) 
09-04 18:47:10.777: W/System.err(12584): at libcore.io.IoBridge.connect(IoBridge.java:120) 
09-04 18:47:10.777: W/System.err(12584): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 
09-04 18:47:10.777: W/System.err(12584): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 
09-04 18:47:10.777: W/System.err(12584): at java.net.Socket.connect(Socket.java:849) 
09-04 18:47:10.777: W/System.err(12584): at org.apache.commons.net.SocketClient.connect(SocketClient.java:176) 
09-04 18:47:10.777: W/System.err(12584): at org.apache.commons.net.SocketClient.connect(SocketClient.java:268) 
09-04 18:47:10.777: W/System.err(12584): at com.example.andro.MainActivity$AsyncUpload.ftpUpload(MainActivity.java:56) 
09-04 18:47:10.777: W/System.err(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:91) 
09-04 18:47:10.777: W/System.err(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:1) 
09-04 18:47:10.777: W/System.err(12584): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
09-04 18:47:10.777: W/System.err(12584): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
09-04 18:47:10.777: W/System.err(12584): at java.lang.Thread.run(Thread.java:856) 
09-04 18:47:10.777: W/System.err(12584): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused) 
09-04 18:47:10.787: W/System.err(12584): at libcore.io.Posix.connect(Native Method) 
09-04 18:47:10.787: W/System.err(12584): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) 
09-04 18:47:10.787: W/System.err(12584): at libcore.io.IoBridge.connectErrno(IoBridge.java:133) 
09-04 18:47:10.787: W/System.err(12584): at libcore.io.IoBridge.connect(IoBridge.java:118) 
09-04 18:47:10.787: W/System.err(12584): ... 15 more 
09-04 18:47:10.787: W/dalvikvm(12584): threadid=11: thread exiting with uncaught exception (group=0x2b542210) 
09-04 18:47:10.797: E/AndroidRuntime(12584): FATAL EXCEPTION: AsyncTask #1 
09-04 18:47:10.797: E/AndroidRuntime(12584): java.lang.RuntimeException: An error occured while executing doInBackground() 
09-04 18:47:10.797: E/AndroidRuntime(12584): at android.os.AsyncTask$3.done(AsyncTask.java:278) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.lang.Thread.run(Thread.java:856) 
09-04 18:47:10.797: E/AndroidRuntime(12584): Caused by: java.lang.NullPointerException 
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:471) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:534) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:583) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.quit(FTP.java:794) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:697) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at com.example.andro.MainActivity$AsyncUpload.ftpUpload(MainActivity.java:79) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:91) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:1) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
09-04 18:47:10.797: E/AndroidRuntime(12584): ... 5 more 

내 코드는 보여줍니다

public class MainActivity extends Activity { 


    TextView tv1; 
    Button bt1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tv1 = (TextView) findViewById(R.id.textView1); 
    bt1 = (Button) findViewById(R.id.button1); 
    bt1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(new AsyncUpload().execute() != null) 
      tv1.setText("uploaded"); 
     } 
    }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
    class AsyncUpload extends AsyncTask<String, Void, Void>{  
     public boolean ftpUpload(){ 
      FTPClient con = new FTPClient(); 
      try 
      { 

       con.connect("127.0.0.1"); //here i receive 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("android", "123")) 
       { 
        con.enterLocalPassiveMode(); 
        String data = Environment.getExternalStorageDirectory().getPath() + "audiorecordtest.3gp"; 
        ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); 
        boolean result = con.storeFile("/test.3gp", in); 
        in.close(); 
        if (result) Log.v("upload result", "succeeded"); 
        return true; 
       } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 


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

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



} 
+1

포맷하시기 바랍니다 로그 캣 출력 – njzk2

+1

에 권한

<uses-permission android:name="android.permission.INTERNET"/> 

을 추가하세요 .. 권한이 거부 말한다, 오히려 그 FTPClient 클래스 프로젝트에 포함되어 있지 않습니다. – njzk2

+0

'java.lang.NoClassDefFoundError : org.apache.commons.net.ftp.FTPClient' 문제는 외부 라이브러리를 연결하는 방법입니다. – xandy

답변

1

나는이 사람이 일을하거나 not.Try 알고 해달라고 알려 주시기를

코드 :

protected Void doInBackground(Void... arg0) { 

      Intent intent = new Intent(); 
      final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 
      // configure the notification   
      notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
      notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.uploadprogress); 
      notification.contentIntent = pendingIntent; 
      notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.progressbar); 
      notification.contentView.setTextViewText(R.id.status_text, "Uploading..."); 
      notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false); 

      notificationManager = (NotificationManager) getApplicationContext().getSystemService(
        getApplicationContext().NOTIFICATION_SERVICE); 

      notificationManager.notify(10, notification); 

      final SharedPreferences prefs = PreferenceManager 
      .getDefaultSharedPreferences(getApplicationContext()); 
      UUID uniqueKey = UUID.randomUUID(); 
       fname = uniqueKey.toString(); 
       Log.e("UNIQUE NAME", fname); 
       String hostName = "MY HOST NAME"; 
       String username = "test"; 
       String password = "****"; 
       String location = selectedPath;   
       InputStream in = null; 
       try { 
        Thread upload = new Thread() { 

         @Override 
         public void run() { 

          for (int i = 1; i < 100; i++) { 
           progress=progress+1; 
           notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);        
           // inform the progress bar of updates in progress 
           notificationManager.notify(id, notification);      
           try { 
            Thread.sleep(2000); 
           } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } 
          }  
         } 
        }; 
        upload.run(); 
        FTPClient ftp = new FTPClient(); 
        ftp.connect(hostName); 
        ftp.login(username, password); 

        ftp.setFileType(FTP.BINARY_FILE_TYPE); 
        ftp.enterLocalPassiveMode(); 
        ftp.changeWorkingDirectory("/uploads"); 

        int reply = ftp.getReplyCode(); 
        System.out.println("Received Reply from FTP Connection:" + reply); 

        if (FTPReply.isPositiveCompletion(reply)) { 
         System.out.println("Connected Success"); 
        } 

        File f1 = new File(location); 
        in = new FileInputStream(f1); 

        ftp.storeFile(fname+"."+extension, in); 

        System.out.println("SUCCESS"); 
        System.out.println("Video Title:" +strTitle+" is uploaded successfully"); 
        System.out.println("Video Name:" +fname+" is uploaded successfully"); 
        ftp.logout(); 
        ftp.disconnect(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       StringBuffer inStreamBuf = new StringBuffer(); 
       strUserName = prefs.getString(
         getResources().getString(R.string.username), null); 
       strToken = prefs.getString(
         getResources().getString(R.string.token), null); 
       url = prefs.getString("VALUE", null); 
       try {     
         inStreamBuf = XmlUtil.getUploadAuthResponse(url, 
           strUserName, strToken, strType, strTitle, 
           fname, strDesc, strCatId, strTags, strAccess, 
           strComments, strEmbed, strRating, strPublish); 
         strXmlResponse = inStreamBuf.toString(); 
         Log.e("Response:", strXmlResponse); 
         DocumentBuilder db = DocumentBuilderFactory 
           .newInstance().newDocumentBuilder(); 
         InputSource is = new InputSource(); 
         is.setCharacterStream(new StringReader(strXmlResponse)); 
         Document doc = db.parse(is); 
         NodeList nodes = doc 
           .getElementsByTagName(getResources().getString(
             R.string.xmlResponse)); 
         Element element = (Element) nodes.item(0); 
         NodeList nl_request_type = element 
           .getElementsByTagName(getResources().getString(
             R.string.response)); 
         Element el_request_type = (Element) nl_request_type 
           .item(0); 
         String strType = getCharacterDataFromElement(el_request_type); 
         // System.out.println("Response----------->"+strType); 
         if (strType.equalsIgnoreCase(getResources().getString(
           R.string.success))) { 
          finish(); 
          Intent intent1 = new Intent(NewVideoActivity.this, 
            MainscreenActivity.class); 
          startActivity(intent); 
         } else if (strType.equalsIgnoreCase(getResources() 
           .getString(R.string.expired))) { 
          XmlUtil.session = null; 
          Intent intent2 = new Intent(NewVideoActivity.this, 
            MainscreenActivity.class); 
          startActivity(intent); 
          finish(); 
          System.exit(0); 
         }           
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       notificationManager.cancel(10); 
       return null; 

      } 
0

아마도 당신은 컴파일이나 테스트 범위에서 FTP 라이브러리를 링크하고 있습니다 (예 : IDEA는 그런 트릭을 허용합니다). 런타임에 부재중. 어느 IDE를 사용합니까?

+0

이클립스 JUNO JEE 버전 – user1624424

0

FTP 연결에 문제가 있다고 생각합니다. 어떤 문제가 파일을 업로드하지 않는 것입니다,

도 매니페스트

+0

여전히 사용 권한을 추가하지 않으 셨습니다. – user1624424

+0

127.0.0.1 또는 localhost를 추가하는 대신 ok를 사용하여 192.168.0.1과 같은 실제 IP 주소를 사용하십시오. – NullPointerException

관련 문제