admin管理员组文章数量:1532372
2023年12月13日发(作者:)
Android应用软件开发(十一)文件下载
使用Http协议下载文件,写入SD卡中
下载步骤:
1、创建一个HttpURLConnection对象
HttpURLConnection urlConn =
(HttpURLConnection )nnection();
2、获得一个InputStream对象
utStream()
3、设置网络访问权限(manifest文件中设置)
ET
访问SD卡:
1. 得到SD卡的目录
ernalStorageDirectory()
2. 访问SD卡的权限:
_EXTERNAL_STORAGE
注意:一定不要忘记设置权限
文件:
package="ad" android:versionCode="1" android:versionName="1.0" > android:icon="@drawable/ic_launcher" android:label="@string/app_name" > android:label="@string/app_name" android:name=".DownloadActivity" >
:
package ad;
import wnloader;
import ty;
import ;
import ;
import kListener;
import ;
public class DownloadActivity extends Activity {
/** Called when the activity is first created. */
private Button downloadTxtButton;
private Button downloadMp3Button;
@Override public void onCreate(Bundle savedInstanceState) {
te(savedInstanceState);
setContentView();
downloadTxtButton = (Button)findViewById(adTxt);
downloadMp3Button = (Button)findViewById(adMp3);
lickListener(new DownLoadTxtListener());
lickListener(new DownLoadMp3Listener());
}
class DownLoadTxtListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
String str = ad("/?action=loading&id=13912266&cid=05_14&p=1");
n("Download");
n(str);
}
}
class DownLoadMp3Listener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String urlStr = "/?action=loading&id=13912266&cid=05_14&p=1";
HttpDownloader httpDownloader = new HttpDownloader();
int state = le(urlStr, "voa/", "myFile");
n(state);
}
}
}
:
package ;
import edReader;
import ;
import ption;
import tream;
import treamReader;
import LConnection;
import medURLException;
import ;
public class HttpDownloader {
private URL url = null;
/**
* 根据URL下载文件
* 1. 创建一个URL对象
* 2. 通过URL对象,创建一个HttpURLConnection对象
* 3. 得到InputStream
* 4. 从InputStream当中读取数据
*/
//文本文件的下载
public String download(String urlStr){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try{
//创建一个URL对象
url = new URL(urlStr);
//创建一个Http连接
HttpURLConnection urlConn = (HttpURLConnection)nnection();
//使用IO流读取数据
/**
* utStream()得到的是字节流
* 套一层InputStreamReader得到的是字符流
* 在套一层BufferedReader得到的字符流就可以使用readLine按行读取数据了
*/
buffer = new BufferedReader(new InputStreamReader(utStream()));
while((line = ne())!=null){
(line);
}
}catch(Exception e){
n("Error");
tackTrace();
}finally{
try{
();
}catch(Exception e){
tackTrace();
} }
return ng();
}
//下载文件并写入到SD卡中
//参数:url地址,文件存储目录绝对路径,存储的文件名
public int downFile(String urlStr,String path,String fileName){
InputStream inputStream = null;
try{
FileUtils fileUtils = new FileUtils();
//如果文件已经存在则返回1
if(Exist(path+fileName)){
return 1;
}else{
//根据url地址得到文件输入流
inputStream = getInputStreamFromUrl(urlStr);
//将输入流写入SD卡,具体过程见
File resultFile = DFromInput(path, fileName, inputStream);
if(resultFile == null){
return -1;
}
}
} catch(Exception e){
tackTrace();
return -1;
} finally{
try{
();
} catch(Exception e){
tackTrace();
}
}
return 0;
}
//根据URL地址得到文件输入流
public InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException, IOException{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)nnection();
InputStream inputStream = utStream();
return inputStream;
}
}
:
package ;
import ;
import tputStream;
import ption;
import tream;
import Stream;
import nment;
/**
* 此类是操作SD卡文件的封装
* 包括判断文件(夹)是否存在,创建文件(夹),将文件流写入文件
*
* @author Administrator
*
*/
public class FileUtils {
//SD卡的路径名
private String SDPATH;
public String getSDPATH(){
return SDPATH;
}
public FileUtils(){
//得到当前外部存储设备的目录
SDPATH = ernalStorageDirectory()+"/";
}
/**
* 在SD卡上创建文件
*
*/
public File createSDFile(String fileName)throws IOException{
//根据文件的全路径名得到文件对象
File file = new File(SDPATH + fileName);
//创建文件
NewFile();
return file;
}
/**
* 在SD卡上创建目录
*/
public File createSDDir(String dirName){
File dir = new File(SDPATH + dirName);
();
return dir;
}
/**
* 判断文件夹是否存在
*/
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return ();
}
/**
* 将一个InputStream里面的数据写入到SD卡中
*/
//参数:文件存放路径(绝对目录路径),文件名,输入流
public File writeSDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try{
//创建文件存储目录
createSDDir(path);
//创建文件
file = createSDFile(path+fileName);
//获取新建文件的输出流
output = new FileOutputStream(file);
//缓冲区
byte buffer [] = new byte[4 * 1024];
//从输入流中依次读入数据并输出到文件中,每次4个字节
while(((buffer))!= -1){
(buffer);
}
//清空缓存
();
}catch(Exception e){
tackTrace();
}finally{
try{
//关闭输出流
();
}catch(Exception e){
tackTrace();
}
}
return file;
}
}
版权声明:本文标题:Android应用软件开发(十一)文件下载 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1702457562a7931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论