{"id":917,"date":"2023-03-11T17:10:44","date_gmt":"2023-03-11T09:10:44","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=917"},"modified":"2023-04-29T15:49:04","modified_gmt":"2023-04-29T07:49:04","slug":"spring-boot-reads-files-under-classpath","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-boot-reads-files-under-classpath\/","title":{"rendered":"Spring Boot\u8bfb\u53d6classpath\u4e0b\u6587\u4ef6"},"content":{"rendered":"<p>\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5fc5\u4e0d\u53ef\u5c11\u7684\u9700\u8981\u8bfb\u53d6\u6587\u4ef6\uff0c\u5bf9\u4e8e\u6253\u5305\u65b9\u5f0f\u7684\u4e0d\u540c\uff0c\u8fd8\u4f1a\u5b58\u5728\u4e00\u4e9b\u5751\uff0c\u6bd4\u5982\u4ee5jar\u5305\u65b9\u5f0f\u90e8\u7f72\u65f6\uff0c\u6587\u4ef6\u90fd\u5b58\u5728\u4e8ejar\u5305\u4e2d\uff0c\u67d0\u4e9b\u8bfb\u53d6\u65b9\u5f0f\u5728\u5f00\u53d1\u5de5\u7a0b\u4e2d\u90fd\u53ef\u884c\uff0c\u4f46\u662f\u6253\u5305\u540e\uff0c\u7531\u4e8e\u6587\u4ef6\u88ab\u4fdd\u5b58\u5728jar\u4e2d\uff0c\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u5931\u8d25\u3002<\/p>\n<p>\u8fd9\u65f6\u5c31\u9700\u8981\u901a\u8fc7\u7c7b\u52a0\u8f7d\u5668\u8bfb\u53d6\u6587\u4ef6\uff0c\u7c7b\u52a0\u8f7d\u5668\u53ef\u4ee5\u8bfb\u53d6jar\u5305\u4e2d\u7684class\u7c7b\u5f53\u7136\u4e5f\u53ef\u4ee5\u8bfb\u53d6jar\u5305\u4e2d\u7684\u6587\u4ef6\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/\/ \u65b9\u6cd51\uff1a\u83b7\u53d6\u6587\u4ef6\u6216\u6d41\nthis.getClass().getResource(&quot;\/&quot;)+fileName;\nthis.getClass().getResourceAsStream(failName);\n\/\/ \u65b9\u6cd52\uff1a\u83b7\u53d6\u6587\u4ef6\nFile file = org.springframework.util.ResourceUtils.getFile(&quot;classpath:test.txt&quot;);\n\/\/ \u65b9\u6cd53\uff1a\u83b7\u53d6\u6587\u4ef6\u6216\u6d41\nClassPathResource classPathResource = new ClassPathResource(&quot;test.txt&quot;);\nclassPathResource .getFile();\nclassPathResource .getInputStream();\n\n\/\/ \u4ee5\u4e0b\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6jar\u5305\u4e0b\u6587\u4ef6\n\u5047\u8bberesources\u76ee\u5f55\u4e0b\u6709\u4e00\u4e2atest.txt\u6587\u4ef6\uff0c\u9996\u5148\u83b7\u5f97\u5f53\u524d\u7684\u7c7b\u52a0\u8f7d\u5668\uff0c\u901a\u8fc7\u7c7b\u52a0\u8f7d\u5668\u8bfb\u53d6\u6587\u4ef6\n\n\/\/ \u65b9\u6cd51\nInputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream(&quot;test.txt&quot;);\n\/\/ \u65b9\u6cd52\nInputStream io = getClass().getClassLoader().getResourceAsStream(&quot;test.txt&quot;);<\/code><\/pre>\n<p>\u6ce8\u610f\uff1aSpring\u5de5\u5177\u7c7b\u4f1a\u5bf9classpath\u8def\u5f84\u505a\u5904\u7406\uff0c\u7c7b\u52a0\u8f7d\u5668\u4e0d\u4f1a\u5bf9classpath\u505a\u5904\u7406\uff0c\u56e0\u6b64\u4f7f\u7528\u7c7b\u52a0\u8f7d\u5668\u8bfb\u53d6\u6587\u4ef6\uff0c\u8def\u5f84\u4e2d\u4e0d\u8981\u6dfb\u52a0classpath<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5fc5\u4e0d\u53ef\u5c11\u7684\u9700\u8981\u8bfb\u53d6\u6587\u4ef6\uff0c\u5bf9\u4e8e\u6253\u5305\u65b9\u5f0f\u7684\u4e0d\u540c\uff0c\u8fd8\u4f1a\u5b58\u5728\u4e00\u4e9b\u5751\uff0c\u6bd4\u5982\u4ee5jar\u5305\u65b9\u5f0f\u90e8\u7f72\u65f6\uff0c\u6587\u4ef6\u90fd\u5b58\u5728\u4e8e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-917","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=917"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/917\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}