2016-01-23 52 views
0

我製作了一個Android應用程序,用於從服務器下載文件並將其保存到內部存儲器的目錄中。首先代碼驗證鏈​​接是否存在以及鏈接是否存在,然後下載文件。該文件正在下載,但當我去文件夾,看看文件是否存在,文件存在,但這不是我放在服務器上的文件。一切正常的的AsyncTaskAndroid-文件從服務器下載,但使用不同的文件名和擴展名保存

這裏去我的代碼裏面:

String fileName = "abc.pdf"; 
     try { 
      URL url = new URL(f_url[0]); 
      URLConnection conection = url.openConnection(); 
      conection.connect(); 

      // this will be useful so that you can show a typical 0-100% 
      // progress bar 
      int lenghtOfFile = conection.getContentLength(); 

      // download the file 
      InputStream input = new BufferedInputStream(url.openStream(), 
        8192); 

      // Output stream 
      OutputStream output = new FileOutputStream(Environment 
        .getExternalStorageDirectory().toString() 
        + "/MyApp/"); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 

       // writing data to file 
       output.write(data, 0, count); 
      } 

      // flushing output 
      output.flush(); 

      // closing streams 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 

     return null; 
    } 
+0

使用:https://github.com/smanikandan14/ThinDownloadManager –

+0

'的OutputStream輸出=新FileOutputStream中(環境 .getExternalStorageDirectory()的toString() +「/ MyApp的/「);'。當/ MyApp /是一個文件夾時,你根本沒有文件名。當然,你可以放任何你想要的文件名:OutputStream output = new FileOutputStream(環境 .getExternalStorageDirectory()。toString() +「/MyApp/myfilename.jpg」); – greenapps

回答

1

一次我也患上這個問題,並在最後我做一個通用的AsyncTask類,這是剛剛從服務器上下載文件並存儲在本地文件夾中。在這段代碼片段我總是得到MP3文件從服務器,所以我靜態設置.mp3格式。您可以更改此代碼根據您的要求。

@Override 
protected File doInBackground(Void... params) { 


    //File sdCardRoot = Environment.getExternalStorageDirectory()+"/Music"; 

    File file = new File(Environment.getExternalStorageDirectory() + "/" + ConstansClass.FOLDERNAME); 

    if (!file.exists()) { 
     file.mkdir(); 
    } 


    String filename = "YOUR LOCAL STORAGE FILE NAME TITLE"; 
    yourDir = new File(file, filename + ".mp3"); 
    if (yourDir.exists()) { 
     return yourDir; 
    } 


    String url = "YOUR FILE DOWNLOADING URL"; 

    URL u = null; 
    try { 
     DebugLog.e("Request Url" + url); 
     u = new URL(url); 
     URLConnection conn = u.openConnection(); 
     int contentLength = conn.getContentLength(); 

     DataInputStream stream = new DataInputStream(u.openStream()); 

     byte[] buffer = new byte[contentLength]; 
     stream.readFully(buffer); 
     stream.close(); 

     DataOutputStream fos = new DataOutputStream(new FileOutputStream(yourDir)); 
     fos.write(buffer); 
     fos.flush(); 
     fos.close(); 
     DebugLog.d("Download Complete in On Background"); 


    } catch (MalformedURLException e) { 
     sucess = false; 
     e.printStackTrace(); 

    } catch (IOException e) { 
     sucess = false; 
     e.printStackTrace(); 

    } catch (Exception e) { 
     sucess = false; 
     e.printStackTrace(); 
     DebugLog.e("Error ::" + e.getMessage()); 
    } 
    return yourDir; 
} 

參數

  • :您的服務器的鏈接,您可以下載文件:存儲在本地文件夾
  • 網址你的文件名。

注意

  • 請確保您下載的文件URL是完美的。
  • 您必須在Manifest中讀取寫入和Internet權限。

我希望你清楚我的想法。 謝謝,

最佳勒克

+0

即使這樣也行不通。現在文件夾中沒有文件。 –

+0

請使用此後發佈您的代碼。 –

相關問題