0

我正在尝试将音频文件从 sdcard 上传到 ftp 服务器。logcat 显示以下消息..

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



}
4

3 回答 3

1

我不知道这个能不能用。试着让我知道

代码:

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;

            }
于 2012-09-04T10:37:32.023 回答
0

我认为问题出在您的 FTP 连接上。其中说许可被拒绝..

请添加权限

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

在你的清单中

于 2012-09-04T11:00:30.617 回答
0

可能你在编译或测试范围内链接了你的 FTP 库(例如,IDEA 允许这样的技巧),所以它在编译时在这里,但在运行时不存在。您使用哪个 IDE?

于 2012-09-04T10:38:25.357 回答