android 从服务器下载文件的脚本
android script for downloading files from server
我正在开发一个应用程序,该应用程序列出目录中的一组文件,然后允许用户 open/download 列出的任何文件。知道我应该从哪里开始吗?
你可以试试它的代码:
private class File_Video extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
File folders = new File(Environment.getExternalStorageDirectory()
+ "/pathofthefile/");
folders.mkdirs();
File file;
file = new File(Environment.getExternalStorageDirectory()
+ "/pathofthefile/file_video.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100%
// progress bar
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return "Downloaded";
} catch (Exception e) {
return null;
}
}
从 onCreate 开始:
File_Video downloadFile_video = new File_Video();
downloadFile_image.execute("http://youvebsite.com/file1.txt");
我正在开发一个应用程序,该应用程序列出目录中的一组文件,然后允许用户 open/download 列出的任何文件。知道我应该从哪里开始吗?
你可以试试它的代码:
private class File_Video extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
File folders = new File(Environment.getExternalStorageDirectory()
+ "/pathofthefile/");
folders.mkdirs();
File file;
file = new File(Environment.getExternalStorageDirectory()
+ "/pathofthefile/file_video.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100%
// progress bar
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return "Downloaded";
} catch (Exception e) {
return null;
}
}
从 onCreate 开始:
File_Video downloadFile_video = new File_Video();
downloadFile_image.execute("http://youvebsite.com/file1.txt");