android java下载文件_Android利用Http下载文件

一、场景

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

二、代码编写

1.AndroidMainfest.xml中配置

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

Java代码

1.  <?xml  version="1.0" encoding="utf-8"?>

2. 

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

4.      

5.

6.      

7.          

8.              

9.                  

10.                 

11.             

12.         

13.     

14.     

15.         

16.         

17.

2.Activity编写

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

1.创建一个URL对象

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

3.得到InputStream

4.从InputStream当中读取数据

存到SDCard

1.取得SDCard路径

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

Java代码AAffA0nNPuCLAAAAAElFTkSuQmCC

1.  package linys.download;

2.

3.  import java.io.BufferedReader;

4.  import java.io.File;

5.  import java.io.FileOutputStream;

6.  import java.io.IOException;

7.  import java.io.InputStream;

8.  import java.io.InputStreamReader;

9.  import java.io.OutputStream;

10. import java.net.HttpURLConnection;

11. import java.net.MalformedURLException;

12. import java.net.URL;

13.

14. import android.app.Activity;

15. import android.os.Bundle;

16. import android.os.Environment;

17. import android.view.View;

18. import android.view.View.OnClickListener;

19. import android.widget.Button;

20. /**

21.  *

22.  * @Project: Android_MyDownload

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

24.     1.创建一个URL对象

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

26.     3.得到InputStream

27.     4.从InputStream当中读取数据

28.     存到SDCard

29.     1.取得SDCard路径

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

31.  *

32.  * @Author: LinYiSong

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

34.  */

35. public class MyDownload extends Activity {

36.

37.     private Button downFileBtn;

38.     private Button downMP3Btn;

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

40.     @Override

41.     public void onCreate(Bundle savedInstanceState) {

42.         super.onCreate(savedInstanceState);

43.         setContentView(R.layout.main);

44.

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

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

47.

48.         downFileBtn.setOnClickListener(new DownFileClickListener());

49.         downMP3Btn.setOnClickListener(new DownMP3ClickListener());

50.     }

51.

52.     /**

53.      *

54.      * @Project: Android_MyDownload

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

56.      * @Author: LinYiSong

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

58.      */

59.     class DownFileClickListener implements OnClickListener{

60.         @Override

61.         public void onClick(View v) {

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

63.             try {

64.                 /*

65.                  * 通过URL取得HttpURLConnection

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

67.                  * 

68.                  */

69.                 URL url=new URL(urlStr);

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

71.                 //取得inputStream,并进行读取

72.                 InputStream input=conn.getInputStream();

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

74.                 String line=null;

75.                 StringBuffer sb=new StringBuffer();

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

77.                     sb.append(line);

78.                 }

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

80.

81.             } catch (MalformedURLException e) {

82.                 e.printStackTrace();

83.             } catch (IOException e) {

84.                 e.printStackTrace();

85.             }

86.         }

87.     }

88.     /**

89.      *

90.      * @Project: Android_MyDownload

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

92.      * @Author: LinYiSong

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

94.      */

95.     class DownMP3ClickListener implements OnClickListener{

96.

97.         @Override

98.         public void onClick(View v) {

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

100.             String path="file";

101.             String fileName="2.mp3";

102.             OutputStream output=null;

103.             try {

104.                 /*

105.                  * 通过URL取得HttpURLConnection

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

107.                  * 

108.                  */

109.                 URL url=new URL(urlStr);

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

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

112.

113.                 /*

114.                  * 写前准备

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

116.                  * 

117.                  * 取得写入SDCard的权限

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

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

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

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

122.                  * 6.关闭流

123.                  */

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

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

126.

127.                 File file=new File(pathName);

128.                 InputStream input=conn.getInputStream();

129.                 if(file.exists()){

130.                     System.out.println("exits");

131.                     return;

132.                 }else{

133.                     String dir=SDCard+"/"+path;

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

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

136.                     output=new FileOutputStream(file);

137.                     //读取大文件

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

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

140.                         output.write(buffer);

141.                     }

142.                     output.flush();

143.                 }

144.             } catch (MalformedURLException e) {

145.                 e.printStackTrace();

146.             } catch (IOException e) {

147.                 e.printStackTrace();

148.             }finally{

149.                 try {

150.                         output.close();

151.                         System.out.println("success");

152.                     } catch (IOException e) {

153.                         System.out.println("fail");

154.                         e.printStackTrace();

155.                     }

156.             }

157.         }

158.

159.     }

160. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值