android之下载文件

使用场景:用tomcat搭建了一个文件服务器,然后在安卓端去下载文件。

使用技术:使用 HttpURLConnection 建立http连接,并下载

package com.example.facecompare.FaceApi;

import android.content.Context;
import android.util.Log;

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

public class ModelDownload {
    /*
     * 获取http连接处理类HttpURLConnection
     */
    private HttpURLConnection getConnection(String strUrl)
    {
        URL url;
        HttpURLConnection urlcon = null;
        try {
            url = new URL(strUrl);
            urlcon = (HttpURLConnection) url.openConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlcon;
    }

    /*
     * 写文件到sd卡 demo
     * 前提需要设置模拟器sd卡容量,否则会引发EACCES异常
     * 先创建文件夹,在创建文件
     */
    public boolean down2sd(String strUrl, String filename, downhandler handler)
    {
        HttpURLConnection urlcon = getConnection(strUrl);
        if (urlcon == null){
            return false;
        }
        File file = new File(filename);

        FileOutputStream outputStream = null;
        try {
            int currentSize =0;
            int totalSize =0;
            InputStream inputStream = urlcon.getInputStream();
            if (200 != urlcon.getResponseCode()){
                return false;
            }
            totalSize = urlcon.getContentLength();
            outputStream = new FileOutputStream(file);
            byte[] buffer = new byte[1024*10];
            while (true) {
                int iReadSize = inputStream.read(buffer);
                if (-1 == iReadSize){
                    break;
                }
                outputStream.write(buffer,0,iReadSize);
                //同步更新数据
                currentSize = currentSize+iReadSize;
                handler.setSize(currentSize,totalSize);
            }
            inputStream.close();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /*
     * 内部回调接口类
     */
    public abstract static class downhandler
    {
        public abstract void setSize(int currentSize,int totalSize);
    }
}

使用方法:

                        ModelDownload load = new ModelDownload();
                        load.down2sd("http://192.168.31.16:8080/download/"+modelName,storagePath,new ModelDownload.downhandler(){

                            @Override
                            public void setSize(int currentSize, int totalSize) {
                                item.put("DownState",currentSize+"/"+totalSize);
                                Message mes = mHandle.obtainMessage();
                                mes.what = 100;
                                mHandle.sendMessage(mes);
                            }


                        });

 

你可能感兴趣的:(android)