|
26 | 26 | import okhttp3.Call; |
27 | 27 | import okhttp3.Callback; |
28 | 28 | import okhttp3.MediaType; |
| 29 | +import okhttp3.MultipartBody; |
29 | 30 | import okhttp3.OkHttpClient; |
30 | 31 | import okhttp3.Request; |
31 | 32 | import okhttp3.RequestBody; |
@@ -117,6 +118,11 @@ private void uploadFile() { |
117 | 118 | ChatManager.Instance().getUploadUrl(new File(filePath).getName(), MessageContentMediaType.FILE, new GetUploadUrlCallback() { |
118 | 119 | @Override |
119 | 120 | 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 | + } |
120 | 126 | MediaType type=MediaType.parse("application/octet-stream"); |
121 | 127 | File file = new File(filePath); |
122 | 128 | RequestBody fileBody = new UploadFileRequestBody(RequestBody.create(type,file), (progress)->{ |
@@ -150,7 +156,41 @@ public void onFail(int errorCode) { |
150 | 156 | } |
151 | 157 | }); |
152 | 158 | } |
| 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 | + } |
153 | 181 |
|
| 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 | + } |
154 | 194 | @Override |
155 | 195 | public void onClick(View view) { |
156 | 196 | if(state == 0 || state == 3 || state == 4) { |
|
0 commit comments