基本信息
源码名称:从服务器下载文件到模拟器
源码大小:17.06M
文件格式:.zip
开发语言:Java
更新时间:2021-04-29
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
可以进行服务器端的文件下载

package com.example.myapplication1;

import android.os.Environment;;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication1.myokhttp.http.OkHttpUtil;
import com.example.myapplication1.myokhttp.http.ProgressListener;

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

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity_d extends AppCompatActivity implements View.OnClickListener{

    public static final String TAG = MainActivity_d.class.getName();
    private ProgressBar download_progress;
    private TextView download_text;
    public static String basePath = Environment.getExternalStorageDirectory().getAbsolutePath()   "/okhttp";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        download_progress = (ProgressBar) findViewById(R.id.download_progress);
        download_text = (TextView) findViewById(R.id.download_text);

        findViewById(R.id.ok_download).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ok_download:
                String url = "http://192.168.43.17:8088/TestServer/music/文爱.mp3";
                final String fileName = url.split("/")[url.split("/").length - 1];
                Log.i(TAG, "fileName=="   fileName);
                OkHttpUtil.downloadFile(url, new ProgressListener() {
                    @Override
                    public void onProgress(long currentBytes, long contentLength, boolean done) {
                        Log.i(TAG, "currentBytes=="   currentBytes   "==contentLength=="   contentLength   "==done=="   done);
                        int progress = (int) (currentBytes * 100 / contentLength);
                        download_progress.setProgress(progress);
                        download_text.setText(progress   "%");
                    }
                }, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }


                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response != null) {
                            InputStream is = response.body().byteStream();
                            FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()   "/DRMP/"   fileName));
                            int len = 0;
                            byte[] buffer = new byte[2048];
                            while (-1 != (len = is.read(buffer))) {
                                fos.write(buffer, 0, len);
                            }
                            fos.flush();
                            fos.close();
                            is.close();
                        }
                    }
                });
                break;
        }
    }
}