2014-02-05 72 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. 保留所有支票和余额以确保下载所有文件。

你的问题是更多的设计问题,然后编程。

相关问题