spring實現圖片上傳
阿新 • • 發佈:2019-01-27
pom依賴
<!--圖片處理相關類-->
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
路徑工具
//路徑工具類
public class FileUtil {}
private static String seperator = System.getProperty("file.separator");
private static final SimpleDateFormat sDateFormate=new SimpleDateFormat("yyyyMMddHHmmss");
private static final Random r = new Random();
//設定圖片的路徑 對不同的系統又不同路徑
public static String getImgBasePath(){
String os=System.getProperty("os.name" );
String basePath;
if((os.toLowerCase().startsWith("win"))){
basePath="D:/workspace/o2oImage";
}else{
basePath="/home/o2oImage/";
}
basePath=basePath.replace("/", seperator);
return basePath;
}
//設定所傳入圖片的檔名
public static String getShopImagePath(long shopId) {
StringBuilder shopImagePathBuilder = new StringBuilder();
shopImagePathBuilder.append("upload/images/item/shop/");
shopImagePathBuilder.append(shopId);
shopImagePathBuilder.append("/");
String shopImagePath = shopImagePathBuilder.toString().replace("/",seperator);
return shopImagePath;
}
//重新設定檔名(時間+五位隨機數) 防重名
public static String getRandomFileName(){
int ramnum=r.nextInt(9999)+10000;
String nowTimeStr=sDateFormate.format(new Date());
return nowTimeStr+ramnum;
}
圖片上傳工具
public class ImageUtil {}
程式碼如下
導包
package com.imooc.o2o.util;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
通過反射獲得水印圖片的路徑
public static String basePath=Thread.currentThread()
.getContextClassLoader()
.getResource("")
.getPath();
//建立略縮圖 且打水印
public static String generateThumbnail(File thumbnail ,String targetAddr ){
String realFileName=FileUtil.getRandomFileName();
String extension=getFileExtension(thumbnail);
makeDirPath(targetAddr);
String relativeAddr=targetAddr+realFileName+extension;
File dest=new File(FileUtil.getImgBasePath()+relativeAddr);
try {
//給圖片打水印
Thumbnails.of(thumbnail).size(200,200).
watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/watermark.jpg")), 0.5f)
.outputQuality(0.8f).toFile(dest);
} catch (IOException e) {
throw new RuntimeException("建立略縮圖失敗"+e.toString());
}
return relativeAddr;
}
//建立普通大小的圖片
public static String generateNormalImg(File thumbnail, String targetAddr) {
String realFileName = FileUtil.getRandomFileName();
String extension = getFileExtension(thumbnail);
makeDirPath(targetAddr);
String relativeAddr = targetAddr + realFileName + extension;
File dest = new File(FileUtil.getImgBasePath() + relativeAddr);
try {
Thumbnails.of(thumbnail).size(337, 640).outputQuality(0.5f).toFile(dest);
} catch (IOException e) {
throw new RuntimeException("建立縮圖失敗:" + e.toString());
}
return relativeAddr;
}
//就建立相關的路徑
private static void makeDirPath(String targetAddr) {
String realFileParentPath = FileUtil.getImgBasePath() + targetAddr;
File dirPath = new File(realFileParentPath);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
}
//獲得上傳檔案的拓展名
private static String getFileExtension(File file) {
String originalFileName =file.getName();
return originalFileName.substring(originalFileName.lastIndexOf("."));
}