用于FTP上载的文件到inputStream返回NullPointerExeption

时间:2014-12-16 06:32:19

标签: android ftp nullpointerexception inputstream fileinputstream

我正在尝试将文件上传到FTP,但输入操作流有问题。它返回java.lang.NullPointerException。

如何将文件加载到inputSteam?

我也没有进展报告。永远不会执行onProgressUpdate,为什么?

我的代码

private class AsyncFTP extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... params) {

            publishProgress("Entro");

            //Creo FTPClient
            FTPClient FTP = new FTPClient();
            //Defino datos del servidor
            String FTP_HOST = "NOT REAL IP";
            String FTP_USER = "NOT REAL USER";
            String FTP_PASS = "NOT REAL PASS";

            //Cargo el archivo
            File file = new File("/data/data/com.lstupenengo.mysql/files/xx.xx");
            FileInputStream FIS = null;
            try {
                FIS = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            publishProgress("File ");
            //Conectar
            try{
                publishProgress("Conectando");
                FTP.connect(FTP_HOST, 21);
                FTP.login(FTP_USER, FTP_PASS);
                publishProgress("Conecto " + String.valueOf(FTP.getReplyCode()));
                FTP.type(FTPClient.BINARY_FILE_TYPE);


                //Directorio
                FTP.changeWorkingDirectory("/public_html/");

                publishProgress("Directorio"+String.valueOf(FTP.getReplyCode()));
                FTP.enterLocalPassiveMode();
                FTP.storeFile("xx.xx",FIS);
                publishProgress("Subio "+String.valueOf(FTP.getReplyCode()));



            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(String txt) {
            TV.setText(TV.getText()+"\n"+txt);
        }

        @Override
        protected void onPostExecute(String result){
            TV.setText(TV.getText()+"\n Termino FTP");
        }
    }

logcat的

 OpenGL ES Shader Compiler Version: 17.01.12.SPL
    Build Date: 07/01/14 Tue
    Local Branch:
    Remote Branch: quic/kk_2.7_rb1.41
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.04.007.064 +  NOTHING
12-16 08:12:46.964    9262-9262/com.lstupenengo.mysql D/OpenGLRenderer﹕ Enabling debug mode 0
12-16 08:12:47.084    9262-9262/com.lstupenengo.mysql I/ActivityManager﹕ Timeline: Activity_idle id: android.os.BinderProxy@44bd9f08 time:324929151
12-16 08:12:55.142    9262-9262/com.lstupenengo.mysql W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
12-16 08:12:57.344    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND rw
12-16 08:12:57.534    9262-9262/com.lstupenengo.mysql I/ActivityManager﹕ Timeline: Activity_idle id: android.os.BinderProxy@44bd9f08 time:324939608
12-16 08:12:57.885    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND ro
12-16 08:12:57.995    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND rw
12-16 08:12:58.115    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, relatime] AND rw
12-16 08:12:58.215    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [ro, relatime] AND ro
12-16 08:12:58.756    9262-9400/com.lstupenengo.mysql I/RootTools v3.1﹕ [rw, seclabel, nosuid, nodev, relatime, discard, noauto_da_alloc, data=ordered] AND ro
12-16 08:12:58.776    9262-9400/com.lstupenengo.mysql W/System.err﹕ java.io.FileNotFoundException: /data/data/com.lstupenengo.mysql/files/wa.db: open failed: EACCES (Permission denied)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:418)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:97)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:76)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.Posix.open(Native Method)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:402)
12-16 08:12:58.786    9262-9400/com.lstupenengo.mysql W/System.err﹕ ... 9 more
12-16 08:13:00.577    9262-9400/com.lstupenengo.mysql W/dalvikvm﹕ threadid=15: thread exiting with uncaught exception (group=0x41a32d88)
12-16 08:13:00.587    9262-9400/com.lstupenengo.mysql E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5
    Process: com.lstupenengo.mysql, PID: 9262
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at org.apache.commons.net.io.Util.copyStream(Util.java:100)
            at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
            at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
            at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)
            at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:117)
            at com.lstupenengo.mysql.MainActivity$AsyncFTP.doInBackground(MainActivity.java:76)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:1)

你的onProgressUpdate没有覆盖,它需要作为参数(Params ... param)而不是单个String。

这是定义。

protected void onProgressUpdate (Progress... values)

所以你需要

protected void onProgressUpdate (String... values)

如果只在publishProgress中放入一个字符串参数,则使用值[0]获取字符串。

另一方面,关于你的异常,没有堆栈跟踪很难说,但在存储文件之前检查你的FIS是否为空。 NullPointers通常很容易修复。无论如何,你有一个small tutorial here