javax.imageio.IIOException: Can't read input file!完美解决
今天遇到一个问题,上图
这段代码主要是给图片添加水印
后来百度发现可能是图片的路径出了问题,因为是动态获取的图片地址,然后我把地址打印出来了
之后通过终端查看,果然没有
之后我在classes目录找到了文件
这时焕然大悟
因为我的图片文件一开始是放在main文件夹的resources文件夹中的,在文件编译时,会被放在classes文件夹中。。
然而我的测试代码是运行在test文件夹的,test文件夹中的resources文件夹中的内容会被放在test-classes文件夹。
代码中动态获取路径时是基于当前运行的代码位置,所以获取的路径为/Users/huang/IdeaProjects/o2o/target/test-classes/watermark.png,但图片被编译后放在了/Users/huang/IdeaProjects/o2o/target/classes/watermark.png中,所以报错找不到文件
解决方法:
我采用了比较粗暴的方式,将图片复制一份到test/resources中,编译时图片就会被放到/Users/huang/IdeaProjects/o2o/target/test-classes路径下,问题解决。
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">3</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/sougou_1323"> <img src="https://profile.csdnimg.cn/C/6/B/3_sougou_1323" class="avatar_pic" username="sougou_1323"> <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/sougou_1323" data-report-click="{"mod":"popu_379"}" target="_blank">nsn_huang</a></span> </div> <div class="text"><span>发布了43 篇原创文章</span> · <span>获赞 60</span> · <span>访问量 1万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=sougou_1323" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div> </article>