当我尝试从FTP下载文件时抛出NullPointerException

时间:2013-10-13 12:17:54

标签: java android ftp inputstream

我正在尝试从FTP顺序下载两个文件。第一个文件下载没有任何问题。但是当InputStream使用ftpClient.retrieveFileStream()进行初始化时,第二次抛出NullPointerException。我试图改变序列,但它没有产生任何影响。文件在FTP上存在且可用。

CODE

FTPClient ftpClient;

    @Override
    protected void onPreExecute() {
        String server = "xxxxxxxxxxxxxxxxxxxxx";
        int port = 21;
        String user = "xxxxxx";
        String pass = "xxxxxx";
        long fileSize = 0;

        try {
            ftpClient = new FTPClient();
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            fileSize += getFileSize(ftpClient, CARDS_DB);
            fileSize += getFileSize(ftpClient, IMG_DB);
            showDialog(PROGRESS_DLG_ID);
            downloadProgress.setMax((int) fileSize);
        } catch (Exception e) {
            Log.e("ERROR", e.toString());
        }
    }

    @Override
    protected Boolean doInBackground(String... strings) {

        final String PATH = getExternalCacheDir().toString() + "/";
        int total = 0;

        try {
            downloadFile(ftpClient, PATH, CARDS_DB, total);
            downloadFile(ftpClient, PATH, IMG_DB, total);
        } catch (Exception e) {
            Log.e("EXCEPTION", e.toString());
            return false;
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        return true;
    }

    private void downloadFile(FTPClient ftpClient, String PATH, String fileName, int total) {
        try {
            File downloadFile = new File(PATH + fileName);
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
            InputStream inputStream;
            inputStream = ftpClient.retrieveFileStream(fileName); //here i have exception at second method invocation.

            byte[] bytesArray = new byte[8192];
            int bytesRead;
            while ((bytesRead = inputStream.read(bytesArray)) != -1) {
                total += bytesRead;
                outputStream.write(bytesArray, 0, bytesRead);
                publishProgress(total);
            }
            outputStream.close();
            inputStream.close();

        } catch (Exception ex) {
            Log.d("ERROR", "Error: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

logcat的:

10-13 14:51:29.519 W/System.err(9554): java.lang.NullPointerException
10-13 14:51:29.529 W/System.err(9554):  at com.FHS.ActivityMain$DBLoad.downloadFile(ActivityMain.java:170)
10-13 14:51:29.529 W/System.err(9554):  at com.FHS.ActivityMain$DBLoad.doInBackground(ActivityMain.java:141)
10-13 14:51:29.529 W/System.err(9554):  at com.FHS.ActivityMain$DBLoad.doInBackground(ActivityMain.java:102)
10-13 14:51:29.529 W/System.err(9554):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-13 14:51:29.529 W/System.err(9554):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-13 14:51:29.529 W/System.err(9554):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-13 14:51:29.529 W/System.err(9554):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-13 14:51:29.529 W/System.err(9554):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-13 14:51:29.529 W/System.err(9554):  at java.lang.Thread.run(Thread.java:1019)

解决 问题解决了。我只需要在方法downloadFile()的每次调用时初始化FTPClient并在方法内打开/关闭连接。

2 个答案:

答案 0 :(得分:0)

我认为问题在于这行代码:

final String PATH = getExternalCacheDir().toString() + "/";

因为方法getExternalCacheDir()仅返回缓存目录,并且仅当缓存不为空时。如果没有缓存,则没有目录。

您可以改用getExternalFilesDir()

参考:

编辑:请将代码修改为将ftpClient初始化为:

int reply;
ftp.connect(server,port);
reply = ftp.getReplyCode();

并查看回复代码。

答案 1 :(得分:0)

我认为您的解决方案正在掩盖您的问题,即您在关闭流之前没有在downloadFile()中调用ftpClient.completePendingCommand()。