android下载简单工具类

功能是实现下载文件,图片或MP3等,为了简单起见使用单线程,此代码为MarsAndroid教程的复制品,放在此处,留着参考。

首先是一个得到字节流随后保存到内存卡上的工具类:

android下载简单工具类
package com.example.utils;

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(){
        //得到当前外部存储设备的目录,即/SDCARD,后边加"/"为了之后方便
        SDpath=Environment.getExternalStorageDirectory()+"/";
    }
    /**
     * 在SD卡上创建文件
     * @param fileName
     * @return File
     * @throws IOException
     */
    public File creatSDFile(String fileName) throws IOException{
        File file=new File(SDpath+fileName);
        file.createNewFile();
        return file;
    }
    /**
     * 在SDCARD创建目录
     * @param dirName
     * @return
     */
    public File creatSDDir(String dirName){
        File dir=new File(SDpath+dirName);
        dir.mkdir();
        return dir;
    }
    public boolean isFileExist(String fileName){
        File file=new File(fileName);
        return file.exists();
    }
    /**
     * 将一个InputStream里的数据写入SD卡
     */
    public File write2SDFromInput(String path,String fileName,InputStream is){
        File file=null;
        OutputStream os=null;
        try{
            creatSDDir(path);
            file=creatSDFile(path+fileName);
            os=new FileOutputStream(file);
            byte buffer[]=new byte[4*1024];//4kb
            while((is.read(buffer))!=-1){
                os.write(buffer);
            }
            os.flush();
            System.out.println("write to "+path+"sucess!");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                os.close();
            }catch(Exception e){
                e.printStackTrace();
            }            
        }
        return file;
    }
    
    
    

}
android下载简单工具类

随后是文件下载类:

android下载简单工具类
package com.example.utils;

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;

public class HttpDownloader {
    private URL url=null;
    //此方法返回文本,但并未保存到SD卡
    public String downloadText(String urlstr){
        StringBuffer sb=new StringBuffer();
        String line=null;
        BufferedReader br=null;
        try{
            url=new URL(urlstr);
            HttpURLConnection con= (HttpURLConnection)url.openConnection();
            br=new BufferedReader(new InputStreamReader(con.getInputStream()));
            while((line=br.readLine())!=null){
                sb.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    /**
     * 
     * @param urlstr
     * @param path
     * @param fileName
     * @return -1-error,0-success,1-file exist
     */
    public int downloadMP3(String urlstr,String path,String fileName){
        InputStream is=null;
        try{
            FileUtils fileUtils=new FileUtils();
            if(fileUtils.isFileExist(path+fileName)){
                return 1;
            }else{
                is=getInputStreamFromUrl(urlstr);
                File resultfile=fileUtils.write2SDFromInput(path, fileName, is);
                if(resultfile==null){
                    return -1;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            return -1;
        }finally{
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return 0;
    }
    private InputStream getInputStreamFromUrl(String urlstr) 
            throws MalformedURLException,IOException{
        URL url=new URL(urlstr);
        HttpURLConnection con=(HttpURLConnection)url.openConnection();
        InputStream is=con.getInputStream();
        return is;
    }

}
android下载简单工具类

android下载简单工具类

上一篇:如何退出整个Android 应用


下一篇:2.1……Android中的单位简介