2014-02-05 76 views
0

我有幾個文件上傳到我的服務器,現在我想要做的是我想要一個一個地下載所有文件並將其保存到一個文件夾中。如何從服務器下載所有文件到SD卡

所以基本上我試圖做一些像[這] [1],但一些如何我無法實現這一點,所以我想一個替代方法來做到這一點。所以我打算將服務器的特定文件夾的所有音頻文件下載到我的SD卡文件夾中,然後我會給SD卡的路徑列表視圖並播放該音頻。

所以基本上我的問題是我怎麼能從服務器文件夾下載所有文件到我的SD卡文件夾。

我有下面的一段代碼工作正常單個文件。

public class ServerFileList extends Activity { 

    Uri uri; 
    URL urlAudio; 
    ListView mListView; 
    ProgressDialog pDialog; 
    private MediaPlayer mp = new MediaPlayer(); 
    private List<String> myList = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.serverfilelist); 

     mListView = (ListView) findViewById(R.id.listAudio); 
     // new getAudiofromServer().execute(); 
     new downloadAudio().execute(); 

     mListView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       playSong(urlAudio + myList.get(position)); 
      } 
     }); 
    } 

    public void updateProgress(int currentSize, int totalSize) { 
     TextView mTextView = new TextView(ServerFileList.this); 
     mTextView.setText(Long.toString((currentSize/totalSize) * 100) + "%"); 
    } 

    private void playSong(String songPath) { 
     try { 
      mp.reset(); 
      mp.setDataSource(songPath); 
      mp.prepare(); 
      mp.start(); 
     } catch (IOException e) { 
      Log.v(getString(R.string.app_name), e.getMessage()); 
     } 
    } 

    class downloadAudio extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(ServerFileList.this); 
      pDialog.setMessage("Downloading File list from server, Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... arg0) { 

      // set the download URL, a url that points to a file on the internet 
      // this is the file to be downloaded 
      URL url = null; 
      try { 
       url = new URL("http://server/folder/uploadAudio/abcd.mp3"); 
      } catch (MalformedURLException e3) { 
       // TODO Auto-generated catch block 
       e3.printStackTrace(); 
      } 

      // create the new connection 
      HttpURLConnection urlConnection = null; 
      try { 
       urlConnection = (HttpURLConnection) url.openConnection(); 
      } catch (IOException e5) { 
       // TODO Auto-generated catch block 
       e5.printStackTrace(); 
      } 

      // set up some things on the connection 
      try { 
       urlConnection.setRequestMethod("GET"); 
      } catch (ProtocolException e4) { 
       // TODO Auto-generated catch block 
       e4.printStackTrace(); 
      } 
      urlConnection.setDoOutput(true); 

      // and connect! 
      try { 
       urlConnection.connect(); 
      } catch (IOException e3) { 
       // TODO Auto-generated catch block 
       e3.printStackTrace(); 
      } 

      // set the path where we want to save the file 
      // in this case, going to save it on the root directory of the 
      // sd card. 
      String MEDIA_PATH = new String(
        Environment.getExternalStorageDirectory() 
          + "/newDirectory/"); 
      File SDCardRoot = new File(MEDIA_PATH); 

      if (!SDCardRoot.exists()) { 
       SDCardRoot.mkdir(); 
      } 
      // create a new file, specifying the path, and the filename 
      // which we want to save the file as. 
      File file = new File(SDCardRoot, System.currentTimeMillis() 
        + ".mp3"); 

      // this will be used to write the downloaded data into the file we 
      // created 
      FileOutputStream fileOutput = null; 
      try { 
       fileOutput = new FileOutputStream(file); 
      } catch (FileNotFoundException e2) { 
       // TODO Auto-generated catch block 
       e2.printStackTrace(); 
      } 

      // this will be used in reading the data from the internet 
      InputStream inputStream = null; 
      try { 
       inputStream = urlConnection.getInputStream(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      // this is the total size of the file 
      int totalSize = urlConnection.getContentLength(); 
      // variable to store total downloaded bytes 
      int downloadedSize = 0; 

      // create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; // used to store a temporary size of the 
            // buffer 

      // now, read through the input buffer and write the contents to the 
      // file 
      try { 
       while ((bufferLength = inputStream.read(buffer)) > 0) { 
        // add the data in the buffer to the file in the file output 
        // stream (the file on the sd card 
        fileOutput.write(buffer, 0, bufferLength); 
        // add up the size so we know how much is downloaded 
        downloadedSize += bufferLength; 
        // this is where you would do something to report the 
        // prgress, 
        // like this maybe 
        updateProgress(downloadedSize, totalSize); 

       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      // close the output stream when done 
      try { 
       fileOutput.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 

      // catch some possible errors... 

     } 

     protected void onPostExecute(String file_url) { 
      if (pDialog.isShowing()) { 
       pDialog.dismiss(); 
      } 
     } 
    } 

    class getAudiofromServer extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(ServerFileList.this); 
      pDialog.setMessage("Getting File list from server, Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected String doInBackground(String... arg0) { 
      try { 
       urlAudio = new URL("http://server/folder/uploadAudio"); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } 
      ApacheURLLister lister1 = new ApacheURLLister(); 
      try { 
       myList = lister1.listAll(urlAudio); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     protected void onPostExecute(String file_url) { 
      if (pDialog.isShowing()) { 
       pDialog.dismiss(); 
      } 

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        ServerFileList.this, android.R.layout.simple_list_item_1, 
        myList); 
      adapter.notifyDataSetChanged(); 
      mListView.setAdapter(adapter); 
      mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
      mListView.setCacheColorHint(Color.TRANSPARENT); 
     } 
    } 
    } 
+0

你需要提供具體信息:你有什麼類型的服務器,它是如何訪問的? 「所有文件」是什麼意思?如果你能下載一個文件,我看不到下載「所有文件」的問題...... '但有些我怎麼無法實現'爲什麼?給我們提供理由和/或嘗試的代碼。 – initramfs

+0

@CPUTerminator,我試過這段代碼http://stackoverflow.com/questions/15873417/how-to-download-a-directory-from-ftp-server-in-android。但它似乎並不適合我。 – InnocentKiller

+0

你有FTP服務器嗎?什麼不行?拋出異常嗎?你可以發佈堆棧跟蹤嗎? 「它不起作用」並不能幫助我們幫助您調試問題。 – initramfs

回答

0

我已經通過添加ivy jar文件作爲庫解決了問題,然後添加了以下代碼。

 try { 
      urlAudio = new URL("http://server/folder/uploadAudio"); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     ApacheURLLister lister1 = new ApacheURLLister(); 
     try { 
      myList = lister1.listAll(urlAudio); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
0

儘量遵循的步驟,

  1. 編寫服務器端組件這將返回文件的列表中,所有的音頻文件被託管的目錄。

  2. 在您的downloadAudio類中,不是下載文件,而是首先調用上述組件來獲取列表。

  3. 將您寫入的代碼移動到一個將使用String參數的函數中。

  4. 在步驟2的url列表中循環並在步驟3中調用函數,並將參數作爲url列表的當前元素。

  5. 保留所有支票和餘額以確保下載所有文件。

你的問題是更多的設計問題,然後編程。

相關問題