创建缩略图失败
来源:9-3 首页前端的开发下
qq_鏖战_0
2019-03-15
这里是imageUtil.java
package com.shop.o2o.util;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.shop.o2o.dto.ImageHolder;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
public class ImageUtil {
private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private static final Random r = new Random();
private static Logger logger = LoggerFactory.getLogger(ImageUtil.class);
/**
* 将CommonsMultipartFile转换成File类
*
* @param cFile
* @return
*/
public static File transferCommonsMultipartFileToFile(CommonsMultipartFile cFile) {
File newFile = new File(cFile.getOriginalFilename());
try {
cFile.transferTo(newFile);
} catch (IllegalStateException e) {
logger.error(e.toString());
e.printStackTrace();
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return newFile;
}
/**
* 处理缩略图,并返回新生成图片的相对值路径
*
* @param thumbnail
* @param targetAddr
* @return
*/
public static String generateThumbnail(ImageHolder thumbnail, String targetAddr) {
// 获取不重复的随机名
String realFileName = getRandomFileName();
// 获取文件的扩展名如png,jpg等
String extension = getFileExtension(thumbnail.getImageName());
// 如果目标路径不存在,则自动创建
makeDirPath(targetAddr);
// 获取文件存储的相对路径(带文件名)
String relativeAddr = targetAddr + realFileName + extension;
logger.debug("current relativeAddr is :" + relativeAddr);
// 获取文件要保存到的目标路径
File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
logger.debug("current complete addr is :" + PathUtil.getImgBasePath() + relativeAddr);
logger.debug("basePath is :" + basePath);
// 调用Thumbnails生成带有水印的图片
try {
Thumbnails.of(thumbnail.getImage()).size(200, 200)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/watermark.jpg")), 0.25f)
.outputQuality(0.8f).toFile(dest);
} catch (IOException e) {
logger.error(e.toString());
throw new RuntimeException("创建缩略图失败:" + e.toString());
}
// 返回图片相对路径地址
return relativeAddr;
}
/**
* 处理详情图,并返回新生成图片的相对值路径
*
* @param thumbnail
* @param targetAddr
* @return
*/
public static String generateNormalImg(ImageHolder thumbnail, String targetAddr) {
// 获取不重复的随机名
String realFileName = getRandomFileName();
// 获取文件的扩展名如png,jpg等
String extension = getFileExtension(thumbnail.getImageName());
// 如果目标路径不存在,则自动创建
makeDirPath(targetAddr);
// 获取文件存储的相对路径(带文件名)
String relativeAddr = targetAddr + realFileName + extension;
logger.debug("current relativeAddr is :" + relativeAddr);
// 获取文件要保存到的目标路径
File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
logger.debug("current complete addr is :" + PathUtil.getImgBasePath() + relativeAddr);
// 调用Thumbnails生成带有水印的图片
try {
Thumbnails.of(thumbnail.getImage()).size(337, 640)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/watermark.jpg")), 0.25f)
.outputQuality(0.9f).toFile(dest);
} catch (IOException e) {
logger.error(e.toString());
throw new RuntimeException("创建缩图片失败:" + e.toString());
}
// 返回图片相对路径地址
return relativeAddr;
}
/**
* 创建目标路径所涉及到的目录,即/home/work/xiangze/xxx.jpg, 那么 home work xiangze 这三个文件夹都得自动创建
*
* @param targetAddr
*/
private static void makeDirPath(String targetAddr) {
String realFileParentPath = PathUtil.getImgBasePath() + targetAddr;
File dirPath = new File(realFileParentPath);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
}
/**
* 获取输入文件流的扩展名
*
* @param thumbnail
* @return
*/
private static String getFileExtension(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}
/**
* 生成随机文件名,当前年月日小时分钟秒钟+五位随机数
*
* @return
*/
public static String getRandomFileName() {
// 获取随机的五位数
int rannum = r.nextInt(89999) + 10000;
String nowTimeStr = sDateFormat.format(new Date());
return nowTimeStr + rannum;
}
public static void main(String[] args) throws IOException {
System.out.println(basePath);
String basePath1 = URLDecoder.decode(basePath, "utf-8");
System.out.println(basePath1);
// Thumbnails.of(new File(“F:\image\lbj.jpg”)).size(200, 200)
// .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + “/watermark.jpg”)), 0.25f)
// .outputQuality(0.8f).toFile(“F:\image\lbjnew.jpg”);
}
/**
* storePath是文件的路径还是目录的路径, 如果storePath是文件路径则删除该文件, 如果storePath是目录路径则删除该目录下的所有文件
*
* @param storePath
*/
public static void deleteFileOrPath(String storePath) {
File fileOrPath = new File(PathUtil.getImgBasePath() + storePath);
if (fileOrPath.exists()) {
if (fileOrPath.isDirectory()) {
File files[] = fileOrPath.listFiles();
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
}
fileOrPath.delete();
}
}
}
这里是pathUtil.java
package com.shop.o2o.util;
public class PathUtil {
// 获取分隔符
private static String separator = System.getProperty("file.separator");
/**
* 图片保存的根目录
*
* @return
*/
public static String getImgBasePath() {
// 获取操作系统
String os = System.getProperty("os.name");
String basePath = "";
if (os.toLowerCase().startsWith("win")) {
// /download/image/
basePath = "F:\\image";
} else {
basePath = "/home/user/image";
}
// 将斜杆替换成分隔符,这样就能保证路径是有效的
basePath = basePath.replace("/", separator);
return basePath;
}
/**
* 图片保存的子目录:</br>
* upload/item/shop/" + shopId
*
* @param shopId
* @return
*/
public static String getShopImagePath(long shopId) {
// /upload/item/shop/"+shopId+"/
String imagePath = "upload/item/shop/" + shopId + "/";
return imagePath.replace("/", separator);
}
}
![图片描述](http://img.mukewang.com/szimg/5c8b310c0001f32a16880830.jpg)
![图片描述](http://img.mukewang.com/szimg/5c8b311a0001714017730984.jpg)
![图片描述](http://img.mukewang.com/szimg/5c8b31280001fd1d16440567.jpg)
2回答
-
同学好,主要看看报什么错。。一般是水印路径或者图片路径的问题,水印basePath建议直接替换成绝对路径,也就是类似D:/xxx/xxx/watermark.jpg
00 -
qq_鏖战_0
提问者
2019-03-15
这是报错的图片。
012019-03-17
相似问题