use of okhttp3.MultipartBody.Builder in project HL4A by HL4A.
the class QCloudUploader method uploadFile.
private void uploadFile() throws 后端错误 {
try {
if (AVOSCloud.showInternalDebugLog()) {
LogUtil.log.d("upload as whole file");
}
byte[] bytes = avFile.getData();
fileSha = AVUtils.SHA1(bytes);
MultipartBody.Builder builder = new MultipartBody.Builder();
RequestBody fileBody = RequestBody.create(MediaType.parse(APPLICATION_OCTET_STREAM), bytes, 0, getCurrentSliceLength(0, bytes.length));
builder.addFormDataPart(FILE_CONTENT, fileKey, fileBody);
builder.addFormDataPart(PARAM_OP, OP_UPLOAD);
builder.addFormDataPart(PARAM_SHA, fileSha);
MediaType type = MediaType.parse(MULTIPART_FORM_DATA);
if (null != type) {
builder.setType(type);
}
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(uploadUrl);
requestBuilder.header(HEADER_AUTHORIZATION, token);
requestBuilder.header(HEADER_CONTENT_TYPE, MULTIPART_FORM_DATA);
for (String key : FileUploader.UPLOAD_HEADERS.keySet()) {
requestBuilder.header(key, FileUploader.UPLOAD_HEADERS.get(key));
}
requestBuilder.post(builder.build());
Request request = requestBuilder.build();
Response response = executeWithRetry(request, RETRY_TIMES);
if (response.code() != 200) {
throw AVErrorUtils.createException(后端错误.OTHER_CAUSE, AVUtils.stringFromBytes(response.body().bytes()));
}
} catch (Exception e) {
if (AVOSCloud.isDebugLogEnabled()) {
LogUtil.avlog.e("Exception during file upload", e);
}
throw AVErrorUtils.createException(e, "Exception during file upload");
}
}
use of okhttp3.MultipartBody.Builder in project HL4A by HL4A.
the class QCloudUploader method uploadControlSlice.
private JSONObject uploadControlSlice(String token, String url, byte[] wholeFile) throws 后端错误 {
MultipartBody.Builder builder = new MultipartBody.Builder();
try {
String fileSha = AVUtils.SHA1(wholeFile);
builder.addFormDataPart(PARAM_SHA, fileSha);
builder.addFormDataPart(PARAM_OP, OP_UPLOAD_SLICE);
builder.addFormDataPart(PARAM_FILE_SIZE, String.valueOf(wholeFile.length));
builder.addFormDataPart(PARAM_SLICE_SIZE, String.valueOf(DEFAULT_SLICE_LEN));
MediaType type = MediaType.parse(MULTIPART_FORM_DATA);
if (null != type) {
builder.setType(type);
}
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(url);
requestBuilder.header(HEADER_AUTHORIZATION, token);
requestBuilder.header(HEADER_CONTENT_TYPE, MULTIPART_FORM_DATA);
requestBuilder.post(builder.build());
Request request = requestBuilder.build();
Response response = executeWithRetry(request, RETRY_TIMES);
if (response != null) {
byte[] responseBody = response.body().bytes();
return parseSliceUploadResponse(AVUtils.stringFromBytes(responseBody));
}
} catch (Exception e) {
e.printStackTrace();
throw new 后端错误(后端错误.OTHER_CAUSE, "Upload file failure");
}
return null;
}
use of okhttp3.MultipartBody.Builder in project HL4A by HL4A.
the class QiniuAccessor method createBlockInQiniu.
/**
* REST API:
* - POST /mkblk/<blockSize> HTTP/1.1
* - Host: upload.qiniu.com
* - Content-Type: application/octet-stream
* - Content-Length: <firstChunkSize>
* - Authorization: UpToken <UploadToken>
* - <firstChunkBinary>
*
* - Response
* {
* "ctx": "<Ctx string>",
* "checksum": "<Checksum string>",
* "crc32": <Crc32 int64>,
* "offset": <Offset int64>,
* "host": "<UpHost string>"
* }
* @param blockSize
* @param firstChunkSize
* @param firstChunkData
* @param retry
* @return
*/
public QiniuBlockResponseData createBlockInQiniu(int blockSize, int firstChunkSize, final byte[] firstChunkData, int retry) {
try {
if (AVOSCloud.isDebugLogEnabled()) {
LogUtil.avlog.d("try to invoke mkblk");
}
String endPoint = String.format(QINIU_CREATE_BLOCK_EP, blockSize);
Request.Builder builder = new Request.Builder();
builder.url(endPoint);
builder.addHeader(HEAD_CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
builder.addHeader(HEAD_CONTENT_LENGTH, String.valueOf(firstChunkSize));
builder.addHeader(HEAD_AUTHORIZATION, "UpToken " + this.uploadToken);
RequestBody requestBody = RequestBody.create(MediaType.parse(DEFAULT_CONTENT_TYPE), firstChunkData, 0, firstChunkSize);
builder = builder.post(requestBody);
Response response = this.client.newCall(builder.build()).execute();
return parseQiniuResponse(response, QiniuBlockResponseData.class);
} catch (Exception e) {
LogUtil.log.e("encounter exception during file uploading(mkblk). retry=" + retry, e);
if (retry-- > 0) {
return createBlockInQiniu(blockSize, firstChunkSize, firstChunkData, retry);
}
}
return null;
}
use of okhttp3.MultipartBody.Builder in project HL4A by HL4A.
the class QiniuAccessor method makeFile.
/**
* REST API
* POST /mkfile/<fileSize>/key/<encodedKey>/mimeType/<encodedMimeType>/x:user-var/<encodedUserVars> HTTP/1.1
* Host: <UpHost>
* Content-Type: text/plain
* Content-Length: <ctxListSize>
* Authorization: UpToken <UploadToken>
* <ctxList>
*
* Request params:
* - fileSize
* - encodeKey
* - encodedMimeType
* - encodedUserVars
*
* Response:
* {
* "hash": "<ContentHash string>",
* "key": "<Key string>"
* }
*
* @param fileTotalSize
* @param uploadFileCtxs
* @param retry
* @return
* @throws Exception
*/
public QiniuMKFileResponseData makeFile(int fileTotalSize, List<String> uploadFileCtxs, int retry) throws Exception {
try {
String endPoint = String.format(QINIU_MKFILE_EP, fileTotalSize, AVUtils.base64Encode(this.fileKey));
final String joinedFileCtx = AVUtils.joinCollection(uploadFileCtxs, ",");
Request.Builder builder = new Request.Builder();
builder.url(endPoint);
builder.addHeader(HEAD_CONTENT_TYPE, TEXT_CONTENT_TYPE);
builder.addHeader(HEAD_CONTENT_LENGTH, String.valueOf(joinedFileCtx.length()));
builder.addHeader(HEAD_AUTHORIZATION, "UpToken " + this.uploadToken);
builder = builder.post(RequestBody.create(MediaType.parse(TEXT_CONTENT_TYPE), joinedFileCtx));
Response response = this.client.newCall(builder.build()).execute();
return parseQiniuResponse(response, QiniuMKFileResponseData.class);
} catch (Exception e) {
LogUtil.log.e("encounter exception during file uploading(mkfile). retry=" + retry, e);
if (retry-- > 0) {
return makeFile(fileTotalSize, uploadFileCtxs, retry);
}
}
return null;
}
use of okhttp3.MultipartBody.Builder in project HL4A by HL4A.
the class S3Uploader method executeWithRetry.
private 后端错误 executeWithRetry(byte[] data) {
if (null != data && data.length > 0) {
OkHttpClient.Builder okhttpBuilder = getOKHttpClient().newBuilder();
int timeout = (writeTimeout > 0 ? writeTimeout : getWriteTimeoutByLength(data.length));
okhttpBuilder.writeTimeout(timeout, TimeUnit.SECONDS);
final OkHttpClient httpClient = okhttpBuilder.build();
Response response = null;
String serverResponse = null;
try {
// decide file mimetype.
String mimeType = AVFileUtil.getFileMimeType(avFile);
// upload to s3
Request.Builder builder = new Request.Builder();
builder.url(uploadUrl);
// ================================================================================
// setup multi part
// ================================================================================
Charset charset = Charset.forName("UTF-8");
// support file for future
RequestBody requestBody = RequestBody.create(MediaType.parse(mimeType), data);
builder.put(requestBody);
builder.addHeader("Content-Type", mimeType);
if (!FileUploader.UPLOAD_HEADERS.containsKey(DEFAULT_HEADER_CACHE_CONTROL)) {
builder.addHeader(DEFAULT_HEADER_CACHE_CONTROL, DEFAULT_HEADER_CACHE_CONTROL_VALUE);
}
for (String key : FileUploader.UPLOAD_HEADERS.keySet()) {
builder.addHeader(key, FileUploader.UPLOAD_HEADERS.get(key));
}
// Send it
call = httpClient.newCall(builder.build());
response = call.execute();
// The 204 status code implies no response is needed
if (2 != (response.code() / 100)) {
serverResponse = AVUtils.stringFromBytes(response.body().bytes());
LogUtil.avlog.e(serverResponse);
if (retryTimes > 0) {
retryTimes--;
executeWithRetry(data);
} else {
return AVErrorUtils.createException(后端错误.OTHER_CAUSE, "upload file failure:" + response.code());
}
}
} catch (IOException exception) {
if (retryTimes > 0) {
retryTimes--;
return executeWithRetry(data);
} else {
return new 后端错误(exception.getCause());
}
}
}
return null;
}
Aggregations