java 上传并压缩图片

protected Map<String, Object> upload(MultipartHttpServletRequest req) throws IOException {
		String root = PropertiesHelper.getProperty("upload_photo_root");
		String tempPath = PropertiesHelper.getProperty("upload_photo_path");
		Map<String, Object> result = new HashMap<String, Object>();
		if (req.getFile("uploadFile") != null) {
			MultipartFile file = req.getFile("uploadFile");
			
			BufferedImage newImage=getNewImage(file,548,381);
			
			
			if (file.getSize() > 0) {
				String path = root + tempPath;
				path = path.replace('\\', '/');
				File directory = new File(path);
				if (!directory.exists()) {
					directory.mkdirs();
				}
				String pre = "RID" + req.getParameter("reportId") + "_"
						+ String.valueOf(System.currentTimeMillis());
				// File destFile = new File(path + pre +
				// file.getOriginalFilename().toLowerCase());

				// String fileType =
				// file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				String fileType = ".png";
				File destFile = new File(path + pre + fileType);
				try {
					//FileUtils.writeByteArrayToFile(destFile, file.getBytes());
					ImageIO.write(newImage, "png", destFile);
					result.put("success", true);
					// result.put("url", path + pre +
					// file.getOriginalFilename().toLowerCase());
					result.put("url", path + pre + fileType);
				} catch (IOException e) {
					result.put("success", false);
					e.printStackTrace();
				}
			}
		} else {
			result.put("success", false);
		}
		return result;
	}
private  BufferedImage  getNewImage(MultipartFile oldImage,double width,double height) throws IOException{
		/*srcURl 原图地址;deskURL 缩略图地址;comBase 压缩基数;scale 压缩限制(宽/高)比例*/
		
		ByteArrayInputStream bais = new ByteArrayInputStream(oldImage.getBytes()); 
		MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(bais);		
		Image src = ImageIO.read(mciis);
		double srcHeight = src.getHeight(null);
		double srcWidth = src.getWidth(null);
		double deskHeight = 0;//缩略图高
		double deskWidth  = 0;//缩略图宽
		if (srcWidth>srcHeight) {
	        
	        if (srcWidth>width) {
	            if (width/height>srcWidth/srcHeight) {
	            	deskHeight = height;
	            	deskWidth = srcWidth/(srcHeight/height);
	            }
	            else {
	            	deskHeight = width/(srcWidth/srcHeight);
	            	deskWidth = width;
	            }
	        }
	        else {
	            
	            if (srcHeight>height) {
	            	deskHeight = height;
	            	deskWidth = srcWidth/(srcHeight/height);
	            }else {
	            	deskHeight=srcHeight;
	                deskWidth=srcWidth;
	            }
	            
	        }
	        
	    }
	    else if(srcHeight>srcWidth)
	    {
	        if (srcHeight>(height)) {
	            if ((height)/width>srcHeight/srcWidth) {
	            	deskHeight = srcHeight/(srcWidth/width);
	            	deskWidth = width;
	            }else {
	            	deskHeight = height;
	                deskWidth = (height)/(srcHeight/srcWidth);
	            }
	        }
	        else {
	            if (srcWidth>width) {
	            	deskHeight = srcHeight/(srcWidth/width);
	                deskWidth = width;
	            }else {
	            	deskHeight=srcHeight;
	                deskWidth=srcWidth;
	            }

	        }
	        
	    }
	    else if (srcWidth==srcHeight) {
	        
	        if (width>=(height)&&srcHeight>(height)) {
	        	deskWidth=(height);
	            deskHeight=(height);
	        }
	        else if (width<=(height)&&srcWidth>width) {
	        	deskWidth=width;
	            deskHeight=width;
	        }
	        else  if (width==(height)&&srcWidth<width) {
	        	deskWidth=srcWidth;
	            deskHeight=srcHeight;
	        }
	        else {
	        	deskHeight=srcHeight;
	            deskWidth=srcWidth;
	        }

	    }
		BufferedImage tag = new BufferedImage((int)deskWidth,(int)deskHeight,
		    BufferedImage.TYPE_3BYTE_BGR);
		tag.getGraphics().drawImage(src, 0, 0, (int)deskWidth, (int)deskHeight, null); //绘制缩小后的图
		return tag;
	}





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值