Java實現等比例壓縮
1. 使用Java源生類實現
2. 使用第三方插件(Thumbnailator)
開發步驟:
- 創建工程
- 編寫頁面
- 編寫Controller
- 編寫業務邏輯
- HTML代碼
<!DOCTYPE HTML> <html> <head> <title>Java實現圖片等比例壓縮</title> </head> <body> <form action="uploadFile/upload" method="post" enctype="multipart/form-data"> <input type="file" name="fileNamePath" /> <input type="submit" value="提交" /> </form> </body> </html>
- 控制器
@RequestMapping("upload") public String thumbnail(@RequestParam("fileNamePath") CommonsMultipartFile file, HttpSession session, Map<String, Object> map) { // 圖片保存的的文件名稱 String uploadPath = "images"; // 項目發布的路徑 String realUploadPath = session.getServletContext().getRealPath(uploadPath); String imgeUrl = ""; String thumbnailImageUrl = ""; // 上傳圖片 imgeUrl = uploadService.uploadImage(file, uploadPath, realUploadPath); // 壓縮圖片 thumbnailImageUrl = thumbnailService.thumbnail(file, uploadPath, realUploadPath); map.put("imgeUrl", imgeUrl); map.put("thumbnailImageUrl", thumbnailImageUrl); return "showImage"; }
- 業務邏輯
- 上傳圖片
@Override public String uploadImage(CommonsMultipartFile file, String uploadPath, String realUploadPath) { InputStream is = null; OutputStream os = null; try { is = file.getInputStream(); String des = realUploadPath + File.separator + file.getOriginalFilename(); os = new FileOutputStream(des); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) > 0) { os.write(buffer); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return uploadPath + File.separator + file.getOriginalFilename(); }
-
- Thumbnailator 等比例壓縮圖片
public static final int WINDTH = 68; public static final int HEIGHT = 68; public String thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath) { try { String des = realUploadPath + "/thum_" + file.getOriginalFilename(); Thumbnails.of(file.getInputStream()).size(WINDTH, HEIGHT).toFile(des); } catch (IOException e) { e.printStackTrace(); } return uploadPath + "/thum_" + file.getOriginalFilename(); }
-
- Java源生等比例壓縮圖片
public String thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath) { OutputStream os = null; String desc = realUploadPath + "/thum_" + file.getOriginalFilename(); try { os = new FileOutputStream(desc); Image image = ImageIO.read(file.getInputStream()); int width = image.getWidth(null);// 獲得原圖的寬度 int height = image.getHeight(null);// 獲得原圖的高度 int rete1 = width / WIDTH;// 寬度縮略比例 int rete2 = height / HEIGHT;// 高度縮略比例 int rete = 0; if (rete1 > rete2) { rete = rete1; } else { rete = rete2; } // 計算縮略圖最總的寬度和高度 int newWidth = width / rete; int newHeight = height / rete; BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, image.SCALE_SMOOTH), 0, 0, null); String imageType = file.getContentType().substring(file.getContentType().indexOf("/") + 1); ImageIO.write(bufferedImage, imageType, os); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return uploadPath + "/thum_" + file.getOriginalFilename(); }
附件一:Maven添加Thumbnailatorjar
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
附件二:項目實例代碼
https://git.oschina.net/hxxiaodao/Thumbnail.git