android 下载程序

HttpDownloader 文件:

package zhou.demo.download;

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.URL;

public class HttpDownloader {
 private URL url = null;
 
 public String download(String urlStr){
  StringBuffer sb = new StringBuffer();
  String line = null;
  BufferedReader buffer = null;
  try {
   url = new URL(urlStr);
   HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
   buffer = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
   while((line = buffer.readLine()) != null){
    sb.append(line);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   try {
    buffer.close();
   } catch (Exception e2) {
    e2.printStackTrace();
   }
  }
  return sb.toString();
 }
 
 public int downFile(String urlStr, String path, String fileName){
  InputStream inputStream = null;
  try {
   FileUtils fileUtils = new FileUtils();
   if(fileUtils.isFileExist(path + fileName)){
    return 1;
   }else{
    inputStream = getInputStreamFromUrl(urlStr);
    File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);
    if(resultFile == null){
     return -1;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
   return -1;
  }finally{
   try {
    inputStream.close();
   } catch (Exception e2) {
    e2.printStackTrace();
   }
  }
  return 0;
 }
 
 /**
  * 根据URL得到输入流
  * @throws IOException 
  */
 public InputStream getInputStreamFromUrl(String urlStr) throws IOException{
  HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
  InputStream inputStream = urlConn.getInputStream();
  return inputStream;
 }
}

 

 

 

 

 

package zhou.demo.download;

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

import android.os.Environment;

public class FileUtils {
 private String SDPATH;
 public String getSDPATH(){
  return SDPATH;
 }
 public FileUtils(){
  //得到当前外部储存设备的目录   (***兼容写法***)
  SDPATH = Environment.getExternalStorageDirectory() + "/";
 }
 public File createSDFile(String fileName) throws IOException{
  File file = new File(SDPATH + fileName);
  file.createNewFile();
  return file;
 }
 /**
  * 在SD上创建目录
  * @param dirName
  * @return
  */
 public File createSDDir(String dirName){
  File dir = new File(SDPATH + dirName);
  dir.mkdir();
  return dir;
 }
 
 /**
  * 判断SD上的文件夹是否存在
  */
 public boolean isFileExist(String fileName){
  File file = new File(SDPATH + fileName);
  return file.exists();
 }
 
 /**
  * 将数据传入SD里面
  */
 public File write2SDFromInput(String path, String fileName, InputStream input){
  File file = null;
  OutputStream output = null;
  try {
   createSDDir(path);
   file = createSDFile(path + fileName);
   output = new FileOutputStream(file);
   byte buffer[] = new byte[4*1024];
   while(input.read(buffer) != -1){
    output.write(buffer);
   }
   output.flush();
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   try {
    output.close();
   } catch (Exception e2) {
    // TODO: handle exception
   }
  }
  return file;
 }
}

  

 

package zhou.demo.download;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DownLoad extends Activity {
    private Button textButton;
    private Button mp3Button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textButton = (Button)this.findViewById(R.id.txt_button);
        mp3Button = (Button)this.findViewById(R.id.mp3_button);
        
        textButton.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-lgenerated method stub
    HttpDownloader httpDownloader = new HttpDownloader();
    String lrc = httpDownloader.download("http://192.168.1.1:8060");
    System.out.println(lrc);
   }
         
        });
        
        mp3Button.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    HttpDownloader httpDownloader = new HttpDownloader();
    int result = httpDownloader.downFile("http://mp3","voa/","a1.mp3");
    System.out.println(result);
   }
         
        });
    }
}

 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
<Button
	android:id="@+id/txt_button"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/txt_button_name"
	/>
<Button
	android:id="@+id/mp3_button"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/mp3_button_name"
	/>
</LinearLayout>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值