Android从网络下载文件

1.主Activity

 1 public class MainActivity extends Activity {
 2 
 3     private Button download=null;
 4     private EditText url=null;
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_main);
 8         
 9         this.download=(Button) this.findViewById(R.id.button1);
10         this.url=(EditText) this.findViewById(R.id.editText1);
11         
12         this.download.setOnClickListener(new ClickListener());
13     }
14 
15     private  final class ClickListener implements OnClickListener 
16     {
17         public void onClick(View v) 
18         {
19             String httpUrl=url.getText().toString();
20             try 
21             {
22                 //创建一个URL对象
23                 URL url=new URL(httpUrl);
24                 //下载数据的方法
25                 DownLoad(url);
26             } catch (Exception e) {
27                 // TODO Auto-generated catch block
28                 e.printStackTrace();
29             }
30         }
31         
32         
33         public void DownLoad(final URL url)
34         {
35         
36             //开启线程下载数据
37                 new Thread(new Runnable()
38                         {
39                             public void run() 
40                             {
41                                 try 
42                                 {
43                                     Thread.sleep(2000);
44                                     //获取SDCard的路径
45                                     String path=Environment.getExternalStorageDirectory().getAbsolutePath().toString();
46                                     //新建文件
47                                     File file=new File(path+"/Mp3");
48                                     //判段文件是否存在
49                                     if(!file.exists())
50                                     {
51                                         //创建目录
52                                         file.mkdir();
53                                     }
54                                     //创建文件,并起名为a.mp3
55                                     File files=new File(file.getAbsolutePath(),"a.mp3");
56                                     @SuppressWarnings("resource")
57                                     //用来保存读取到的网络数据,保存到文件中
58                                     FileOutputStream  fileoutputstream=new FileOutputStream(files) ;
59                                     //获取httpURLConnection
60                                     HttpURLConnection http=(HttpURLConnection) url.openConnection();
61                                     //设置请求方式
62                                     http.setRequestMethod("GET");
63                                     //设置连接超时时间
64                                     http.setConnectTimeout(5000);
65                                     //如果连接成功读取网络数据
66                                     if(http.getResponseCode()==200)
67                                     {
68                                         //得到HttpURLConnection的输入流对象,用拿来读取网络中的数据
69                                         InputStream inputstream=http.getInputStream();
70                                         //调用WebTools中的getData方法并得到数据
71                                         byte[] data=WebTools.getData(inputstream);
72                                         //把数据写入到文件中
73                                         fileoutputstream.write(data);
74                                         System.out.println("下载成功");
75                                     }
76                                         
77                                 } 
78                                 catch (Exception e) 
79                                 {
80                                     e.printStackTrace();
81                                 } 
82                             }
83                             
84                         }).start();
85                     
86                     
87                 }
88             
89     }
90 
91 }

2.读取网络数据的WebTools工具类

public class WebTools {

    public static byte[] getData(InputStream input) throws Exception
    {
        //存放数据的byte数组
        byte[] buffer=new byte[5000];
        //保存数据的输出流对象
        ByteArrayOutputStream output=new ByteArrayOutputStream();
        int len=0;
        while((len=input.read(buffer))!=-1)
        {
            //写入数据
            output.write(buffer, 0, len);
        }
        //返回输入流中的数据 
        return output.toByteArray();
        
    }
}

3.界面预览图

4.结果图

转载于:https://www.cnblogs.com/tianshidechibang234/p/3194019.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值