Search in sources :

Example 1 with UploadFileInfo

use of com.okhttplib.bean.UploadFileInfo in project OkHttp3 by MrZhousf.

the class DownUpLoadHelper method uploadFile.

/**
     * 文件上传
     */
void uploadFile(OkHttpHelper helper) {
    try {
        final HttpInfo info = httpInfo;
        List<UploadFileInfo> uploadFileList = helper.getUploadFileInfoList();
        String url = info.getUrl();
        if (TextUtils.isEmpty(url)) {
            showLog("文件上传接口地址不能为空");
            return;
        }
        StringBuilder log = new StringBuilder("PostParams: ");
        MultipartBody.Builder mBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        ProgressCallback progressCallback = helper.getProgressCallback();
        String logInfo;
        if (null != info.getParams() && !info.getParams().isEmpty()) {
            for (String key : info.getParams().keySet()) {
                mBuilder.addFormDataPart(key, info.getParams().get(key));
                logInfo = key + " =" + info.getParams().get(key) + ", ";
                log.append(logInfo);
            }
        }
        for (UploadFileInfo fileInfo : uploadFileList) {
            if (progressCallback == null) {
                progressCallback = fileInfo.getProgressCallback();
            }
            String filePath = fileInfo.getFilePathWithName();
            String interfaceParamName = fileInfo.getInterfaceParamName();
            File file = new File(filePath);
            log.append(interfaceParamName);
            log.append("=");
            log.append(filePath);
            log.append(",");
            mBuilder.addFormDataPart(interfaceParamName, file.getName(), RequestBody.create(MediaTypeUtil.fetchFileMediaType(filePath), file));
        }
        showLog(log.toString());
        RequestBody requestBody = mBuilder.build();
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url(url).post(new ProgressRequestBody(requestBody, progressCallback, timeStamp, requestTag));
        helper.getHttpHelper().addHeadsToRequest(info, requestBuilder);
        Request request = requestBuilder.build();
        helper.setRequest(request);
        helper.getHttpHelper().responseCallback(helper.doRequestSync(), progressCallback, OkMainHandler.RESPONSE_UPLOAD_CALLBACK, requestTag);
    } catch (Exception e) {
        showLog("上传文件失败:" + e.getMessage());
    }
}
Also used : ProgressCallback(com.okhttplib.callback.ProgressCallback) Request(okhttp3.Request) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) HttpInfo(com.okhttplib.HttpInfo) UploadFileInfo(com.okhttplib.bean.UploadFileInfo) MultipartBody(okhttp3.MultipartBody) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ProgressRequestBody(com.okhttplib.progress.ProgressRequestBody) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.okhttplib.progress.ProgressRequestBody)

Aggregations

HttpInfo (com.okhttplib.HttpInfo)1 UploadFileInfo (com.okhttplib.bean.UploadFileInfo)1 ProgressCallback (com.okhttplib.callback.ProgressCallback)1 ProgressRequestBody (com.okhttplib.progress.ProgressRequestBody)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 MultipartBody (okhttp3.MultipartBody)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1