zoukankan      html  css  js  c++  java
  • android下载简单工具类

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

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

    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;
        }
        
        
        
    
    }

    随后是文件下载类:

    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;
        }
    
    }
  • 相关阅读:
    js预编译
    JS防抖和节流模式的实际应用
    常见的几种数组去重的方法,总有一种适合你~
    调用微信扫一扫功能,踩坑'invalid signature'
    如何快速的vue init 属于自己的vue模板?
    如何做到在webpack打包vue项目后,在外部动态修改配置文件
    vue拖拽组件 vuedraggable API options实现盒子之间相互拖拽排序克隆clone
    js解析URL参数
    vue复选框 模拟checkbox多选全选,vue页面加载屏蔽花括号
    js毫秒转换天时分秒/动态倒计时
  • 原文地址:https://www.cnblogs.com/makefile/p/3561726.html
Copyright © 2011-2022 走看看