zoukankan      html  css  js  c++  java
  • android使用OkHttp或者上传图片显示进度

    public class UploadImageBody extends RequestBody {
    
      private File mFile;
      private String mContentType;
      private ProgressListener mListener;
    
      @Override
      public MediaType contentType() {
        return MediaType.parse(mContentType);
      }
    
      @Override
      public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
          source = Okio.source(mFile);
          long total = 0;
          long read;
    
          while ((read = source.read(sink.buffer(), 8192)) != -1) {
            total += read;
            sink.flush();
            if (mListener != null) {
              mListener.onProgress(total, mFile.length());
            }
          }
        } finally {
          Util.closeQuietly(source);
        }
      }
    
      @Override
      public long contentLength() {
        return mFile.length();
      }
    
      public interface ProgressListener {
    
        void onProgress(long current, long total);
      }
    
    
      public static final class Builder {
    
        private String mFilePath;
        private ProgressListener mListener;
    
        public Builder withFilePath(String filePath) {
          this.mFilePath = filePath;
          return this;
        }
    
        public Builder withListener(ProgressListener mListener) {
          this.mListener = mListener;
          return this;
        }
    
        private String getFileName() {
          return FileUtil.getFileName(mFilePath);
        }
    
        public MultipartBody.Part buildPart() {
          UploadImageBody uploadImageBody = new UploadImageBody();
          uploadImageBody.mListener = this.mListener;
          uploadImageBody.mContentType = "application/octet-stream";
          uploadImageBody.mFile = new File(mFilePath);
          return MultipartBody.Part.createFormData("file", getFileName(), uploadImageBody);
        }
      }
    }
  • 相关阅读:
    使用正向proxy 连调部署在k8s 中的spring cloud 中的rest服务
    goflow golang 的基于flow的编程库
    gvm golang 的多版本工具
    jvm-profiler 学习试用
    httpdiff http 请求diff 工具
    tengine lua 模块docker 镜像集成
    tengine 支持dubbo 的docker镜像
    openresty ngx.location.capture http2 问题
    systemd 使用rc.local 说明
    revel golang的全栈开发框架
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/10650901.html
Copyright © 2011-2022 走看看