Android菜鸟日记11 下载

本文深入探讨了通过HTTP从互联网下载内容的方法,并详细解释了直接下载内容和保存成文件的具体实现过程。包括如何通过URL获取连接、装饰输入流、读取数据以及将文件保存到SD卡的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://ask.lurencun.com
海大知道


下载
1. 作用:通过HTTP从互联网下载内容
2. 重点:
1.直接下载内容不保存到Sdcard时 通过url.opneConnection获得连接
2.需要保存成文件时 先判断是否有文件,无则先创建文件夹, 在创建文件。
使用:
不保存到file的
1.通过url获得连接 并强转成HttpURLConnection对象
URL url=new URL(urlstr);
HttpURLConnection hc=(HttpURLConnection)url.openConnection();
2.通过HttpURLConnection对象获取inputStream对象并一步步的装饰成BufferedReader对象。
br=new BufferedReader(new InputStreamReader(hc.getInputStream()));
3.从BufferedReader通过readline()读数据
while((line=br.readLine())!=null){
sb.append(line); //sb是一个StringBuffer对象
}
//bufferReader .readLine() 输出为下一行数据内容且把游标指向下一行 默认游标是在第一行之上 固,每次调用readLine()必须有对象接受此方法返回值(返回值为执行前游标所指的下一行数据)

//Returns the next line of text available from this reader
4.最后把stringbutter 对象toString()生成string对象输出。

保存成file文件的
注意:保存成file的类 应该独立写 只需传入 url,保存的文件名,保存的路径即可。
1.构造file的类 传入当前SD的路径 通过Environment.getExternalStorageDirectory()获得
因为不是所有手机的SD路径都一样 所以这样写可以较好的匹配各种型号的手机
String SDPATH=Environment.getExternalStorageDirectory()

2.判断是否存在当前文件。
public boolean isisFileExist(String path)
{
File file=new File(sd+path);
return file.exists();
}
3.如不存在则生成文件夹 再生成文件
public void creatDirPath(String path)
{
File dir=new File(sd+path);
dir.mkdir();
}
public File creatfile(String namepath) throws IOException
{
File file=new File(sd+namepath);
file.createNewFile();
return file;
}
4.把通过步骤3创造出的FILE对象传入到new FileOutputStram(FILE对象)。 得到一个向File文件输出的FileOutputStram对象。
file=creatfile(dirpath+namepath);
out=new FileOutputStream(file);
5.创建缓冲区
byte buffer [] = new byte[4 * 1024];//缓冲区大小为4K
6.读传入的inputStream流 并写入FileOutputStream
while(is.read(buffer)!=-1){//每次读4K的数据 写入buffer
out.write(buffer);//每次从buffer 取出4k数据
}
7.刷新缓冲区 关闭输出流
刷新缓冲
out.flush();
关闭输出流
finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


代码
DownloadActivity
package cfuture.poolo;

import java.io.IOException;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cfuture.tool.*;

public class DownloadActivity extends Activity {

private Button txtbn=null;
private Button filebn=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbn=(Button)this.findViewById(R.id.txtbutton);
filebn=(Button)this.findViewById(R.id.filebutton);
System.out.println("1111");
txtbn.setOnClickListener(new txtbn());
filebn.setOnClickListener(new filebn());

}
class txtbn implements OnClickListener
{


public void onClick(View arg0) {
download d=new download();
String temp=d.downloadTxt("http://192.168.1.101:8080/temp/1.txt");
System.out.println(temp);

}



}
class filebn implements OnClickListener
{


public void onClick(View arg0) {
download d=new download();
try {
System.out.println("22222222222");
d.url2file("http://192.168.1.101:8080/temp/1.wav","muisc/","3.wav");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}



}
}

Download.java
package cfuture.tool;

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

import javax.net.ssl.HttpsURLConnection;


public class download {

public String downloadTxt(String urlstr)
{
String line=null;
StringBuffer sb=new StringBuffer();
BufferedReader br=null;
try {
URL url=new URL(urlstr);
HttpURLConnection hc=(HttpURLConnection)url.openConnection();
br=new BufferedReader(new InputStreamReader(hc.getInputStream()));
while((line=br.readLine())!=null){
sb.append(line);
}
//br.readLine() 输出为下一行数据内容且把游标指向下一行 默认游标是在第一行之上
//Returns the next line of text available from this reader


} catch (IOException e) {

e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return sb.toString();
}

public void url2file(String urlstr,String path,String filename) throws IOException{
FileSD fd=new FileSD();
URL url= new URL(urlstr);
HttpURLConnection ht=(HttpURLConnection)url.openConnection();
int x=fd.write2file(ht.getInputStream(), path, filename);
System.out.println("================="+x);
}
}

FileSD.java
package cfuture.tool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Environment;

public class FileSD {

String sd=null;
public String getSd()
{

return this.getSd();
}
public FileSD()
{
sd=Environment.getExternalStorageDirectory()+"/";
}
//创建路径
public void creatDirPath(String path)
{
File dir=new File(sd+path);
dir.mkdir();
}
public File creatfile(String namepath) throws IOException
{
File file=new File(sd+namepath);
file.createNewFile();
return file;
}
public boolean isisFileExist(String path)
{
File file=new File(sd+path);
return file.exists();
}
public int write2file(InputStream is,String dirpath,String namepath)
{
File file=null;
FileOutputStream out=null;
if(isisFileExist(dirpath+namepath)){
return 0;
}else{
creatDirPath(dirpath);
try {
file=creatfile(dirpath+namepath);
out=new FileOutputStream(file);
int x=0;
byte buffer [] = new byte[4 * 1024];//缓冲区大小为4K
while(is.read(buffer)!=-1){//每次读4K的数据 写入buffer

out.write(buffer);//每次从buffer 取出4k数据
}
out.flush();
} catch (IOException e) {

e.printStackTrace();
return -1;
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
}
}


[img]http://dl.iteye.com/upload/attachment/538321/891df802-77b9-3269-afbd-7ae1d81a72fe.gif[/img]

2011-8-14
poolo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值