当前位置:酷游电竞官网 > 酷游电竞赛事竞猜 > 五日双差别上早上巡回,OkHttp实现文件上传进程

五日双差别上早上巡回,OkHttp实现文件上传进程

文章作者:酷游电竞赛事竞猜 上传时间:2019-05-22

【3月新规来啦!四川城镇职工医保账户资金家人可共用】今日热搜①首批21个罕见病药品减按3%征增值税;②支付宝信用卡还款超2000元部分收取0.1%服务费;③公交车驾驶区防护隔离设施侧围高度不少于1.6米;④山东:用人单位每1至2年组织一次女职工妇科检查;⑤贵州:犬只伤人最高罚个人1000元。与你有关转发周知!

文件上传就一个没刻度的进度条在那里转怎么行,本篇带你实现上传进度,为你的进度条添加刻度吧,啥都不说了,重点重写RequestBody,看代码

六天双分化上下午循环训练是高级后阶段或赛前的极限密度训练方法之一。该法强调和突出负荷强度另一组成内容及练习密度的控制和应用。较高密度的训练活动可较高效率的增进肌肉线条,肌肉质量和肌肉耐力。同时也非常有助于肌肉和肌束之间的脂肪的缩减和消除。pe8健身计划_快吧健身网_一个全面而专业的健身知识网站

酷游电竞官网 1

import com.squareup.okhttp.*;
import okio.Buffer;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

import java.io.File;
import java.io.IOException;

public class ProgressUploadFile {
    private static final OkHttpClient okHttpClient = new OkHttpClient();

    private void run() {

        MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);

        File file = new File("D:\file.jpg");
        builder.addFormDataPart("file", file.getName(), createCustomRequestBody(MultipartBuilder.FORM, file, new ProgressListener() {
            @Override public void onProgress(long totalBytes, long remainingBytes, boolean done) {
                System.out.print((totalBytes - remainingBytes) * 100 / totalBytes   "%");
            }
        }));

        RequestBody requestBody = builder.build();

        Request request = new Request.Builder()
                .url("http://localhost:8080/upload") //地址
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override public void onFailure(Request request, IOException e) {

            }

            @Override public void onResponse(Response response) throws IOException {
                System.out.println("response.body().string() = "   response.body().string());

            }
        });
    }

    public static RequestBody createCustomRequestBody(final MediaType contentType, final File file, final ProgressListener listener) {
        return new RequestBody() {
            @Override public MediaType contentType() {
                return contentType;
            }

            @Override public long contentLength() {
                return file.length();
            }

            @Override public void writeTo(BufferedSink sink) throws IOException {
                Source source;
                try {
                    source = Okio.source(file);
                    //sink.writeAll(source);
                    Buffer buf = new Buffer();
                    Long remaining = contentLength();
                    for (long readCount; (readCount = source.read(buf, 2048)) != -1; ) {
                        sink.write(buf, readCount);
                        listener.onProgress(contentLength(), remaining -= readCount, remaining == 0);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }

    interface ProgressListener {
        void onProgress(long totalBytes, long remainingBytes, boolean done);
    }

    public static void main(String[] args) {
        new ProgressUploadFile().run();

    }

}

酷游电竞官网 2酷游电竞官网 3酷游电竞官网 4pe8健身计划_快吧健身网_一个全面而专业的健身知识网站

酷游电竞官网 5

结果: 
0%0%1%1%1%2%2%3%3%3%4%4%4%5%5%6%6%6%7%7%7%8%8%9%9%9 !!!""###$$$%%&&&'''(()))000112223334455566677888999@@AAABBCCCDDDEEFFFGGGHHIIIPPPQQRRRSSSTTUUUVVVWWXXXYYY``aaabbbccdddeefffggghhiiipppqqrrrsssttuuuvvvwwxxxyyy

本文由酷游电竞官网发布于酷游电竞赛事竞猜,转载请注明出处:五日双差别上早上巡回,OkHttp实现文件上传进程

关键词: 酷游电竞官网