博主介绍:本人专注于Android/java/数据库/微信小程序技术领域的开发,以及有好几年的计算机毕业设计方面的实战开发经验和技术积累;尤其是在安卓(Android)的app的开发和微信小程序的开发,很是熟悉和了解;本人也是多年的Android开发人员;希望我发布的此篇文件可以帮助到您;
效果演示
一、gradle配置
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okio:okio:1.12.0'
二、文件上传代码
private void uploadFile(String fileInfor){ File file = new File(fileInfor); String postUrl = Consts.URL + Consts.APP.MessageAction + "?action_flag=uploadFile"; OkHttpUtil.postFile(postUrl, new ProgressListener() { @Override public void onProgress(long currentBytes, long contentLength, boolean done) { int progress = (int) (currentBytes * 100 / contentLength); if(intentFlag==1){ choiceImage.setText(imageName+"已上传"+progress+"%"); }else{ choiceVideo.setText(videoName+"已上传"+progress+"%"); } } }, new Callback() { @Override public void onFailure(Call call, IOException e) {} @Override public void onResponse(Call call, Response response) throws IOException { if (response != null) { String result = response.body().string(); } } }, file); }
三、对应的附属文件
1:ProgressListener
public interface ProgressListener { void onProgress(long currentBytes, long contentLength, boolean done); }
2:ProgressModel
public class ProgressModel implements Parcelable { private long currentBytes; private long contentLength; private boolean done = false; public ProgressModel(long currentBytes, long contentLength, boolean done) { this.currentBytes = currentBytes; this.contentLength = contentLength; this.done = done; } public long getCurrentBytes() { return currentBytes; } public void setCurrentBytes(long currentBytes) { this.currentBytes = currentBytes; } public long getContentLength() { return contentLength; } public void setContentLength(long contentLength) { this.contentLength = contentLength; } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } private static final Creator<ProgressModel> CREATOR = new Creator<ProgressModel>() { @Override public ProgressModel createFromParcel(Parcel parcel) { return new ProgressModel(parcel); } @Override public ProgressModel[] newArray(int i) { return new ProgressModel[i]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeLong(currentBytes); parcel.writeLong(contentLength); parcel.writeByte((byte) (done==true?1:0)); } protected ProgressModel(Parcel parcel) { currentBytes = parcel.readLong(); contentLength = parcel.readLong(); done = parcel.readByte()!=0; } }
3:ProgressRequestBody
public class ProgressRequestBody extends RequestBody { public static final int UPDATE = 0x01; private RequestBody requestBody; private ProgressListener mListener; private BufferedSink bufferedSink; private MyHandler myHandler; public ProgressRequestBody(RequestBody body, ProgressListener listener) { requestBody = body; mListener = listener; if (myHandler==null){ myHandler = new MyHandler(); } } class MyHandler extends Handler { public MyHandler() { super(Looper.getMainLooper()); } @Override public void handleMessage(Message msg) { switch (msg.what){ case UPDATE: ProgressModel progressModel = (ProgressModel) msg.obj; if (mListener!=null)mListener.onProgress(progressModel.getCurrentBytes(),progressModel.getContentLength(),progressModel.isDone()); break; } } } @Override public MediaType contentType() { return requestBody.contentType(); } @Override public long contentLength() throws IOException { return requestBody.contentLength(); } @Override public void writeTo(BufferedSink sink) throws IOException { if (bufferedSink==null){ bufferedSink = Okio.buffer(sink(sink)); } //写入 requestBody.writeTo(bufferedSink); //刷新 bufferedSink.flush(); } private Sink sink(BufferedSink sink) { return new ForwardingSink(sink) { long bytesWritten = 0L; long contentLength = 0L; @Override public void write(Buffer source, long byteCount) throws IOException { super.write(source, byteCount); if (contentLength==0){ contentLength = contentLength(); } bytesWritten += byteCount; //回调 Message msg = Message.obtain(); msg.what = UPDATE; msg.obj = new ProgressModel(bytesWritten,contentLength,bytesWritten==contentLength); myHandler.sendMessage(msg); } }; } }
4:ProgressResponseBody
public class ProgressResponseBody extends ResponseBody { public static final int UPDATE = 0x01; public static final String TAG = ProgressResponseBody.class.getName(); private ResponseBody responseBody; private ProgressListener mListener; private BufferedSource bufferedSource; private Handler myHandler; public ProgressResponseBody(ResponseBody body, ProgressListener listener) { responseBody = body; mListener = listener; if (myHandler==null){ myHandler = new MyHandler(); } } /** * 将进度放到主线程中显示 */ class MyHandler extends Handler { public MyHandler() { super(Looper.getMainLooper()); } @Override public void handleMessage(Message msg) { switch (msg.what){ case UPDATE: ProgressModel progressModel = (ProgressModel) msg.obj; //接口返回 if (mListener!=null)mListener.onProgress(progressModel.getCurrentBytes(),progressModel.getContentLength(),progressModel.isDone()); break; } } } @Override public MediaType contentType() { return responseBody.contentType(); } @Override public long contentLength() { return responseBody.contentLength(); } @Override public BufferedSource source() { if (bufferedSource==null){ bufferedSource = Okio.buffer(mySource(responseBody.source())); } return bufferedSource; } private Source mySource(Source source) { return new ForwardingSource(source) { long totalBytesRead = 0L; @Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead = super.read(sink, byteCount); totalBytesRead +=bytesRead!=-1?bytesRead:0; //发送消息到主线程 Message msg = Message.obtain(); msg.what = UPDATE; msg.obj = new ProgressModel(totalBytesRead,contentLength(),totalBytesRead==contentLength()); myHandler.sendMessage(msg); return bytesRead; } }; } }