看过2个博主的解决方案,亲测都有效:
https://blog.csdn.net/lili625/article/details/77783300
假如springboot中,
List<FileItem> list = upload.parseRequest(request);
获取长度为0
请看https://blog.csdn.net/life_360/article/details/80556844
我自己的后端框架springboot代码:
/**
* 扫图识别并记录行为结果
* @param file
* @return
*/
@RequestMapping(value = "/recognizeARCode", method = RequestMethod.POST)
public ApiResponse recognizeARCode(@RequestParam(value="file",required = false) MultipartFile file,
@RequestParam(value="memberId",required = false) String memberId,
@RequestParam(value="memberNo",required = false) String memberNo,
@RequestParam(value="telNo",required = false) String telNo,
@RequestParam(value="openid",required = false) String openid){
String hotelGroupCode=SystemContext.getHotelGroupCode();
String hotelCode=SystemContext.getHotelCode();
String arCloudKey=SysOptionsCacheManager.getSysOptionsValue(hotelGroupCode,hotelCode,"ARCloudKey");
String arCloudSecret=SysOptionsCacheManager.getSysOptionsValue(hotelGroupCode,hotelCode,"ARCloudSecret");
String arCloudUrl="http://"+SysOptionsCacheManager.getSysOptionsValue(hotelGroupCode,hotelCode,"ARCloudURL")+"/search";
WebAR webAR = new WebAR(arCloudKey, arCloudSecret, arCloudUrl);
String errorMsg="";
ArLogoScanRecord arLogoScanRecord=new ArLogoScanRecord();
arLogoScanRecord.setId(UUIDUtils.getUUIDNo());
arLogoScanRecord.setCreateDatetime(new Date());
arLogoScanRecord.setGetItemType("p");
arLogoScanRecord.setIsHalt("F");
arLogoScanRecord.setHotelGroupCode(hotelGroupCode);
arLogoScanRecord.setHotelCode(hotelCode);
if(StringUtils.isNotBlank(memberId)){
arLogoScanRecord.setMemberId(memberId);
}
if(StringUtils.isNotBlank(memberNo)){
arLogoScanRecord.setMemberNo(memberNo);
}
if(StringUtils.isNotBlank(telNo)){
arLogoScanRecord.setTelNo(telNo);
}
if(StringUtils.isNotBlank(openid)){
arLogoScanRecord.setOpenid(openid);
}
try {
// 图片的base64数据,使用前请更换为你的图片数据
String image=Base64.encode(file.getBytes());
ARResultInfo info = webAR.recognize(image);
if (info.getStatusCode() == 0) {
// statusCode为0时,识别到目标,数据在target中
//记录扫图行为及结果
arLogoScanRecord.setLogoId(info.getResult().getTarget().getTargetId());
arLogoScanRecordService.insert(arLogoScanRecord);
return successReturn(arLogoScanRecord.getId());
} else {
// 未识别到目标
// System.out.println(info.getStatusCode());
errorMsg="错误代码:"+info.getStatusCode()+","+info.getResult().getMessage();
arLogoScanRecord.setErrMsg(info.getStatusCode()+","+info.getResult().getMessage());
arLogoScanRecordService.insert(arLogoScanRecord);
}
} catch (Exception e) {
errorMsg=e.getMessage();
}
return failReturn(errorMsg);
}
wx.uploadFile({
url: 'https://xxxxxx/upload/picture',
filePath: filePath,//图片路径,如tempFilePaths[0]
name: 'file',
header : {
"Content-Type": "multipart/form-data"
},
formData:
{
userId: 12345678 //举例说明:附加信息为用户ID
},
success: function (res) {
console.log(res);
},
fail: function (res) {
console.log(res);
},
complete: function (res) {
}
})
注意:
@RequestParam(value="file",required = false) MultipartFile file //一定要与js中name: 'file'名称对应。js中一定要加上header