android通过网络下载文字和Mp3的例子

最近看了mars老师的的视频的网络下载章节,下面就贴一下代码,备用。

android通过网络下载文件分2步:

1.建立网络连接获取数据流;

2.创建文件把数据流写到文件中即可。(简单吧)

废话不多说,上代码:

 

网络处理类 HttpDownloader.java

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/***
 * 通过http协议下载文件
 * ***/
public class HttpDownloader {
   
	private URL url = null;
	
	public InputStream getInputStreamFormUrl(String urlStr)
	        throws MalformedURLException,IOException{
		url = new URL(urlStr);
		HttpURLConnection httpc = (HttpURLConnection)url.openConnection();
		return httpc.getInputStream();
	}

	public String downloadText(String urlStr){
		 StringBuffer sb = new StringBuffer();
		 String line = null;
		 BufferedReader breader = null;
		 try{
			 url = new URL(urlStr);
			 HttpURLConnection httpc = (HttpURLConnection)url.openConnection();
			 InputStream inputs = httpc.getInputStream();
			 breader = new BufferedReader(new InputStreamReader(inputs));
			 while((line=breader.readLine())!=null){
				 sb.append(line);
			 }			 
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 try {
				breader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }  
		 return sb.toString();
	}
	
	public int downloadFile(String urlStr,String path,String name){
		InputStream input = null;
		try{
			FileUtils fu = new FileUtils();
			if(fu.isFileExist(path+name)){   // 是否存在文件,如果存在返回1
			   return 1;
			}else{
				input = getInputStreamFormUrl(urlStr);
				File f = fu.inputStream2File(path, name, input);
				if(f==null){
					return -1;              // 数据有异常的时候返回-1
				}
					input.close();
			} 
		}catch(Exception e){
		  e.printStackTrace();	
		  return -1;
		}
		return 0;                         // 数据正常的时候返回0
	}
	
}

 

文件处理类  FileUtils.java

 

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/***
 * 通过http协议下载文件
 * ***/
public class HttpDownloader {
   
	private URL url = null;
	
	public InputStream getInputStreamFormUrl(String urlStr)
	        throws MalformedURLException,IOException{
		url = new URL(urlStr);
		HttpURLConnection httpc = (HttpURLConnection)url.openConnection();
		return httpc.getInputStream();
	}

	public String downloadText(String urlStr){
		 StringBuffer sb = new StringBuffer();
		 String line = null;
		 BufferedReader breader = null;
		 try{
			 url = new URL(urlStr);
			 HttpURLConnection httpc = (HttpURLConnection)url.openConnection();
			 InputStream inputs = httpc.getInputStream();
			 breader = new BufferedReader(new InputStreamReader(inputs));
			 while((line=breader.readLine())!=null){
				 sb.append(line);
			 }			 
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 try {
				breader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }  
		 return sb.toString();
	}
	
	public int downloadFile(String urlStr,String path,String name){
		InputStream input = null;
		try{
			FileUtils fu = new FileUtils();
			if(fu.isFileExist(path+name)){   // 是否存在文件,如果存在返回1
			   return 1;
			}else{
				input = getInputStreamFormUrl(urlStr);
				File f = fu.inputStream2File(path, name, input);
				if(f==null){
					return -1;              // 数据有异常的时候返回-1
				}
					input.close();
			} 
		}catch(Exception e){
		  e.printStackTrace();	
		  return -1;
		}
		return 0;                         // 数据正常的时候返回0
	}
	
}

 

// 方法调用代码

 

	HttpDownloader httpd = new HttpDownloader(); 
				String txt = httpd.downloadText("http://192.168.1.41/Temp.txt");// 下载文本文件
				int state = httpd.downloadFile("http://192.168.1.41/temp.mp3", "ch", "temp.mp3"); //下载mp3文件

 

顺便说一个本人的android群85506951欢迎大家加入!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值