在idea上能够正常读取到项目资源上的图片,到了tomcat8上就识别不了,打印发现路径上的空格处竟然变成了20%这样的乱码,现在给出一致性的解决办法,在idea和tomcat上写法一样。我的项目路径如下图所示:项目是运行在idea上的springboot2.X,
现在程序需要读取static/img/front.png文件,那么通用的写法为
import org.springframework.util.ResourceUtils; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.net.URLDecoder;
//以下为关键代码
File path = new File(ResourceUtils.getURL("classpath:").getPath()); String decoderURL= URLDecoder.decode(path.getAbsolutePath(),"utf-8"); File upload = new File(decoderURL,"static/img/front.png");