上下架操作失败(上传图片不能为空)
来源:8-11 商品列表展示之前端开发
哎呦喂啊哈
2019-02-12
在调试上下架功能的时候发现未能进入if (multipartResolver.isMultipart(request)) 的判断,返回错误信息,上传图片不能为空。而在测试编辑功能的时候不上传图片则可以进入if判断。
写回答
2回答
-
哈,不好意思,昨晚熬夜太严重短路了,代码逻辑应该是这样的
try { if (multipartResolver.isMultipart(request)) { thumbnail = handleImage(request, thumbnail, productImgList); } } catch (Exception e) { modelMap.put("success", false); modelMap.put("errMsg", e.toString()); return modelMap; }
这样才不会去检查如果request里没图片就卡住
032019-02-12 -
翔仔
2019-02-12
同学好,上下架调用的是modifyShop吧?怎么感觉同学这个是registerShop的逻辑。。
同学可以看看我的
@RequestMapping(value = "/modifyshop", method = RequestMethod.POST) @ResponseBody private Map<String, Object> modifyShop(HttpServletRequest request) { Map<String, Object> modelMap = new HashMap<String, Object>(); if (!CodeUtil.checkVerifyCode(request)) { modelMap.put("success", false); modelMap.put("errMsg", "输入了错误的验证码"); return modelMap; } // 1.接收并转化相应的参数,包括店铺信息以及图片信息 String shopStr = HttpServletRequestUtil.getString(request, "shopStr"); ObjectMapper mapper = new ObjectMapper(); Shop shop = null; try { shop = mapper.readValue(shopStr, Shop.class); } catch (Exception e) { modelMap.put("success", false); modelMap.put("errMsg", e.getMessage()); return modelMap; } CommonsMultipartFile shopImg = null; CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); if (commonsMultipartResolver.isMultipart(request)) { MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg"); } // 2.修改店铺信息 if (shop != null && shop.getShopId() != null) { ShopExecution se; try { if (shopImg == null) { se = shopService.modifyShop(shop, null); } else { ImageHolder imageHolder = new ImageHolder(shopImg.getOriginalFilename(), shopImg.getInputStream()); se = shopService.modifyShop(shop, imageHolder); } if (se.getState() == ShopStateEnum.SUCCESS.getState()) { modelMap.put("success", true); } else { modelMap.put("success", false); modelMap.put("errMsg", se.getStateInfo()); } } catch (ShopOperationException e) { modelMap.put("success", false); modelMap.put("errMsg", e.getMessage()); } catch (IOException e) { modelMap.put("success", false); modelMap.put("errMsg", e.getMessage()); } return modelMap; } else { modelMap.put("success", false); modelMap.put("errMsg", "请输入店铺Id"); return modelMap; } }
012019-02-12
相似问题