因此,我对此并不陌生,并且我的应用程序一直崩溃。我在这里找到了这段代码,无法使其一生使用。我想做的只是下载选定的文件,当我不在作业文件夹的子目录中时,它可以工作。我从MySQL数据库下载作业文件夹名称,并且工作正常。每个工作都有用于照片,文档,杂项等的子文件夹。说我有工作4131 ..,而在4131内部有以下文件。

4131 / cdgjkg.png
4131 /其他
4131 /照片

在photots内部还有另一个png。 (4131 / Photos / dioghg.png)。问题是,如果我在base 4131方法中下载png wthin,它将起作用。没有错误。如果我进入Photots目录并尝试下载该文件,则会收到大量异步错误。下面是代码。如果您需要更多信息,请告诉我。

new LoadAllJobs().execute();

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String file = ((TextView)view).getText().toString();
            String current_dir = dir + "/" +  file;
            if(file.contains(".")){
                //download the file or view it
                try {
                    Log.d("Downloading ", file);
                    new FTPDownload().execute(file);  // <-----This is where I start the task to enter the FTPDownload, never to be seen again :(
                    Log.d("Success ", current_dir);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }


这是我在其中检索清单并仅在文件中包含“。”的情况下下载文件的地方。 (在服务器上,文件夹名称中不允许使用'。'字符,因此这是一个快速作弊的功能)。

FTPDownload类

class FTPDownload extends AsyncTask<String, Integer, String> {
//public FTPClient client;

@Override
protected String doInBackground(String... filename) {
    try {
        System.out.println("Filename->: " + filename[0]);
        GetFileFTP("/" + filename[0], Environment.getExternalStorageDirectory() + "/Download/", filename[0]);
    }
    finally
    {
        //e.printStackTrace();
        Log.v("DONE: ", "All done!");
    }
    System.out.println("I HATE YOU.");  // <----- NEVER REACHES THIS LINE WHEN TRYING TO DOWNLOAD IN A SUBFOLDER
    return null;
}



    public void GetFileFTP(String srcFileSpec, String destpath, String destname) {
    FTPClient client = new FTPClient();
    Log.v("pathSpec: ", destpath);

    try {
        client.connect("xxxxxx");
        client.login("xxxxxx", "xxxxxx");

        client.enterLocalPassiveMode();
        client.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);

        InputStream input = client.retrieveFileStream(srcFileSpec);
        if(input != null) Log.v("GetFileFTP: ", "INPUT STREAM OPENED SUCCESSFULLY!");
        Log.v("File InputStream: ", srcFileSpec);

        File output_file = new File(destpath + destname);
        Log.v("OUTPUTFILE: ", destpath + destname);

        inputstreamcopy(input, output_file);

        /*try {
            //Log.e("Closing connection: ", client.toString());
            client.logout();
            client.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }
    catch (IOException e) {
        Log.e("FTP", "Error Getting File");
        e.printStackTrace();
    }
}

    public static void inputstreamcopy(InputStream source, File destination){
        try {
            org.apache.commons.io.FileUtils.copyInputStreamToFile(source, destination);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


控制台输出

04-08 15:10:05.010: D/Downloading(29309): login.php
04-08 15:10:05.010: D/Success(29309): 4131/Miscellaneous/login.php
04-08 15:10:05.010: I/System.out(29309): Filename->: login.php
04-08 15:10:05.020: V/pathSpec:(29309): /storage/emulated/0/Download/
04-08 15:10:06.010: V/File InputStream:(29309): /login.php
04-08 15:10:06.020: V/OUTPUTFILE:(29309): /storage/emulated/0/Download/login.php
04-08 15:10:06.030: V/DONE:(29309): All done!
04-08 15:10:06.030: W/dalvikvm(29309): threadid=11: thread exiting with uncaught exception (group=0x418f5930)
04-08 15:10:06.040: E/AndroidRuntime(29309): FATAL EXCEPTION: AsyncTask #1
04-08 15:10:06.040: E/AndroidRuntime(29309): java.lang.RuntimeException: An error occured while executing doInBackground()
04-08 15:10:06.040: E/AndroidRuntime(29309):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.lang.Thread.run(Thread.java:856)
04-08 15:10:06.040: E/AndroidRuntime(29309): Caused by: java.lang.NullPointerException
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1792)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.FileUtils.copyInputStreamToFile(FileUtils.java:1512)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.inputstreamcopy(FTPDownload.java:70)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.GetFileFTP(FTPDownload.java:51)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.doInBackground(FTPDownload.java:21)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.doInBackground(FTPDownload.java:1)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-08 15:10:06.040: E/AndroidRuntime(29309):    ... 4 more

最佳答案

这是您的罪魁祸首:

04-08 15:10:06.040: E/AndroidRuntime(29309): Caused by: java.lang.NullPointerException
[...]
org.apache.commons.io.FileUtils.copyInputStreamToFile(FileUtils.java:1512)


您将null传递给inputstreamcopy(),这将导致NPE。

10-06 02:50