使用Retrofit2.0+spingmvc上传图片和参数

如题,使用SPINGMVC接收数据

服务端接口如下

@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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值