Android开发中如何实现http下载文件呢(磨砺营马剑威)

一、场景

下载存文本文件和下载如mp3等大容量的文件

界面

二、代码编写

1.AndroidMainfest.xml中配置

主要是解决网络权限和写SDCard的权限


package="linys.download" android:versionCode="1" android:versionName="1.0">


2.Activity编写

利用Http协议下载文件并存储到SDCard

1.创建一个URL对象

2.通过URL对象,创建一个HttpURLConnection对象

3.得到InputStream

4.从InputStream当中读取数据

存到SDCard

1.取得SDCard路径

2.利用读取大文件的IO读法,读取文件

package linys.download;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/**

*

* @Project: Android_MyDownload

* @Desciption: 利用Http协议下载文件并存储到SDCard

1.创建一个URL对象

2.通过URL对象,创建一个HttpURLConnection对象

3.得到InputStream

4.从InputStream当中读取数据

存到SDCard

1.取得SDCard路径

2.利用读取大文件的IO读法,读取文件

*

* @Author: LinYiSong

* @Date: 2011-3-25~2011-3-25

*/

public class MyDownload extends Activity {

private Button downFileBtn;

private Button downMP3Btn;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

downFileBtn=(Button)this.findViewById(R.id.downFile);

downMP3Btn=(Button)this.findViewById(R.id.downMP3);

downFileBtn.setOnClickListener(new DownFileClickListener());

downMP3Btn.setOnClickListener(new DownMP3ClickListener());

}

/**

*

* @Project: Android_MyDownload

* @Desciption: 只能读取文本文件,读取mp3文件会出现内存溢出现象

* @Author: LinYiSong

* @Date: 2011-3-25~2011-3-25

*/

class DownFileClickListener implements OnClickListener{

@Override

public void onClick(View v) {

String urlStr="http://172.17.54.91:8080/download/down.txt";

try {

/*

* 通过URL取得HttpURLConnection

* 要网络连接成功,需在AndroidMainfest.xml中进行权限配置

*

*/

URL url=new URL(urlStr);

HttpURLConnection conn=(HttpURLConnection)url.openConnection();

//取得inputStream,并进行读取

InputStream input=conn.getInputStream();

BufferedReader in=new BufferedReader(new InputStreamReader(input));

String line=null;

StringBuffer sb=new StringBuffer();

while((line=in.readLine())!=null){

sb.append(line);

}

System.out.println(sb.toString());

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

*

* @Project: Android_MyDownload

* @Desciption: 读取任意文件,并将文件保存到手机SDCard

* @Author: LinYiSong

* @Date: 2011-3-25~2011-3-25

*/

class DownMP3ClickListener implements OnClickListener{

@Override

public void onClick(View v) {

String urlStr="http://172.17.54.91:8080/download/1.mp3";

String path="file";

String fileName="2.mp3";

OutputStream output=null;

try {

/*

* 通过URL取得HttpURLConnection

* 要网络连接成功,需在AndroidMainfest.xml中进行权限配置

*

*/

URL url=new URL(urlStr);

HttpURLConnection conn=(HttpURLConnection)url.openConnection();

//取得inputStream,并将流中的信息写入SDCard

/*

* 写前准备

* 1.在AndroidMainfest.xml中进行权限配置

*

* 取得写入SDCard的权限

* 2.取得SDCard的路径: Environment.getExternalStorageDirectory()

* 3.检查要保存的文件上是否已经存在

* 4.不存在,新建文件夹,新建文件

* 5.将input流中的信息写入SDCard

* 6.关闭流

*/

String SDCard=Environment.getExternalStorageDirectory()+"";

String pathName=SDCard+"/"+path+"/"+fileName;//文件存储路径

File file=new File(pathName);

InputStream input=conn.getInputStream();

if(file.exists()){

System.out.println("exits");

return;

}else{

String dir=SDCard+"/"+path;

new File(dir).mkdir();//新建文件夹

file.createNewFile();//新建文件

output=new FileOutputStream(file);

//读取大文件

byte[] buffer=new byte[4*1024];

while(input.read(buffer)!=-1){

output.write(buffer);

}

output.flush();

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

output.close();

System.out.println("success");

} catch (IOException e) {

System.out.println("fail");

e.printStackTrace();

}

}

}

}

}

更多内容关注微信公众号mjw-java或访问www.moliying.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,778评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,660评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,486评论 19 139
  • 一、流的概念和作用。 流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象。即数据在两设备之间的传输...
    布鲁斯不吐丝阅读 13,420评论 2 95
  • 9.给你两个正整数a和b, 输出它们的最大公约数。 方法一 :辗转相除法 方法二:迭代法
    everfight阅读 1,397评论 0 0