1、使用HTTP协议下载文件
文件下载步骤:
1、创建一个HttpURLConnection对象
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
2、获得一个InputStream对象
urlConn.getInputStream()
3、访问网络的权限
android.permission.INTERNET
2、将下载的文件写入SDCARD
访问SDCARD:
1、得到当前设备SD卡的目录:
Environment.getExternalStorageDirectory()
2、访问SD卡的权限:
android.permission.INTERNET // 访问网络的权限
android.permission.WRITE_EXTERNAL_STORAGE // 访问SD卡的权限
--------------------------------------------------
实例:download
1、修改AndroidManifest.xml,设置SD卡的访问权限
<uses-permission android:name="android.permission.INTERNET">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
1、DownloadActivity.java // 使用Tomcat运行的程序作为服务器,用作测试使用;
1、DownloadTxtListener // 下载文本文件
HttpDownloader hd = new HttpDownloader(); // 自定义的封装类
String lrc = hd.download("http://192.168.1.102:8080/voal500/al.lrc"); // al.lrc :歌词文本文件
//注意路径中的ip不要写错,参照本地ip设置,否则查找不到文件;可启动Tomcat后先在浏览器中查找一下该文件,确保路径无误。
2、DownloadMp3Listener // 下载Mp3文件
HttpDownloader hd = new HttpDownloader();
int result = hd.downFile("http://192.168.1.102:8080/voal500/al.mp3", "voa/", "al.mp3");
2、FileUtils.java // 有关SDCARD的一些操作的封装类
private String SDPATH;
1、得到当前设备SD卡的目录
public FileUtils(){
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
2、在SD卡上创建文件
public File createSDFile(String fileName){
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
3、在SD卡上创建目录
public File createSDDir(String dirName){
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
4、判断SD卡上的文件夹是否存在
public boolean isFileExist(String fileName){
File file = new File(SDPATH + dirName);
return file.exists();
}
5、将数据流写入到SD上
public File write2SDFromInput(String path, String fileName, InputStream input){
createSDDir(path);
File file = createSDFile(path + fileName);
OutputStream output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
while((input.read(buffer)) != -1){
output.write(buffer);
}
output.flush();
}
3、HttpDownloader.java
1、下载任何格式的文本文件
public String download(String urlStr){
URL url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
BufferedReader buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream));
StringBuffer sb = new StringBuffer();
while((line=buffer.readLine()) != null){
sb.append(line);
}
}
InputStream // 读取出字节流
InputStreamReader // 读取出字符流
BufferedReader // 读取出行 readLine()
2、下载其它格式的文件, 并写入到SD卡里;
public int downFile(String urlStr, String path, String fileName){
// 返回整形-1,代表文件出错;0代表成功;1代表该文件已经存在
FileUtils fileUtils = new FileUtils();
if(fileUtils.isFileExist(path + fileName)){
return 1;
}else{
InputStream inputStream = getInputStreamFromUrl(urlStr);
File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);
if(resultFile==null){ return -1; }
}
return 0;
}
3、根据URL得到输入流
public InputStream getInputStreamFromUrl(String urlStr){
URL url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream ios = urlConn.getInputStream();
return inputStream;
}