这个代码有写了注释,我就不多解释了。关于Ajax同步,是因为我个人认为,上传文件这个还是做成同步比较好,等文件上传完成后,js代码才能继续执行下去。因为文件上传毕竟是一个耗时的工作,有的逻辑又确实需要当文件上传成功以后才执行,比如刷新页面,所以为了避免出现问题,还是做成同步的比较好。还有就是去掉预览,用过bootstrap-fileinput插件的都知道,这个插件的图片预览功能很强大,甚至可以单独依靠这个插件来制作相册管理。但是因为我们这次要结合Jcrop,所以要割掉这部分功能。
SpringMVC-Controller获取文件
@ResponseBody @RequestMapping(value = "/portrait", method = {RequestMethod.POST}) public JsonResult upload(HttpServletRequest request) throws Exception { Integer x = Integer.parseInt(MyStringTools.checkParameter(request.getParameter("x"), "图片截取异常:X!")); Integer y = Integer.parseInt(MyStringTools.checkParameter(request.getParameter("y"), "图片截取异常:Y!")); Integer w = Integer.parseInt(MyStringTools.checkParameter(request.getParameter("w"), "图片截取异常:W!")); Integer h = Integer.parseInt(MyStringTools.checkParameter(request.getParameter("h"), "图片截取异常:H!")); String scaleWidthString = MyStringTools.checkParameter(request.getParameter("sw"), "图片截取异常:SW!"); int swIndex = scaleWidthString.indexOf("px"); Integer sw = Integer.parseInt(scaleWidthString.substring(0, swIndex)); String scaleHeightString = MyStringTools.checkParameter(request.getParameter("sh"), "图片截取异常:SH!"); int shIndex = scaleHeightString.indexOf("px"); Integer sh = Integer.parseInt(scaleHeightString.substring(0, shIndex)); //获取用户ID用于指向对应文件夹 SysUsers sysUsers = HttpTools.getSessionUser(request); int userID = sysUsers.getUserId(); //获取文件路径 String filePath = FileTools.getPortraitPath(userID); CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); String path; //检查form中是否有enctype="multipart/form-data" if (multipartResolver.isMultipart(request)) { //将request变成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; //获取multiRequest 中所有的文件名 Iterator iterator = multiRequest.getFileNames(); while (iterator.hasNext()) { //一次遍历所有文件 MultipartFile multipartFile = multiRequest.getFile(iterator.next().toString()); if (multipartFile != null) { String[] allowSuffix = {".jpg",".JPG"}; if (!FileTools.checkSuffix(multipartFile.getOriginalFilename(), allowSuffix)) { throw new BusinessException("文件后缀名不符合要求!"); } path = filePath + FileTools.getPortraitFileName(multipartFile.getOriginalFilename()); //存入硬盘 multipartFile.transferTo(new File(path)); //图片截取 if (FileTools.imgCut(path, x, y, w, h, sw, sh)) { CompressTools compressTools = new CompressTools(); if (compressTools.simpleCompress(new File(path))) { return JsonResult.success(FileTools.filePathToSRC(path, FileTools.IMG)); } else { return JsonResult.error("图片压缩失败!请重新上传!"); } } else { return JsonResult.error("图片截取失败!请重新上传!"); } } } } return JsonResult.error("图片获取失败!请重新上传!"); }
Image图片切割
/** * 截图工具,根据截取的比例进行缩放裁剪 * * @param path 图片路径 * @param zoomX 缩放后的X坐标 * @param zoomY 缩放后的Y坐标 * @param zoomW 缩放后的截取宽度 * @param zoomH 缩放后的截取高度 * @param scaleWidth 缩放后图片的宽度 * @param scaleHeight 缩放后的图片高度 * @return 是否成功 * @throws Exception 任何异常均抛出 */ public static boolean imgCut(String path, int zoomX, int zoomY, int zoomW, int zoomH, int scaleWidth, int scaleHeight) throws Exception { Image img; ImageFilter cropFilter; BufferedImage bi = ImageIO.read(new File(path)); int fileWidth = bi.getWidth(); int fileHeight = bi.getHeight(); double scale = (double) fileWidth / (double) scaleWidth; double realX = zoomX * scale; double realY = zoomY * scale; double realW = zoomW * scale; double realH = zoomH * scale; if (fileWidth >= realW && fileHeight >= realH) { Image image = bi.getScaledInstance(fileWidth, fileHeight, Image.SCALE_DEFAULT); cropFilter = new CropImageFilter((int) realX, (int) realY, (int) realW, (int) realH); img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage bufferedImage = new BufferedImage((int) realW, (int) realH, BufferedImage.TYPE_INT_RGB); Graphics g = bufferedImage.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); //输出文件 return ImageIO.write(bufferedImage, "JPEG", new File(path)); } else { return true; } }