如题,使用SPINGMVC接收数据
服务端接口如下
处理图片的流程如下,在使用如下的方法是容易导致绑定解析错误
修改成如下:
安卓端处理如下:最开始是打算1-1处理,最终还是整体作为RequestBody来处理,这样服务端接的时候,数据就会在multiRequest;
/***********使用OHTTP3**************/
服务端接口如下
@RequestMapping(value ="/photo", method = RequestMethod.POST)
@ApiOperation(value = "XXX", notes = "XXX")
public ResponseBean photo(@ApiParam(required = true, name = "param1", value = "XXX")@RequestParam String param1,
@ApiParam(required = true, name = "param2", value = "XXX")@RequestParam Long param2,
@ApiParam(required = true, name = "param3", value = "XXX")@RequestParam String param3,
@ApiParam(required = true, name = "param4", value = "XXX")@RequestParam int param4,
HttpServletRequest request){
处理图片的流程如下,在使用如下的方法是容易导致绑定解析错误
//MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request)
修改成如下:
public static List<FileInfo> multipartUpload(HttpServletRequest request) throws Exception {
List<FileInfo> list = new ArrayList();
MultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)) {
//转换成多部分request
//MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request);<==caused error
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
List<MultipartFile> multipartFileList = multiRequest.getFiles("file");
if (multipartFileList.size()==0){//ios版本
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
list.add(FastDFS(file));//上传头像
}
}else{//android版本
for (MultipartFile file :multipartFileList){
list.add(FastDFS(file));//上传头像
}
}
}
return list;
}
安卓端处理如下:最开始是打算1-1处理,最终还是整体作为RequestBody来处理,这样服务端接的时候,数据就会在multiRequest;
RequestBody requestBody = new MultipartBody.Builder()
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
.addFormDataPart("param1", param1)
.addFormDataPart("param2", param2)
.addFormDataPart("param3, param3)
.addFormDataPart("param4", param4+"").build();
Observable observable = connectionService.photo(requestBody).map(new HttpResultFunc<Long>());
/***********使用OHTTP3**************/
RequestBody filePartBody = new MultipartBody.Builder()
.addFormDataPart("file", pressedPhotoFile.getName(), RequestBody.create(MediaType.parse("image/*"), pressedPhotoFile))
.addFormDataPart("param1", param1)
.addFormDataPart("param2", param2)
.addFormDataPart("param3", param3)
.addFormDataPart("param4", param4).build();
Request request = new Request.Builder()
.url(ConstantNewValue.PHOTO_INTERFACE)
.post(filePartBody)
.build();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
OkHttpClient okHttpClient = builder
.connectTimeout(200, TimeUnit.SECONDS)
.addInterceptor(new ReadCookieInterceptor())
.build();
response = okHttpClient.newCall(request).execute();
String mResult = response.body().string();