我必须使用截击MultipartEntityBuilder将图像/音频/视频/pdf...etc文件上传到我的服务器,并且我正在使用以下代码进行Mimetype设置,但我却遇到了以下异常
有人可以帮助我如何添加多种Mime类型吗?
/**
*buildMultipartEntity
*/
private void buildMultipartEntity() {
if (mParams != null) {
for (Map.Entry<String, String> entry : mParams.entrySet()) {
mBuilder.addTextBody(entry.getKey(), entry.getValue());
}
}
if (mFileParams != null) {
for (Map.Entry<String, List<File>> entry : mFileParams.entrySet()) {
List<File> listFiles = entry.getValue();
for (File file : listFiles) {
String[] mimeTypes = {"image/*", "application/pdf"};
mBuilder.addBinaryBody(entry.getKey(), file, ContentType.create(String.valueOf(mimeTypes),""),
file.getName());
}
}
}
mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
}
}
java.lang.IllegalArgumentException: MIME type may not contain reserved characters
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err: at org.apache.http.util.Args.check(Args.java:36)
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err: at org.apache.http.entity.ContentType.create(ContentType.java:176)
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err: at org.apache.http.entity.ContentType.create(ContentType.java:204)
2018-11-29 11:14:52.955 18868-18868/com.karvyinnotech.claimsportal W/System.err: at com.karvyinnotech.claimsportal.restapi.MultipartAPI.buildMultipartEntity(MultipartAPI
答案 0 :(得分:1)
使用mime类型"*/*"
,它将接受所有类型的文件
var fileBody = ProgressRequestBody(fileToUpload, "*/*", this)
photos = MultipartBody.Part.createFormData(key, fileToUpload.name, fileBody)
就像在改造中一样,我像上面那样制作RequestBody对象,它可以接受任何类型的文件