最近写上传照片时出现不少问题,今天把这些问题总结下,方便日后使用
问题:1.一个对象中有个属性是头像,之前是一个字段一个字段传值,这样都OK,但是前端拼接url太麻烦,想着能不能传一个对象(字段不包含头像属性),再传一个头像属性,但是各种报错,上网查找说是@RequestBody传对象时和MultipartFile无法共存
解决方法:一个接口这样无法实现他两共存的问题,那就写两个接口,一个接口只是上传图片,给前端返回图片路径,前端把此路径赋值给此对象的相应属性上,第二个接口把此对象(包含头像属性)传给后端,这样就OK了;
问题:2.如果是传多张照片呢?思路还是一样,两个接口,一个接口专门传照片,返回一个集合给前端,前端把此集合赋值给对象,(此对象中多一个属性是private List<String> stringList;),第二个接口专门负责传对象(包含此private List<String> stringList集合),剩下的后端去处理,我这块传多张照片是一对多,逻辑自己去写,我这块就不多啰嗦了!
下面附上我的controller层代码:
单张照片:
/**
* 给前端返回头像地址
* @param image
* @return
* @throws IOException
*/
@PostMapping("/uploadImageFile")
@ApiOperation(value="编辑个人资料时上传头像",notes="照片(headPortrait)")
@ApiResponses(value = { })
public BaseResult uploadImageFile(
@RequestParam @ApiParam(name="image",value="图片",required=true) MultipartFile image) throws IOException {
String videoCoverPath = imgUtil.save(image, PathConstant.HEADPORTRAIT_PATH);
if (videoCoverPath.equals("WRONG_FILE_EXTENSION") || videoCoverPath.equals("FILE_EMPTY") || videoCoverPath.equals("CANNOT_DECODE_IMAGE")) {
return BaseResultUtil.error(ResultEnum.OPERATE_FAIL);
}
return BaseResultUtil.success(videoCoverPath);
}
/**
* 传此对象(对象中包含头像的属性)
* @param userInfo
* @return
* @throws Exception
*/
@PostMapping("/editProfile")
@ApiOperation(value="编辑个人资料",notes="对象【用户id(userId),个人头像(headPortrait),昵称(nickname),个性签名(idiograph),省份(province),市(city),性别(sex),年龄(age),身高(height),体重(weight),教练个人资料简介(briefIntroduction)】")
@ApiResponses(value = { })
public BaseResult editProfile(
@RequestBody(required=false) @ApiParam(name="userInfo",required=false) UserInfo userInfo) throws Exception{
return userInfoService.editProfile(userInfo);
}
多张照片:
/**
* 给前端返回照片地址 List<String>
*/
/**
* swgger2上传多张照片出错,头部信息一直是application/json,所以添加headers="content-type=multipart/form-data"
* @param image
* @return
* @throws IOException
*/
@PostMapping(value="/uploadImageFile",consumes="multipart/*",headers="content-type=multipart/form-data")
@ApiOperation(value="教练资质审核上传多张照片",notes="图片(qualitycerList)")
@ApiResponses(value = { })
public BaseResult uploadImageFile(
@RequestParam @ApiParam(name="image",value="资质认证图片",required=true) MultipartFile[] image) throws IOException {
List<String> qualitycerList=new ArrayList<String>();
for (MultipartFile multipartFile : image) {
String videoCoverPath = imgUtil.save(multipartFile, PathConstant.QUALIFICATION_PATH);
if (videoCoverPath.equals("WRONG_FILE_EXTENSION") || videoCoverPath.equals("FILE_EMPTY") || videoCoverPath.equals("CANNOT_DECODE_IMAGE")) {
return BaseResultUtil.error(ResultEnum.OPERATE_FAIL);
}
qualitycerList.add(videoCoverPath);
}
return BaseResultUtil.success(qualitycerList);
}
/**
* 传对象 包含List<String>照片地址集合的属性
* @param coachDetails
* @return
* @throws Exception
*/
@PostMapping("/insertAptitude")
@ApiOperation(value="教练资质审核添加",notes="对象【姓名(name),性别(sex),年龄(age),电话(phone),教练所属(coachOfBelonging),所属工作室(studio),所属健身房(fitnessRoom),简介(reviewSummary)】")
@ApiResponses(value = { })
public BaseResult insertAptitude(
@RequestBody(required=false) @ApiParam(name="coachDetails",required=false) CoachDetails coachDetails) throws Exception{
return coachDetailsService.insertAptitude(coachDetails);
}
补充:最近看到别人可以这样做,参考网址:https://ask.csdn.net/questions/695481