Spring Boot —— 圖片上傳生態類
阿新 • • 發佈:2019-02-15
寫在前面
在常老師寫的spring-mvc圖片上傳工具類UploadResolver的基礎上,重寫了這個 上傳圖片的工具類。增加了幾個圖片處理的方法(按比例縮放,指定寬高縮放,指定座標裁剪)。我稱之為springboot檔案上傳生態類。
專案結構為Springboot+mybatis
jdk 1.8
工具 idea
貼出這幾個類的大致功能。
spring-boot取消了大多數的配置檔案,因此圖片上傳的一些基礎配置資料被放置在了application.properties檔案或application.yml檔案中。(此處小編選用的是yml檔案)相關配置項為:
upload:
uploadPath: D:/image 儲存檔案的本地路徑
thumbDir: thumbs 縮圖、裁剪圖等存放的資料夾
imageHeight: 100 預設縮略後的高度
imageFormat: jpg 統一儲存格式
大家需要什麼配置,可自行新增。
UploadResolverProperties.java
/**
* @author longping jie
* @name UploadResolverProperties
* @description the class is 上傳檔案基本配置專案,從配置檔案中讀取資料
* @date 2017/9/22
*/
@ConfigurationProperties("upload")
public class UploadResolverProperties {
private String uploadPath;
private String thumbDir;
private Integer imageHeight;
private String imageFormat;
public String getUploadPath() {
return uploadPath;
}
public void setUploadPath (String uploadPath) {
this.uploadPath = uploadPath;
}
public String getThumbDir() {
return thumbDir;
}
public void setThumbDir(String thumbDir) {
this.thumbDir = thumbDir;
}
public Integer getImageHeight() {
return imageHeight;
}
public void setImageHeight(Integer imageHeight) {
this.imageHeight = imageHeight;
}
public String getImageFormat() {
return imageFormat;
}
public void setImageFormat(String imageFormat) {
this.imageFormat = imageFormat;
}
}
HandleImageWays.java
/**
* @author longping jie
* @name HandleImageWays
* @description the class is 處理圖片的方法型別常量的類
* @date 2017/9/22
*/
public class HandleImageWays {
//按比例縮放一張圖片
public static final String SCALING_IMAGE="SCALING_IMAGE";
//按寬高縮放一張圖片
public static final String SCALING_IMAGE_WH="SCALING_IMAGE_WH";
//指定座標裁剪一張圖片
public static final String TRIMIMAGE_POINT="TRIMIMAGE_POINT";
}
HandleImageDataModel.java
/**
* @author longping jie
* @name HandleImage
* @description the class is 裁剪圖片的資料模型類
* 圖片縮放等配置資料項
* @date 2017/9/21
*/
public class HandleImageDataModel {
//圖片當前顯示在畫布上的的高度
private int presentHeight;
//圖片當前顯示在畫布上的的寬度
private int presentWidth;
//裁剪的x座標
private int x;
//裁剪的y座標
private int y;
//裁剪的高度
private int h;
//裁剪的寬度
private int w;
//裁剪後需要的寬度
private int need_w;
//裁剪後需要的高度
private int need_h;
//需要縮放的比例
private double scaling;
//需要縮放的寬度
private int scaling_w;
//需要縮放的高度
private int scaling_h;
public int getPresentHeight() {
return presentHeight;
}
public void setPresentHeight(int presentHeight) {
this.presentHeight = presentHeight;
}
public int getPresentWidth() {
return presentWidth;
}
public void setPresentWidth(int presentWidth) {
this.presentWidth = presentWidth;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public int getNeed_w() {
return need_w;
}
public void setNeed_w(int need_w) {
this.need_w = need_w;
}
public int getNeed_h() {
return need_h;
}
public void setNeed_h(int need_h) {
this.need_h = need_h;
}
public double getScaling() {
return scaling;
}
public void setScaling(double scaling) {
this.scaling = scaling;
}
public int getScaling_w() {
return scaling_w;
}
public void setScaling_w(int scaling_w) {
this.scaling_w = scaling_w;
}
public int getScaling_h() {
return scaling_h;
}
public void setScaling_h(int scaling_h) {
this.scaling_h = scaling_h;
}
}
UploadResolver.java
/**
* @author longping jie
* @name UploadResolver
* @description the class is 上傳圖片的工具類,封裝有圖片縮放和剪裁
* @date 2017/9/22
*/
public class UploadResolver {
@Autowired
UploadResolverProperties uploadResolverProperties;
String dateName;
public UploadResolver() {
LocalDate dt = LocalDate.now();
int year = dt.getYear();
int month = dt.getMonthValue();
int date = dt.getDayOfMonth();
String monthStr = month + "";
String dateStr = date + "";
if (month < 10) {
monthStr = "0" + monthStr;
}
if (date < 10) {
dateStr = "0" + date;
}
dateName = String.format("%s-%s-%s", year, monthStr, dateStr);
}
/**
* 圖片上傳的類
*
* @param image 需要上傳的圖片檔案物件
* @param fileName 指定一個名字
* @param handleImageWays 處理圖片的方式,如縮放,裁剪等
* @param dataModel 處理圖片的資料模型
* @throws IOException
*/
public void saveImage(MultipartFile image, String fileName, String handleImageWays, HandleImageDataModel dataModel) throws IOException {
String imageName = String.format("%s/%s/%s.jpg", uploadResolverProperties.getUploadPath(),
dateName, fileName);
String thumbName = String.format("%s/%s/%s/%s.jpg", uploadResolverProperties.getUploadPath(),
dateName, uploadResolverProperties.getThumbDir(), fileName);
File imageFile = new File(imageName);
File thumbFile = new File(thumbName);
if (!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
thumbFile.getParentFile().mkdirs();
}
image.transferTo(imageFile);
switch (handleImageWays){
case HandleImageWays.SCALING_IMAGE:
changeSize_Scaling(imageFile,thumbFile,dataModel);
break;
case HandleImageWays.SCALING_IMAGE_WH:
changeSize_WH(imageFile,thumbFile,dataModel);
break;
case HandleImageWays.TRIMIMAGE_POINT:
trimImage(imageFile,thumbFile,dataModel);
break;
}
}
/**
* 指定寬度高度縮放圖片
*
* @param file 原圖片檔案
* @param newFile 目標圖片檔案
*/
private void changeSize_WH(File file, File newFile,HandleImageDataModel dataModel) throws IOException {
//變為400*300,遵循原圖比例縮或放到400*某個高度
Thumbnails.of(file).size(dataModel.getScaling_w(), dataModel.getScaling_h()).toFile(newFile);
}
/**
* 指定比例縮放圖片
*
* @param file
* @param newFile
* @throws IOException
*/
private void changeSize_Scaling(File file, File newFile, HandleImageDataModel dataModel) throws IOException {
Thumbnails.of(file).scale(dataModel.getScaling()).toFile(newFile);//按比例縮小
}
/**
* 判斷一個檔案是否是圖片型別
*
* @param type 檔案型別
* @return 是否是圖片型別
*/
public boolean checkImageFileType(String type) {
String[] types = {".PNG", ".JPG", ".JPEG", ".BMP", ".GIF"};
boolean res = false;
for (String t : types) {
if (t.equals(type.toUpperCase())) {
res = true;
break;
}
}
return res;
}
/**
* 用uuid重新命名檔案
*
* @param fileName 檔案原名
* @return 檔案新的名字
*/
private String createFileName(String fileName) {
return UUID.randomUUID().toString().replaceAll("-", "") + "." +
fileName.substring(fileName.lastIndexOf(".") + 1);
}
/**
* 裁剪圖片
*
* @param file 原圖片檔案
* @param newFile 新圖片檔案
* @param dataModel 裁剪需要的資料模型
*/
private void trimImage(File file, File newFile, HandleImageDataModel dataModel) throws IOException {
BufferedImage buf = Thumbnails.of(file).width(dataModel.getPresentWidth()).height(dataModel.getPresentHeight())
.asBufferedImage();
Thumbnails.of(buf).sourceRegion(dataModel.getX(), dataModel.getY(), dataModel.getW(), dataModel.getH())
.size(dataModel.getNeed_w(), dataModel.getNeed_h()).toFile(newFile);
}
@Override
public String toString() {
return "UploadResolver{" +
"uploadResolverProperties=" + uploadResolverProperties +
", dateName='" + dateName + '\'' +
'}';
}
UploadResolverConfig.java
/**
* @author longping jie
* @name UploadResolverConfig
* @description the class is 裝載UploadResolver物件
* @date 2017/9/22
*/
@Configuration
@EnableConfigurationProperties(UploadResolverProperties.class)
public class UploadResolverConfig {
@Bean
public UploadResolver getUploadResolver(){
return new UploadResolver();
}
}
使用時
@Autowired
UploadResolverConfig uploadResolverConfig;
@Test
public void findOne() {
//直接呼叫UploadResolver中的方法,並指定處理圖片的方式和響應的資料模型。
System.out.println(uploadResolverConfig.getUploadResolver());
}
相關類還沒有在前端頁面中進行具體除錯,貼出來僅供大家參考,同時,程式碼有需要優化的地方,和潛在的bug,還希望各位讀者,積極在下方評論區提出。以便作者更好地完善這個功能模組。