Skip to content

Commit 3b7ab6c

Browse files
committed
支持七牛大文件上传
1 parent 5d74131 commit 3b7ab6c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

uikit/src/main/java/cn/wildfire/chat/kit/conversation/bigfile/UploadBigFileActivity.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import okhttp3.Call;
2727
import okhttp3.Callback;
2828
import okhttp3.MediaType;
29+
import okhttp3.MultipartBody;
2930
import okhttp3.OkHttpClient;
3031
import okhttp3.Request;
3132
import okhttp3.RequestBody;
@@ -117,6 +118,11 @@ private void uploadFile() {
117118
ChatManager.Instance().getUploadUrl(new File(filePath).getName(), MessageContentMediaType.FILE, new GetUploadUrlCallback() {
118119
@Override
119120
public void onSuccess(String uploadUrl, String remoteUrl, String backUploadupUrl, int serverType) {
121+
if(serverType == 1) {
122+
String[] ss = uploadUrl.split("\\?");
123+
uploadQiniu(ss[0], ss[1], ss[2], filePath);
124+
return;
125+
}
120126
MediaType type=MediaType.parse("application/octet-stream");
121127
File file = new File(filePath);
122128
RequestBody fileBody = new UploadFileRequestBody(RequestBody.create(type,file), (progress)->{
@@ -150,7 +156,41 @@ public void onFail(int errorCode) {
150156
}
151157
});
152158
}
159+
private void uploadQiniu(String url, String token, String key, String filePath) {
160+
File file = new File(filePath);
161+
MediaType type=MediaType.parse("application/octet-stream");
162+
RequestBody fileBody = new UploadFileRequestBody(RequestBody.create(type,file), (progress)->{
163+
runOnUiThread(()->updateProgress(progress));
164+
});
165+
166+
final MultipartBody.Builder mb = new MultipartBody.Builder();
167+
mb.addFormDataPart("key", key);
168+
mb.addFormDataPart("token", token);
169+
mb.addFormDataPart("file", "fileName", fileBody);
170+
mb.setType(MediaType.parse("multipart/form-data"));
171+
RequestBody body = mb.build();
172+
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
173+
174+
mCall = okHttpClient.newCall(requestBuilder.build());
175+
176+
mCall.enqueue(new Callback() {
177+
@Override
178+
public void onFailure(Call call, IOException e) {
179+
runOnUiThread(()-> updateState(4));
180+
}
153181

182+
@Override
183+
public void onResponse(Call call, Response response) throws IOException {
184+
if(response.code() != 200) {
185+
runOnUiThread(()-> updateState(4));
186+
} else {
187+
UploadBigFileActivity.this.remoteUrl = remoteUrl;
188+
189+
runOnUiThread(()-> updateState(2));
190+
}
191+
}
192+
});
193+
}
154194
@Override
155195
public void onClick(View view) {
156196
if(state == 0 || state == 3 || state == 4) {

0 commit comments

Comments
 (0)