• 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.结果图

  • 相关阅读:
    CSS3实战手册(第3版)(影印版)
    21世纪C语言(影印版)
    Spring Data:企业级Java的现代数据访问技术(影印版)
    Hive编程(影印版)
    iOS 6编程Cookbook(影印版)
    做自己——鬼脚七自媒体第一季
    放飞App:移动产品经理实战指南
    《推荐系统》+《推荐系统实践》
    步步惊“芯”——软核处理器内部设计分析
    ip的划分,超详细
  • 原文地址:https://www.cnblogs.com/tianshidechibang234/p/3194019.html
Copyright © 2020-2023  润新知