|
2 | 2 |
|
3 | 3 | import com.qiniu.android.http.UserAgent; |
4 | 4 | import com.qiniu.android.storage.UpToken; |
| 5 | +import com.qiniu.android.utils.GZipUtil; |
5 | 6 |
|
6 | 7 | import java.io.File; |
7 | 8 | import java.io.FileNotFoundException; |
8 | 9 | import java.io.FileOutputStream; |
9 | 10 | import java.io.IOException; |
| 11 | +import java.io.RandomAccessFile; |
10 | 12 | import java.nio.charset.Charset; |
11 | 13 | import java.util.Date; |
12 | 14 | import java.util.concurrent.ExecutorService; |
@@ -241,9 +243,17 @@ private void tryUploadAndClean(final UpToken upToken, File recordFile) { |
241 | 243 |
|
242 | 244 | //同步上传 |
243 | 245 | private boolean upload(final UpToken upToken, File recordFile) { |
| 246 | + |
| 247 | + byte[] logData = getLogData(recordFile); |
| 248 | + logData = GZipUtil.gZip(logData); |
| 249 | + if (logData == null || logData.length == 0){ |
| 250 | + return false; |
| 251 | + } |
| 252 | + |
244 | 253 | try { |
| 254 | + |
245 | 255 | OkHttpClient client = getHttpClient(); |
246 | | - RequestBody reqBody = RequestBody.create(MediaType.parse("text/plain"), recordFile); |
| 256 | + RequestBody reqBody = RequestBody.create(MediaType.parse("text/plain"), logData); |
247 | 257 |
|
248 | 258 | Request.Builder requestBuilder = new Request.Builder().url(serverURL). |
249 | 259 | addHeader("Authorization", "UpToken " + upToken.token). |
@@ -271,6 +281,30 @@ private boolean upload(final UpToken upToken, File recordFile) { |
271 | 281 | } |
272 | 282 | } |
273 | 283 |
|
| 284 | + private byte[] getLogData(File recordFile){ |
| 285 | + if (recordFile == null || recordFile.length() == 0){ |
| 286 | + return null; |
| 287 | + } |
| 288 | + |
| 289 | + long length = recordFile.length(); |
| 290 | + RandomAccessFile randomAccessFile = null; |
| 291 | + byte[] data = null; |
| 292 | + try { |
| 293 | + randomAccessFile = new RandomAccessFile(recordFile, "r"); |
| 294 | + data = new byte[(int)length]; |
| 295 | + randomAccessFile.read(data); |
| 296 | + } catch (FileNotFoundException ignored) { |
| 297 | + } catch (IOException e) { |
| 298 | + data = null; |
| 299 | + } |
| 300 | + if (randomAccessFile != null){ |
| 301 | + try { |
| 302 | + randomAccessFile.close(); |
| 303 | + } catch (IOException e){} |
| 304 | + } |
| 305 | + return data; |
| 306 | + } |
| 307 | + |
274 | 308 | private boolean isOk(Response res) { |
275 | 309 | return res.isSuccessful() && res.header("X-Reqid") != null; |
276 | 310 | } |
|
0 commit comments