2012-11-07 65 views
2

此代碼現在正在運行。 LogCat只適用於從其他引導回來的問題pplAndroid:如何下載一個ftp文件而不被破壞?

如何從我的ftp服務器下載文件而不損壞(下載的文件)? 我可以下載該文件,但它有0字節....

但是所有相同的.txt,.png,.exe,.apk文件在我的手機上每次損壞/不可讀(0字節)。 例如,我的ftp服務器上的一個名爲init.txt的文件(其中包含以下文本:「testfileini !!」),其大小爲13個字節的ftp和0個字節的電話​​。

OnItemClickListener(用於直接選擇從FTP文件列表中的文件)

lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       String file = (String) lv.getItemAtPosition(position); 
       new DownloadFile().execute(file); 
      } 

     }); 

的AsyncTask Downloadfile(用於在後臺下載)

private class DownloadFile extends AsyncTask<String, Integer, String> { 
    @Override 
    protected String doInBackground(String... filename) { 
     try { 
      System.out.println("Filename->: " + filename[0]); 
      GetFileFTP("/subdomains/giveyourapps/httpdocs/apps/"+filename[0], Environment.getExternalStorageDirectory() + "/Download/", filename[0]); 

     } finally { 
      disconnectFTP(); 
     } 
     return null; 
    } 
} 

GetFileFTP

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

    try { 
     client.connect("176.28.25.46"); 
     client.login("ftp031220", "hSigSf"); 

     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("Fileinputstream: ", srcFileSpec); 

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

     inputstreamcopy(input, output_file); 

     disconnectFTP(); 

    }catch (IOException e) { 
      Log.e("FTP", "Error Getting File"); 
      e.printStackTrace(); 
    } 
} 

copyInputStreamToFile

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

logcat的

11-07 15:09:08.675: D/GestureDetector(15746): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0 
11-07 15:09:08.905: I/System.out(15746): status :: 211-Status of 'ProFTPD' 

11-07 15:09:08.905: I/System.out(15746): Connected from 109.164.221.167 (109.164.221.167) 

11-07 15:09:08.905: I/System.out(15746): Logged in as ftp031220 

11-07 15:09:08.905: I/System.out(15746): TYPE: BINARY, STRUcture: File, Mode: Stream 

11-07 15:09:08.905: I/System.out(15746): No data connection 

11-07 15:09:08.910: I/System.out(15746): 211 End of status 

11-07 15:09:09.025: D/dalvikvm(15746): GC_CONCURRENT freed 93K, 10% free 12464K/13703K, paused 15ms+12ms, total 58ms 
11-07 15:09:09.390: I/System.out(15746): Files->: 1Tap_Cleaner_Pro_v1.45.apk 
11-07 15:09:09.390: I/System.out(15746): Files->: AppBrain_App_Market_v7.5.apk 
11-07 15:09:09.395: I/System.out(15746): Files->: Apparatus_v1.0.4_ADFREE_ANDROiD-P2P.apk 
11-07 15:09:09.395: I/System.out(15746): Files->: Atomic_Bomber_5.0.apk 
11-07 15:09:09.395: I/System.out(15746): Files->: IMG_20120917_120935.jpg 
11-07 15:09:09.395: I/System.out(15746): Files->: init.txt 
11-07 15:09:09.395: I/System.out(15746): Files->: unnamed.png 
11-07 15:09:09.500: E/SpannableStringBuilder(15746): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
11-07 15:09:09.500: E/SpannableStringBuilder(15746): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
11-07 15:09:13.805: D/GestureDetector(15746): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0 
11-07 15:09:13.880: I/System.out(15746): Filename->: init.txt 
11-07 15:09:13.880: V/pathSpec:(15746): /storage/sdcard0/Download/ 
11-07 15:09:14.310: V/GetFileFTP:(15746): Input Stream Opened successfully 
11-07 15:09:14.310: V/Fileinputstream:(15746): /subdomains/giveyourapps/httpdocs/apps/init.txt 
11-07 15:09:14.310: V/OUTPUTFILE:(15746): /storage/sdcard0/Download/outputfile.txt 
11-07 15:09:14.310: I/dalvikvm(15746): threadid=11: stack overflow on call to Lcom/mseiz/give/your/apps/MainSite;.copyInputStreamToFile:VLL 
11-07 15:09:14.310: I/dalvikvm(15746): method requires 8+20+8=36 bytes, fp is 0x5ec4b310 (16 left) 
11-07 15:09:14.310: I/dalvikvm(15746): expanding stack end (0x5ec4b300 to 0x5ec4b000) 
11-07 15:09:14.315: I/dalvikvm(15746): Shrank stack (to 0x5ec4b300, curFrame is 0x5ec4eeb0) 
11-07 15:09:14.315: W/dalvikvm(15746): threadid=11: thread exiting with uncaught exception (group=0x40d9f2a0) 
11-07 15:09:14.375: D/dalvikvm(15746): GC_CONCURRENT freed 283K, 10% free 12654K/14023K, paused 14ms+11ms, total 47ms 
11-07 15:09:14.405: D/dalvikvm(15746): GC_FOR_ALLOC freed 314K, 11% free 12682K/14215K, paused 10ms, total 10ms 
11-07 15:09:14.405: E/AndroidRuntime(15746): FATAL EXCEPTION: AsyncTask #1 
11-07 15:09:14.405: E/AndroidRuntime(15746): java.lang.RuntimeException: An error occured while executing doInBackground() 
11-07 15:09:14.405: E/AndroidRuntime(15746): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at java.lang.Thread.run(Thread.java:856) 
11-07 15:09:14.405: E/AndroidRuntime(15746): Caused by: java.lang.StackOverflowError 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(MainSite.java:119) 
11-07 15:09:14.405: E/AndroidRuntime(15746): at com.mseiz.give.your.apps.MainSite.copyInputStreamToFile(Ma 
11-07 15:09:14.420: D/dalvikvm(15746): GC_CONCURRENT freed 553K, 12% free 12644K/14343K, paused 2ms+2ms, total 12ms 
11-07 15:09:14.420: D/dalvikvm(15746): WAIT_FOR_CONCURRENT_GC blocked 9ms 
11-07 15:09:14.440: D/dalvikvm(15746): GC_CONCURRENT freed 324K, 11% free 12802K/14343K, paused 1ms+2ms, total 17ms 

回答

2

這條線:

readBytes(input); 

不執行任何操作。您需要將那些返回的字節寫入FileOutputStream。順便說一句,考慮使用org.apache.commons.io.IOUtils.copy(InputStream, OutputStream)或類似的。如果下載的文件足夠大,將字節數組存儲在內存中可能會導致OutOfMemoryErrors

UPDATE:
更妙的是:
org.apache.commons.io.FileUtils.copyInputStreamToFile(InputStream,File)

+0

我怎樣才能添加此我的代碼? 'File input_file = new File(destpath + destname); copyInputStreamToFile(輸入,INPUT_FILE);'到GetFileFTP 和'公共靜態無效copyInputStreamToFile(InputStream的源, 文件目的地){ \t \t copyInputStreamToFile(源,目的地); }' –

+0

是的,你就是這麼做的。提示:文件參數是輸出文件,它是從ftp下載的文件,所以'output_file'是該變量的更好名稱。 –

+0

現在我得到了一個錯誤...我會發布新的情況下,我的問題下的當前代碼... –

相關問題