HarmonyOS应用开发-okhttp3.0快速集合文件上传

鸿蒙时代
发布于 2022-3-8 10:17
浏览
0收藏

应用开发过程中经常需要进行文件上传功能开发,通过okhttp3.0可以快速集合完成文件上传的功能。
代码如下:

        MultipartBody.Builder mBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        int i = 0;
        for(String filePath :filelist) {
            File file = new File(filePath);
            if (!file.exists()){
                Toast.makeText(MainActivity.this,"上传"+filePath+"文件不存在!", Toast.LENGTH_SHORT).show();
                continue;
            }
            String  fileMimeType = getMimeType(file);
            //这里获取文件类型,方法自己定义
            MediaType mediaType = MediaType.parse(fileMimeType);
            RequestBody fileBody = RequestBody.create(mediaType, file);
            mBody.addFormDataPart("file" + i, file.getName(), fileBody);
            i++;
        }
        RequestBody requestBody = mBody.build();
        Request requestPostFile = new Request.Builder()
                .url("http://www.jianshu.com/")
                .post(requestBody)
                .build();
         ...

分类
标签
HarmonyOS应用开发-okhttp3.0快速集合文件.docx 17.3K 5次下载
1
收藏
回复
举报
回复
    相关推荐