实在找不出来是哪的错了..
2020-05-04 18:56:13.572 [main] DEBUG com.oa.dao.ShopDao.insertShop - ==> Preparing: INSERT INTO tb_shop(owner_id, area_id, shop_category_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice) VALUES (?,?,?,?, ?,?,?,?,?, ?,?, ?,?)
2020-05-04 18:56:13.681 [main] DEBUG com.oa.dao.ShopDao.insertShop - ==> Parameters: 1(Long), 2(Integer), 1(Long), 测试的店铺3(String), test3(String), test3(String), test3(String), null, null, 2020-05-04 18:56:13.473(Timestamp), 2020-05-04 18:56:13.473(Timestamp), 0(Integer), 审核中(String)
2020-05-04 18:56:13.687 [main] DEBUG com.oa.dao.ShopDao.insertShop - <== Updates: 1
2020-05-04 18:56:13.751 [main] DEBUG com.oa.util.ImageUtil - upload\item\shop\54\2020050418561368848.jpg
2020-05-04 18:56:13.752 [main] DEBUG com.oa.util.ImageUtil - current path is :D:\O2oImages1\upload\item\shop\54\2020050418561368848.jpg
2020-05-04 18:56:13.811 [main] ERROR com.oa.util.ImageUtil - javax.imageio.IIOException: Can't read input file!
java.lang.RuntimeException: addShop error : 添加图片失败:创建缩略图失败:javax.imageio.IIOException: Can't read input file!
at com.oa.service.impl.ShopServiceImpl.addShop(ShopServiceImpl.java:55)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy22.addShop(Unknown Source)
at com.oa.service.ShopServiceTest.testAddShop(ShopServiceTest.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
==========================================
ImageUtil:
package com.oa.util;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class ImageUtil {
//获取classPath的绝对值路径,通过线程逆推
private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
//设置时间格式,为了getRandomFileName();
private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private static final Random r = new Random();
private static Logger logger = LoggerFactory.getLogger(ImageUtil.class);
public static File transferCommonsMultipartFileToFile(CommonsMultipartFile cFile) throws IOException {
File newFile = new File(cFile.getOriginalFilename());
cFile.transferTo(newFile);
return newFile;
}
public static String generateThumbnail(InputStream thumbnailInputStream, String fileName ,String targetAddr) throws IOException {
//定义随机图片名
String realFileName = getRandomFileName();
//拿出扩展名
String extension = getFileExtension(fileName);
//如果没有目录,需创建目录
makeDirPath(targetAddr);
String relativeAddr = targetAddr + realFileName + extension;
logger.debug(relativeAddr);
File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
logger.debug("current path is :" + PathUtil.getImgBasePath() + relativeAddr);
// try{
// Thumbnails.of(thumbnailInputStream).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());
// e.printStackTrace();
// }
// 调用Thumbnails生成带有水印的图片
try {
Thumbnails.of(thumbnailInputStream).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 targetAddr
*/
private static void makeDirPath(String targetAddr) {
String realFileParentPath = PathUtil.getImgBasePath() + targetAddr;
File dirPath = new File(realFileParentPath);
if(!dirPath.exists()){
//创建多级目录
dirPath.mkdirs();
}
}
private static String getFileExtension(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}
/**
* 生成随机文件名,当前年月日小时分钟秒钟数+五位随机数
* @return
*/
public static String getRandomFileName() {
//获取随机的五位数
int rannum = r.nextInt(89999) + 10000;//随机数从10000-99999
String nowTimeStr = sDateFormat.format(new Date());
return nowTimeStr + rannum;
}
public static void main(String[] args) throws IOException {
Thumbnails.of(new File("D://O2oImages1/f22.jpg"))
.size(200,200).watermark(Positions.BOTTOM_RIGHT , ImageIO.read(new File(basePath + "/watermark.jpg")),
0.25f).outputQuality(0.8f)
.toFile("D://O2oImages1/f22Pro.jpg");
}
}
PathUtil:
package com.oa.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")){
basePath = "D:/O2oImages1/";
}else {
basePath = "/home/xiaodu/image/";
}
basePath = basePath.replace("/" , separator);
return basePath;
}
public static String getShopImagePath(long shopId){
String imagePath = "upload/item/shop/" + shopId + "/";
return imagePath.replace("/" , separator);
}
}
==================================
watermark也有
测试传入图片的路径也没问题啊,我再此电脑搜那个路径都能搜出来
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星