所以我对此很陌生,我的应用程序不断崩溃。我在这里找到了这段代码,但无法让它为我的一生工作。我要做的只是下载选定的文件,当我不在工作文件夹的子目录中时它可以工作。我从 MySQL 数据库下载作业文件夹名称,效果很好。每个工作都有用于照片、文档、杂项等的子文件夹......假设我有工作 4131.. 并且在 4131 里面有以下......
4131/cdgjkg.png 4131/杂项 4131/照片
在photots里面还有另一个png。(4131/照片/dioghg.png)。问题是,如果我使用基本 4131 方法下载 png ,它就可以工作。没有错误。如果我进入 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();
}
这是一个块,我已经检索了列表并仅在文件包含“。”时才下载文件。(服务器上的文件夹名称中不允许使用“.”字符,因此这是一种快速作弊)。
FTP下载类
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