ANDROID_MARS学习笔记_S01原始版_009_下载文件
一、代码
1.xml
(1)main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <Button 8 android:id="@+id/downloadTxt" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="下载文本文件" 12 /> 13 <Button 14 android:id="@+id/downloadMp3" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="下载MP3文件 " 18 /> 19 </LinearLayout>
(2)AndroidManifest.xml.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.s01_original_e18_download" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".Download" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 <uses-permission android:name="android.permission.INTERNET"/> 27 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 28 </manifest>
2.java
(1)Download.java
1 package com.example.s01_original_e18_download; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 9 public class Download extends Activity { 10 /** Called when the activity is first created. */ 11 private Button downloadTxtButton; 12 private Button downloadMp3Button; 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 downloadTxtButton = (Button)findViewById(R.id.downloadTxt); 18 downloadTxtButton.setOnClickListener(new DownloadTxtListener()); 19 downloadMp3Button = (Button)findViewById(R.id.downloadMp3); 20 downloadMp3Button.setOnClickListener(new DownloadMp3Listener()); 21 } 22 23 class DownloadTxtListener implements OnClickListener{ 24 25 @Override 26 public void onClick(View v) { 27 HttpDownloader httpDownloader = new HttpDownloader(); 28 //String lrc = httpDownloader.download("http://192.168.1.104:8080/goods/LoseYourself.lrc"); 29 String lrc = httpDownloader.download("http://www.runoob.com/sqlite/sqlite-intro.html"); 30 System.out.println(lrc); 31 } 32 33 } 34 class DownloadMp3Listener implements OnClickListener{ 35 36 @Override 37 public void onClick(View v) { 38 // TODO Auto-generated method stub 39 HttpDownloader httpDownloader = new HttpDownloader(); 40 int result = httpDownloader.downFile("http://192.168.1.104:8080/goods/LoseYourself.mp3", "voa/", "a1.mp3"); 41 System.out.println(result); 42 } 43 44 } 45 46 }
(2)HttpDownloader.java
1 package com.example.s01_original_e18_download; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.net.HttpURLConnection; 9 import java.net.MalformedURLException; 10 import java.net.URL; 11 12 13 public class HttpDownloader { 14 private URL url = null; 15 16 /** 17 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容 18 * 1.创建一个URL对象 19 * 2.通过URL对象,创建一个HttpURLConnection对象 20 * 3.得到InputStram 21 * 4.从InputStream当中读取数据 22 * @param urlStr 23 * @return 24 */ 25 public String download(String urlStr) { 26 StringBuffer sb = new StringBuffer(); 27 String line = null; 28 BufferedReader buffer = null; 29 try { 30 // 创建一个URL对象 31 url = new URL(urlStr); 32 // 创建一个Http连接 33 HttpURLConnection urlConn = (HttpURLConnection) url 34 .openConnection(); 35 // 使用IO流读取数据 36 buffer = new BufferedReader(new InputStreamReader(urlConn 37 .getInputStream())); 38 while ((line = buffer.readLine()) != null) { 39 sb.append(line); 40 } 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } finally { 44 try { 45 buffer.close(); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 return sb.toString(); 51 } 52 53 /** 54 * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在 55 */ 56 public int downFile(String urlStr, String path, String fileName) { 57 InputStream inputStream = null; 58 try { 59 FileUtils fileUtils = new FileUtils(); 60 61 if (fileUtils.isFileExist(path + fileName)) { 62 return 1; 63 } else { 64 inputStream = getInputStreamFromUrl(urlStr); 65 File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream); 66 if (resultFile == null) { 67 return -1; 68 } 69 } 70 } catch (Exception e) { 71 e.printStackTrace(); 72 return -1; 73 } finally { 74 try { 75 inputStream.close(); 76 } catch (Exception e) { 77 e.printStackTrace(); 78 } 79 } 80 return 0; 81 } 82 83 /** 84 * 根据URL得到输入流 85 * 86 * @param urlStr 87 * @return 88 * @throws MalformedURLException 89 * @throws IOException 90 */ 91 public InputStream getInputStreamFromUrl(String urlStr) 92 throws MalformedURLException, IOException { 93 url = new URL(urlStr); 94 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 95 InputStream inputStream = urlConn.getInputStream(); 96 return inputStream; 97 } 98 }
(3)FileUtils.java
1 package com.example.s01_original_e18_download; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 9 import android.os.Environment; 10 11 public class FileUtils { 12 private String SDPATH; 13 14 public String getSDPATH() { 15 return SDPATH; 16 } 17 public FileUtils() { 18 //得到当前外部存储设备的目录 19 // /SDCARD 20 SDPATH = Environment.getExternalStorageDirectory() + "/"; 21 } 22 /** 23 * 在SD卡上创建文件 24 * 25 * @throws IOException 26 */ 27 public File creatSDFile(String fileName) throws IOException { 28 File file = new File(SDPATH + fileName); 29 file.createNewFile(); 30 return file; 31 } 32 33 /** 34 * 在SD卡上创建目录 35 * 36 * @param dirName 37 */ 38 public File creatSDDir(String dirName) { 39 File dir = new File(SDPATH + dirName); 40 dir.mkdirs(); 41 return dir; 42 } 43 44 /** 45 * 判断SD卡上的文件夹是否存在 46 */ 47 public boolean isFileExist(String fileName){ 48 File file = new File(SDPATH + fileName); 49 return file.exists(); 50 } 51 52 /** 53 * 将一个InputStream里面的数据写入到SD卡中 54 */ 55 public File write2SDFromInput(String path,String fileName,InputStream input){ 56 File file = null; 57 OutputStream output = null; 58 try{ 59 creatSDDir(path); 60 file = creatSDFile(path + fileName); 61 output = new FileOutputStream(file); 62 byte buffer [] = new byte[4 * 1024]; 63 while((input.read(buffer)) != -1){ 64 output.write(buffer); 65 } 66 output.flush(); 67 } 68 catch(Exception e){ 69 e.printStackTrace(); 70 } 71 finally{ 72 try{ 73 output.close(); 74 } 75 catch(Exception e){ 76 e.printStackTrace(); 77 } 78 } 79 return file; 80 } 81 82 }
转载于:https://www.cnblogs.com/shamgod/p/5189162.html
ANDROID_MARS学习笔记_S01原始版_009_下载文件相关推荐
- 单片机c语言北航,【下载资料】《51单片机学习笔记》北航版
原标题:[下载资料]<51单片机学习笔记>北航版 如果手机下载有问题,请移步至电脑端,链接:https://forum.mianbaoban.cn/t/topic/36906 内容简介 本 ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- LaTeX 学习笔记(刘大成版)
LaTeX \LaTeX LATEX 学习笔记(刘大成版) 常用参考网站: 目录 LaTeX \LaTeX LATEX 学习笔记(刘大成版) 1. LaTeX \LaTeX LATEX的概述与安 ...
- 学习笔记1:更改python下载源
学习笔记1:更改python下载源,让安装速度更快! 1.常用的国内镜像源 清华镜像源 https://pypi.tuna.tsinghua.edu.cn/simple 中科大镜像源 https:// ...
- MongoDB学习笔记一:MongoDB的下载和安装
MongoDB学习笔记一:MongoDB的下载和安装 趁着这几天比較空暇,准备学习一下MongoDB数据库.今天就简单的学习了一些MongoDB的下载和安装.并创建了存储MongoDB的数据仓库. 将 ...
- Java中如何创建自定义的注解学习笔记(MD版)
概要 Java中如何创建自定义的注解学习笔记(MD版). 博客 博客地址:IT老兵驿站. 前言 记得这篇笔记还是在泉州的龙玲酒店记录的,是一个周六的晚上,坐飞机从上海到泉州,从笔记中能勾起一些旅游的回 ...
- pandas基础学习笔记(简略版)
pandas基础学习笔记(简略版) 1.DataFrame 2.series 3.基本数据操作 4.DataFrame 运算 5.pandas绘图 6.文本的读取与存储 1.DataFrame 既有行 ...
- 李宏毅深度学习笔记——呕心整理版
李宏毅深度学习笔记--呕心整理版 闲谈叨叨叨: 之前看过吴恩达的一部分课程,所以有一定理论基础,再看李宏毅的课程会有新的理解.我先以有基础的情况写完学习过程,后续再以零基础的角度补充细节概念(估计不会 ...
- FPGA学习笔记之Altera FPGA使用JIC文件配置固化教程
FPGA学习笔记之Altera FPGA使用JIC文件配置固化教程 很多做过单片机的朋友都知 道,我们在对MCU烧写完程序固件后,那么该程序固件就存储在了该MCU内部.即使MCU断电了再重新上电,程序 ...
- Python 学习笔记(3)对txt文件的读与写操作(下)
上一章节我们讨论了如何对txt文本文件进行读写操作,这一张将讨论如何进行二进制文件的写与读.<Python 学习笔记(3)对txt文件的读与写操作(上)>的链接如下https://blog ...
最新文章
- HTML发送语音,上传音频PHP接收
- ORA-01919: role 'PLUSTRACE' does not exist
- c语言一对圆括号不能,C语言单元习题集
- IOS 多线程的一些总结
- 为什么C++(来自C++之父的观点)
- 中国汽车融资租赁业运营前景与投资战略规划报告2022-2028年版
- Cocos2d-JS项目之UI界面的优化
- 创建自已的sql函数
- HTML5新特征、窍门和技术(1~5)
- vim安装时报错:Depends:vim-common (=2:7.4.1689-3ubuntu1.4) but 2:8.0.1453-1ubuntu1.1 is to be installed
- Java数组去重的多种方法,[Java教程]JavaScript常见的五种数组去重的方式
- Activities
- 应用程序到偏好设置跳转 以及 应用程序之间的跳转
- 锆石科技开发板的简单介绍
- 用故障恢复控制台恢复注册表
- java多线程系列(一)
- inductive bias:归纳偏置
- 企业信用报告可用网银自助查询
- java 生成word表格
- Java面试java基础,重庆java哪家培训班好
热门文章
- Cisco路由器配置动态路由rip
- scl语言用plc脉冲做定时器_scl语言用plc脉冲做定时器_西门子PLC SCL语言开发学习笔记(二)...
- python中ADSL拨号
- Source Insight 4.0安装破解及简单使用
- Moto me525+ 刷机
- html设计判断闰年,html5闰年判断函数
- php如何拼接图片路径,如何把图片拼接在一张图上?
- 网易面试总结——面试案例1~面试案例4
- 360oauth token是什么意思_币圈token是什么意思?
- 基本标示符-宏-编译连接