java 上传并压缩图片
2012-07-27 15:57
369 查看
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; }
相关文章推荐
- 使用lrz压缩上传图片,后台使用java
- Java图片上传压缩处理
- java 上传图片 并压缩图片大小
- java 使用Thumbnailator 上传图片 并压缩图片大小
- Java实现图片的压缩(包括文件上传的图片压缩)
- 使用Java上传图片经过压缩处理图片变色问题
- Java - MultipartFile图片上传服务器,并且指定大小压缩
- 基于spring-boot的文件上传和下载(包括图片的压缩)--第一篇(Java代码)
- Java图片上传压缩处理
- java 上传图片 并压缩图片大小(转)
- java input 调用手机相机和本地照片上传图片到服务器然后压缩的方法
- java 上传图片 并压缩图片大小
- java上传并压缩图片(等比例压缩或者原尺寸压缩)
- java 上传图片 并压缩图片大小
- Java图片上传压缩处理
- Java图片上传压缩处理
- java上传图片压缩大小
- Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
- java 上传图片的压缩与放大
- java实现上传图片并压缩图片大小功能