關于文件上傳MultipartBody的使用方法
文件上傳MultipartBody的使用
最近有使用一個文件上傳的功能,需要在請求中添加文件,一起傳給服務器
Okhttp提供了這個文件添加然后上傳的功能
下面給出核心的代碼,然后分析一下
//多個文件上傳,Filelist
private static Request getFilesRequest(String url, List<File> files, Map<String, String> maps){
MultipartBody.Builder builder= new MultipartBody.Builder().setType(MultipartBody.FORM);
if(maps==null){
for(int i = 0;i < files.size();i++){
builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"file.jpg\""),
RequestBody.create(MediaType.parse("image/png"),files.get(i))
).build();
}
}else{
for (String key : maps.keySet()) {
String str = maps.get(key);
builder.addFormDataPart(key,str );
}
for(int j = 0;j < files.size();j++){
long fileSize = files.get(j).length();
builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"file.jpg\";filesize="+fileSize),
RequestBody.create(MediaType.parse("image/png"),files.get(j))
);
}
}
RequestBody body=builder.build();
return new Request.Builder().url(url).post(body).build();
}先說三個參數吧
- 三個參數第一個是請求的URL
- 第二個是Multipart的文件list
- 第三個是headermap,就是請求的請求頭params
首先通過Multipart的Builder模式實例化一個builder
其次如果Header的map為空則直接將file加入到part中
否則依次將headermap 和 file的list中的數據加入到Request中
完成后builder build 出來的MultipartBody 請求賦值給 RequestBody(Multipartbody繼承了Requestbody)
之后將Request構建完成即可
Call call =mOkhttpClient.newCall(request);
然后就是用Okhttp 進行請求,請求方法略過
MultipartBody取出key,value數據,打印參數
MultipartBody打印參數比較麻煩
kotlin:
if (requestBody is MultipartBody) {
val params = mutableMapOf<String, String>()
val files = mutableMapOf<String, String>()
requestBody.parts().forEach {
val body = it.body()
it.headers()?.let {
val header = it.value(0)
val split = header.replace(" ", "").replace("\"", "").split(";")
when (split.size) {
2 -> {
//文本參數
val keys = split[1].split("=")
if (keys.size > 1 && body.contentLength() < 1024) {
val key = keys[1]
val buffer = Buffer()
body.writeTo(buffer)
val value = buffer.readUtf8()
params[key] = value
}
}
3 -> {
//文件
val fileKeys = split[1].split("=")
val fileKey = if (fileKeys.size > 1) {
fileKeys[1]
} else ""
val nameValue = split[2].split("=")
val fileName = if (nameValue.size > 1) nameValue[1] else ""
files[fileKey] = fileName
}
}
}
}
println("文件-->$files")
println("文本-->$params")
}java寫法
if (requestBody instanceof MultipartBody) {
MultipartBody body = (MultipartBody) requestBody;
Map<String, String> params = new HashMap<>();
Map<String, String> files = new HashMap<>();
for (MultipartBody.Part part : body.parts()) {
RequestBody body1 = part.body();
Headers headers = part.headers();
if (headers != null && headers.size() > 0) {
String[] split = headers.value(0).replace(" ", "").replace("\"", "").split(";");
if (split.length == 2) {
//文本
String[] keys = split[1].split("=");
if (keys.length > 1 && body1.contentLength() < 1024) {
String key = keys[1];
String value = "";
Buffer buffer = new Buffer();
body.writeTo(buffer);
value = buffer.readUtf8();
params.put(key, value);
}
} else if (split.length == 3) {
//文件
String fileKey = "";
String fileName = "";
String[] keys = split[1].split("=");
String[] names = split[2].split("=");
if (keys.length > 1) fileKey = keys[1];
if (names.length > 1) fileName = names[1];
files.put(fileKey, fileName);
}
}
}
System.out.println("文本參數-->" + params);
System.out.println("文件參數-->" + files);
}以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Boot監(jiān)聽Redis Key失效事件實現定時任務的示例
這篇文章主要介紹了Spring Boot監(jiān)聽Redis Key失效事件實現定時任務的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04

