FromData接收文件
@ResponseBody @RequestMapping(value="commentPicture") public AjaxJson commentPicture(@ModelAttribute Picture picture, MultipartHttpServletRequest multiRequest, HttpServletRequest request, HttpServletResponse response) { AjaxJson j = new AjaxJson(); try { Comment comment = CommentService.queryById(picture.getCommentId()); //使用MultipartHttpServletRequest根据前台参数接收文件 MultipartFile file = multiRequest.getFile("imgFile"); if (file == null) { j.setSuccess(false); j.setMsg("未获取到图片"); return j; } //通过构造器传入文件上传的参数,开启一个子线程,并在子线程中定义一个属性,赋值为文件存放的地址 UploadImgUtil uploadImgUtil = new UploadImgUtil(file, filePath, "comment"); // String imgUrl = UploadImgUtil.uploadImg(file, filePath, "tymall/comment"); //开启线程 uploadImgUtil.run(); //获取文件图片存放地址 String imgUrl = uploadImgUtil.getImgUrl(); //评论图片 picture.setCreateTime(new Date()); picture.setImgurl(imgUrl); picture.setProductId(comment.getProductId()); picture.setRemark(""); // tymallCommentPictureService.doAdd(picture); j.setSuccess(true); j.setMsg("评论成功"); } catch (Exception e) { e.printStackTrace(); j.setSuccess(false); j.setMsg("评论失败"); } return j; }