且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Tomcat中部署的未爆炸战争文件中读取资源文件?

更新时间:2023-11-17 22:08:04

如果使用Maven进行构建,则需要确保将图像实际放置到存档中.

If you are building using Maven, you'll want to make sure the image actually gets placed into the archive.

将资源放入您的src/main/resources目录.然后使用以下命令访问它们:

Put resources in your src/main/resources directory. Then access them with:

this.getClass().getResource("/logo.png"); 

或:

Thread.currentThread().getContextClassLoader().getResource("logo.png"); 

(以上注释中的代码示例,但请在答案中显示得更明显)

(Code samples from comment above, but put in the answer to be more visible)