实在找不出来是哪的错了..

实在找不出来是哪的错了..

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);
   }


}

==================================

http://img1.sycdn.imooc.com//climg/5eaff66009b91f9103730423.jpg

watermark也有

测试传入图片的路径也没问题啊,我再此电脑搜那个路径都能搜出来

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
提问者 阿满___最帅 2020-05-04 20:33:16

这是我第二轮跟着老师学这个项目,以前没有过这个问题,我的图片路径也没有中文

我这次写用的数据库是和上次是一样的,存图片的文件夹以前是O2oImages,现在是O2oImages1,

上节课老师将加水印还加进去了,现在不知道哪出问题了

  • 提问者 阿满___最帅 #1
    把老师的代码下载下来也出错,就我第一次写的那个项目不出错,难搞啊
    2020-05-04 21:55:00
  • 提问者 阿满___最帅 #2
    好了,解决了~
    2020-05-05 10:18:12
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师