首页 > 代码库 > Android 使用okhtth上传图片multipart/form-data
Android 使用okhtth上传图片multipart/form-data
自己整这个问题整了半天,写篇博客记录下
private void uploadMultiFile() {
String imageType = "multipart/form-data";
File file = new File(imgUrl);imgUrl为图片位置
RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "head_image", fileBody)
.addFormDataPart("imagetype", imageType)
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
final okhttp3.OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
OkHttpClient okHttpClient = httpBuilder
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
String htmlStr = response.body().string();
Log.i("result", htmlStr);
}
});
}
Android 使用okhtth上传图片multipart/form-data
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。