Thumbnails压缩图片

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u014204541/article/details/87283746

引入jar包

	<!-- 压缩图片工具包 -->
	<dependency>
		<groupId>net.coobird</groupId>
		<artifactId>thumbnailator</artifactId>
		<version>0.4.8</version>
	</dependency>

使用:

	/**
	 * 附件上传  --2018.10.19
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "doAddAttachment")
	@ResponseBody
	public AjaxJson doAddAttachment(HttpServletRequest request,HttpServletResponse response) {
		System.out.println("附件上传");
		String idd = request.getParameter("idd");
		System.out.println("33idd:" + idd);
		String separator = File.separator;
		String message = null;
		String attPath = bundle.getString("attPath");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");	
		HttpSession session = request.getSession();
		String picType = (String)session.getAttribute("picType");
		System.out.println("picType:" + picType);
		//request.getSession().removeAttribute("picType");
		AjaxJson j = new AjaxJson();
		message = "附件上传成功";
		if ("00".equals(picType) || StringUtils.isBlank(picType)) {
			System.out.println("情况1");
			message = "请选择图片类型";
		} else {
			System.out.println("情况2");
			try {
				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
				Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
				for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
					MultipartFile file = entity.getValue();// 获取上传文件对象
					String attName = file.getOriginalFilename();
					System.out.println("文件名:" + file.getOriginalFilename());

					// 路径 示例:F:\out-put\attachment\20180914\idd\附件名
					String subPath = separator + df.format(new Date()) + separator + idd;
					String finalPath = attPath + subPath;
					File files = new File(finalPath);

					if (files.exists()) {
						if (files.isDirectory()) {
							logger.info("dir exists---文件夹已存在");
						} else {
							logger.info("the same name file exists, can not create dir");
						}
					} else {
						logger.info("dir not exists, create it ...");
						files.mkdirs();
					}

					File files2 = new File(finalPath + separator + attName);

					long size = file.getSize() / 1024;// 获取文件大小,单位kb
					System.out.println("图片大小:" + size);

					if (size > 2000) {// 图片大于200k进行压缩处理
						System.out.println("图片过大,压缩处理");
						// 压缩并保存图片,图片缩放80%, 图片质量压缩60%
						Thumbnails.of(file.getInputStream()).scale(0.8).outputQuality(0.6).toFile(files2);
					} else {
						System.out.println("不压缩");
						file.transferTo(new File(finalPath + separator + attName));// 保存到目录下
					}

					ContractAttachmentEntity att = new ContractAttachmentEntity();
					att.setContractManagementId(idd);
					att.setAttachmentName(file.getOriginalFilename());
					att.setCreateBy(ResourceUtil.getSessionUser().getUserName());
					att.setCreateTime(new Date());
					att.setPath(finalPath + separator + attName);
					att.setStatus("0");
					att.setPicType(picType);
					contractAttachmentService.save(att);
				}
			} catch (Exception e) {
				message = "附件上传失败";
				e.printStackTrace();
				logger.info(e.getMessage(),e);

			}
		}

		j.setMsg(message);
		return j;
	}

猜你喜欢

转载自blog.csdn.net/u014204541/article/details/87283746