图片读取出现错误麻烦老师看看
"C:\Program Files\Java\jdk1.8.0_281\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Users\15070\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\211.7628.21\lib\idea_rt.jar=53450:C:\Users\15070\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\211.7628.21\bin -Dfile.encoding=UTF-8 -classpath "C:\Users\15070\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\211.7628.21\lib\idea_rt.jar;C:\Users\15070\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\211.7628.21\plugins\junit\lib\junit5-rt.jar;C:\Users\15070\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\211.7628.21\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_281\jre\lib\rt.jar;D:\Java\o2o\target\test-classes;D:\Java\o2o\target\classes;D:\repository\junit\junit\4.13\junit-4.13.jar;D:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\repository\org\springframework\spring-core\5.3.7\spring-core-5.3.7.jar;D:\repository\org\springframework\spring-jcl\5.3.7\spring-jcl-5.3.7.jar;D:\repository\org\springframework\spring-context\5.3.7\spring-context-5.3.7.jar;D:\repository\org\springframework\spring-aop\5.3.7\spring-aop-5.3.7.jar;D:\repository\org\springframework\spring-beans\5.3.7\spring-beans-5.3.7.jar;D:\repository\org\springframework\spring-expression\5.3.7\spring-expression-5.3.7.jar;D:\repository\org\springframework\spring-jdbc\5.3.7\spring-jdbc-5.3.7.jar;D:\repository\org\springframework\spring-tx\5.3.7\spring-tx-5.3.7.jar;D:\repository\org\springframework\spring-web\5.3.7\spring-web-5.3.7.jar;D:\repository\org\springframework\spring-webmvc\5.3.7\spring-webmvc-5.3.7.jar;D:\repository\org\springframework\spring-test\5.3.7\spring-test-5.3.7.jar;D:\repository\javax\servlet\javax.servlet-api\4.0.1\javax.servlet-api-4.0.1.jar;D:\repository\com\fasterxml\jackson\core\jackson-databind\2.12.3\jackson-databind-2.12.3.jar;D:\repository\com\fasterxml\jackson\core\jackson-annotations\2.12.3\jackson-annotations-2.12.3.jar;D:\repository\com\fasterxml\jackson\core\jackson-core\2.12.3\jackson-core-2.12.3.jar;D:\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;D:\repository\mysql\mysql-connector-java\8.0.24\mysql-connector-java-8.0.24.jar;D:\repository\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;D:\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;D:\repository\org\mybatis\mybatis\3.5.7\mybatis-3.5.7.jar;D:\repository\com\mchange\c3p0\0.9.5.5\c3p0-0.9.5.5.jar;D:\repository\com\mchange\mchange-commons-java\0.2.19\mchange-commons-java-0.2.19.jar;D:\repository\net\coobird\thumbnailator\0.4.14\thumbnailator-0.4.14.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.o2o.service.ShopServiceTest,testAddShop
2021-07-17 12:43:00:934 [main] INFO o.s.t.c.support.DefaultTestContextBootstrapper-Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2021-07-17 12:43:00:967 [main] INFO o.s.t.c.support.DefaultTestContextBootstrapper-Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@255b53dc, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1dd92fe2, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@6b53e23f, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@64d2d351, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1b68b9a4, org.springframework.test.context.transaction.TransactionalTestExecutionListener@4f9a3314, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@3b2c72c2, org.springframework.test.context.event.EventPublishingTestExecutionListener@491666ad]
2021-07-17 12:43:01:879 [MLog-Init-Reporter] INFO com.mchange.v2.log.MLog-MLog clients using slf4j logging.
2021-07-17 12:43:02:377 [main] INFO com.mchange.v2.c3p0.C3P0Registry-Initializing c3p0-0.9.5.5 [built 11-December-2019 22:18:33 -0800; debug? true; trace: 10]
2021-07-17 12:43:02:992 [main] INFO c.m.v2.c3p0.impl.AbstractPoolBackedDataSource-Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 2, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 10000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge72gai1dlqcos14ap4s9|66982506, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge72gai1dlqcos14ap4s9|66982506, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/o2o?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 30, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
2021-07-17 12:43:03:020 [main] WARN com.mchange.v2.resourcepool.BasicResourcePool-Bad pool size config, start 3 < min 10. Using 10 as start.
2021-07-17 12:43:03:988 [main] DEBUG com.imooc.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(?,?,?, ?,?,?,?,?,?, ?,?,?,?)
2021-07-17 12:43:04:069 [main] DEBUG com.imooc.o2o.dao.ShopDao.insertShop-==> Parameters: 1(Long), 2(Integer), 1(Long), 测试的店铺3(String), test3(String), test3(String), test3(String), null, null, 2021-07-17 12:43:03.938(Timestamp), 2021-07-17 12:43:03.938(Timestamp), 0(Integer), 审核中(String)
2021-07-17 12:43:04:072 [main] DEBUG com.imooc.o2o.dao.ShopDao.insertShop-<== Updates: 1
2021-07-17 12:43:04:104 [main] DEBUG com.imooc.o2o.util.ImageUtil-current relative addr is:/upload/item/shop/55/2021071712430480278.jpg
2021-07-17 12:43:04:104 [main] DEBUG com.imooc.o2o.util.ImageUtil-current complete addr is:D:/Java/o2o/image/upload/item/shop/55/2021071712430480278.jpg
2021-07-17 12:43:04:104 [main] ERROR com.imooc.o2o.util.ImageUtil-watermark addr is:D:/Java/o2o/target/test-classes/watermark.png
2021-07-17 12:43:04:142 [main] ERROR com.imooc.o2o.util.ImageUtil-javax.imageio.IIOException: Can't read input file!
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at com.imooc.o2o.util.ImageUtil.generateThumbnail(ImageUtil.java:60)
at com.imooc.o2o.service.impl.ShopServiceImpl.addShopImg(ShopServiceImpl.java:62)
at com.imooc.o2o.service.impl.ShopServiceImpl.addShop(ShopServiceImpl.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
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.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy27.addShop(Unknown Source)
at com.imooc.o2o.service.ShopServiceTest.testAddShop(ShopServiceTest.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
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:366)
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$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
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$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
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:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
2021-07-17 12:43:04:174 [main] DEBUG com.imooc.o2o.dao.ShopDao.updateShop-==> Preparing: UPDATE tb_shop SET shop_name=?, shop_desc=?, shop_addr=?, phone=?, shop_img=?, last_edit_time=?, enable_status=?, advice=?, area_id=?, shop_category_id=? WHERE shop_id=?;
2021-07-17 12:43:04:175 [main] DEBUG com.imooc.o2o.dao.ShopDao.updateShop-==> Parameters: 测试的店铺3(String), test3(String), test3(String), test3(String), /upload/item/shop/55/2021071712430480278.jpg(String), 2021-07-17 12:43:03.938(Timestamp), 0(Integer), 审核中(String), 2(Integer), 1(Long), 55(Long)
2021-07-17 12:43:04:177 [main] DEBUG com.imooc.o2o.dao.ShopDao.updateShop-<== Updates: 1
Process finished with exit code 0
package com.imooc.o2o.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.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
public class ImageUtil {
private static String basePath=ImageUtil.class.getResource("/").getPath().substring(1);
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 (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return newFile;
}
/**
* 处理缩略图并返回新生成图片相对路径
* @param thumbnail
* @param targetAddr
* @return
*/
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 relative addr is:" + relativeAddr);
File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
logger.debug("current complete addr is:"+PathUtil.getImgBasePath() + relativeAddr);
try{
logger.error("watermark addr is:"+basePath+"watermark.png");
Thumbnails.of(thumbnail).size(200,200)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath+"watermark.png")),1)
.outputQuality(0.9f).toFile(dest);
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return relativeAddr;
}
/**
* 生成随机文件名
*/
private static String getRandomFileName(){
UUID.randomUUID().toString().replace("-", "");
int rannum=r.nextInt(89999)+10000;
String nowTimeStr = sDateFormat.format(new Date());
return nowTimeStr + rannum;
}
/**
* 获取输入文件流的拓展名
* @param cFile
* @return
*/
private static String getFileExtension(File cFile){
String originalFileName = cFile.getName();
return originalFileName.substring(originalFileName.lastIndexOf("."));
}
/**
* 创建目标路径所涉及到的目录
* @param targetAddr
*/
private static void makeDirPath(String targetAddr){
String realFileParentPath=PathUtil.getImgBasePath()+targetAddr;
File dirPath = new File(realFileParentPath);
if(dirPath.exists()){
dirPath.mkdirs();
}
}
public static void main(String[] args) {
String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
try {
Thumbnails.of(new File("C:/Users/15070/Desktop/d533c4e216cd4102aef688aebafdb4cce0a9fd4495bbc1c2952691d49a6f2083.jpg"))
.size(1920,1920).watermark(Positions.BOTTOM_RIGHT,
ImageIO.read(new File(basePath+"/watermark.png")),1)
.outputQuality(0.8f).toFile(new File("C:/Users/15070/Desktop/new.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
之前测试用ImageUtil中的main方法生成缩略图没有问题
public static void main(String[] args) {
String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
try {
Thumbnails.of(new File("C:/Users/15070/Desktop/d533c4e216cd4102aef688aebafdb4cce0a9fd4495bbc1c2952691d49a6f2083.jpg"))
.size(1920,1920).watermark(Positions.BOTTOM_RIGHT,
ImageIO.read(new File(basePath+"/watermark.png")),1)
.outputQuality(0.8f).toFile(new File("C:/Users/15070/Desktop/new.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
19
收起
正在回答
1回答
同学你好,查看同学贴出的错误与老师视频中讲解的错误一致,建议同学根据视频中的演示修改下代码试试
祝学习愉快~
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星