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; }
java 上传并压缩图片
最新推荐文章于 2024-07-14 19:55:19 发布