小程序上传图片文件,JAVA服务器接收

看过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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值