【发布时间】:2014-03-01 18:22:42
【问题描述】:
我有几个文件上传到我的服务器,现在我想做的是我想一个一个下载所有文件并将其保存到一个文件夹中。
所以基本上我正在尝试做类似 [this][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);
}
}
}
【问题讨论】:
-
你需要具体说明:你有什么类型的服务器,它是如何访问的? “所有文件”是什么意思?如果您能够下载一个文件,我认为下载“所有文件”没有问题...
but some how i am not able to achieve为什么?给我们理由和/或尝试的代码。 -
@CPUTerminator,我试过这个代码stackoverflow.com/questions/15873417/…。但它似乎对我不起作用。
-
您有 FTP 服务器吗?什么不起作用?是否抛出异常?你可以发布堆栈跟踪吗? “它不起作用”并不能帮助我们帮助您调试问题。
-
@CPUTerminator,很抱歉,但我不知道如何集成它。我刚刚通过提供像
http://i-qualtech.com/Fidol/uploadAudio/abcd.mp3这样的绝对路径来实现下载单个文件的代码,但现在我不知道如何通过删除abcd.mp3的文件名来实现这个并下载所有文件。 -
首先,这是一个 HTTP 链接,因此您尝试实现的代码肯定不会工作,因为那是 FTP,一个完全不同的协议。您需要在 HTTP 服务器上发布某种文件列表,以便您的程序可以检索要下载的文件列表。如果没有索引文件,您的程序不可能知道文件名。
标签: android audio download directory android-sdcard