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


                        });

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值