ShopOperationException: addShop error:addShopImg error:null

来源:4-5 店铺注册之Service层的实现

AshesHearts

2019-07-29

翻来覆去检查路径,并DEBUG,之前在写ImageUtil时的main函数时也成功输出了一张打印好水印的图片,但是这次test时就一直报错,图片路径都是用的绝对路径,DEBUG时也能读到目标图片的路径,比对代码也没有错,这次实在是不知道出啥问题了

ImageUtil

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*/
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;
}
/*处理缩略图,并返回目标图片的相对值路径*/
public static String generateThumbnail(File thumbnail, String targetAddr)
{
    String realFileName = getRandomFileName();
    String extension = getFileExtension(thumbnail);
    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);
    try {
        Thumbnails.of(thumbnail).size(300,600).watermark(Positions.BOTTOM_RIGHT,
                ImageIO.read(new File("D:/实训文件/pic/watermark.png")),0.25f).outputQuality(0.8f)
                .toFile(dest);
    }catch (IOException e)
    {
        logger.error(e.toString());
        e.printStackTrace();
    }
    return relativeAddr;
}



/*生成随机文件名,当前年月日小时分钟秒钟+五位随机数*/
private static String getRandomFileName()
{
    //获取随机的五位数
    int rannum = r.nextInt(89999)+10000;
    String nowTimeStr = sDateFormat.format(new Date());
    return nowTimeStr + rannum;
}

/*获取输入的文件扩展名*/
private static String getFileExtension(File cFile)
{
    String originalFileName = cFile.getName();
    return originalFileName.substring(originalFileName.lastIndexOf("."));
}

private static void makeDirPath(String targetAddr) {
    String realFileParentPath = PathUtil.getImgBasePath() + targetAddr;
    File dirPath = new File(realFileParentPath);
    if (!dirPath.exists())
    {
        dirPath.mkdirs();
    }
}

PathUtil

public class PathUtil {
private static String seperator = System.getProperty(“file.separator”);
public static String getImgBasePath()
{
String os = System.getProperty(“os.name”);
String basePath = “”;
if(os.toLowerCase().startsWith(“win”))
{
basePath = “D:/实训文件/pic/”;
}
else
{
basePath = “/Users/AboutO2O/image/”;
}
basePath = basePath.replace("/", seperator);
return basePath;
}

public static String getShopImagePath(long shopId) {
    String imagePath = "/upload/images/item/shop/" + shopId + "/";
    return imagePath.replace("/", seperator);
}

}

控制台信息

2019-07-29 12:01:57.438 [main] DEBUG com.own.o2o.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 (?,?,?,?, ?,?,?,?,?,?, ?,?,?)
2019-07-29 12:01:57.501 [main] DEBUG com.own.o2o.dao.ShopDao.insertShop - > Parameters: 1(Long), 2(Long), 1(Long), 测试的店铺1(String), test1(String), test1(String), test1(String), null, null, 2019-07-29 12:01:57.398(Timestamp), 2019-07-29 12:01:57.398(Timestamp), 0(Integer), 审核中(String)
2019-07-29 12:01:57.504 [main] DEBUG com.own.o2o.dao.ShopDao.insertShop - < Updates: 1

com.own.o2o.exceptions.ShopOperationException: addShop error:addShopImg error:null

at com.own.o2o.service.impl.ShopServiceImpl.addShop(ShopServiceImpl.java:57)
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:566)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy27.addShop(Unknown Source)
at com.own.o2o.service.ShopServiceTest.testAddShop(ShopServiceTest.java:43)
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:566)
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.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
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:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

第一处57行那个错是CATCH捕获到并抛出异常
第二处43行就是异常的代码处, 相关test类里的 ShopExecution se = shopService.addShop(shop,ShopImg);

写回答

2回答

慕前端551891

2019-08-25

我也遇到这个问题了,同学你解决了吗

0
0

翔仔

2019-07-29

同学好,会尝试debug去调试问题难能可贵,你可以尝试在generateThumbnail()方法里面设置一个断点,在Catch的地方,看看Exception是啥,然后针对性解决下,估计还是路径问题

0
0

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程