{"id":1498,"date":"2023-03-25T13:49:06","date_gmt":"2023-03-25T05:49:06","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1498"},"modified":"2023-04-28T20:28:11","modified_gmt":"2023-04-28T12:28:11","slug":"spring-boot-read-files-in-resources-directory-after-packaged-as-jar","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/spring-boot-read-files-in-resources-directory-after-packaged-as-jar\/","title":{"rendered":"Spring Boot\u6253\u6210jar\u5305\u540e\uff0c\u8bfb\u53d6resources\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6"},"content":{"rendered":"<h2>\u539f\u6765\u5b9e\u73b0<\/h2>\n<pre><code class=\"language-java\">File file = ResourceUtils.getFile(&quot;classpath:appblog\/test.json&quot;);\nInputStream in = new FileInputStream(file);\nInputStreamReader reader = new InputStreamReader(new FileInputStream(file));\nBufferedReader br = new BufferedReader(reader)<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">URL keyUrl = getClass().getClassLoader().getResource(&quot;key.pem&quot;);\nString key = keyUrl != null ? keyUrl.getFile() : &quot;&quot;;\nlog.info(&quot;key: {}&quot;, key);<\/code><\/pre>\n<p>\u672c\u5730\u5f00\u53d1\u8fd0\u884c\u6b63\u5e38\uff1a<\/p>\n<pre><code>key: ${project_dir}\/target\/classes\/key.pem<\/code><\/pre>\n<p>\u6253\u6210jar\u5305\u540e\u8fd0\u884c\u5f02\u5e38\uff1a<\/p>\n<pre><code>com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:${jar_path}.jar!\/BOOT-INF\/classes!\/key.pem (No such file or directory)&quot;<\/code><\/pre>\n<p>\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u6587\u4ef6\u662f\u771f\u5b9e\u5b58\u5728\u4e8e\u78c1\u76d8\u7684\u67d0\u4e2a\u76ee\u5f55\u3002\u6b64\u65f6\u901a\u8fc7\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\uff0c\u662f\u53ef\u4ee5\u6b63\u5e38\u8bfb\u53d6\u7684\uff0c\u56e0\u4e3a\u6587\u4ef6\u786e\u5b9e\u5b58\u5728\u3002<\/p>\n<p>\u800c\u6253\u5305\u6210jar\u4ee5\u540e\uff0c\u5b9e\u9645\u4e0a\u6587\u4ef6\u662f\u5b58\u5728\u4e8ejar\u91cc\u9762\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u5728\u78c1\u76d8\u662f\u6ca1\u6709\u771f\u5b9e\u8def\u5f84\u7684\uff08<code>\\BOOT-INF\\classes!appblog\/test.json<\/code>\uff09\u3002\u6240\u4ee5\u901a\u8fc7<code>ResourceUtils.getFile<\/code>\u6216\u8005<code>this.getClass().getResource(&quot;&quot;)<\/code>\u65b9\u6cd5\u65e0\u6cd5\u6b63\u786e\u83b7\u53d6\u6587\u4ef6\u3002<\/p>\n<h2>\u73b0\u5728\u5b9e\u73b0<\/h2>\n<h3>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<p>\u91c7\u7528\u6d41\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\uff0c\u540c\u65f6\u8bfb\u53d6\u6d41\u65f6\u8bbe\u7f6e\u7f16\u7801UTF-8\uff0c\u4f7f\u7528<code>InputStream inputStream = this.getClass().getResourceAsStream(&quot;&quot;)<\/code>\u4f1a\u6307\u5b9a\u8981\u52a0\u8f7d\u7684\u8d44\u6e90\u8def\u5f84\u4e0e\u5f53\u524d\u7c7b\u6240\u5728\u5305\u7684\u8def\u5f84\u4e00\u81f4\uff0c\u56e0\u6b64\u80fd\u6b63\u5e38\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-java\">StringBuffer stringBuffer = new StringBuffer();\ntry {\n    InputStream stream = getClass().getClassLoader().getResourceAsStream(&quot;appblog\/test.json&quot;);\n    BufferedReader br = new BufferedReader(new InputStreamReader(stream, &quot;UTF-8&quot;));\n    String line;\n    while ((line = br.readLine()) != null) {\n        stringBuffer.append(line);\n    }\n} catch (Exception e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n<h3>\u83b7\u53d6\u6587\u4ef6\u8def\u5f84<\/h3>\n<pre><code class=\"language-java\">@Slf4j\n@Component\npublic class ResourceFileHelper {\n\n    public File getFile(String sourceFileName, File targetFile) {\n        if (targetFile.exists() &amp;&amp; targetFile.length() &gt; 0) return targetFile;\n        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(sourceFileName)) {\n            if (inputStream == null) {\n                throw new RuntimeException(&quot;Resource file not found&quot;);\n            }\n\n            FileUtils.copyInputStreamToFile(inputStream, targetFile);\n            return targetFile;\n\n        } catch (IOException e) {\n            log.error(&quot;ResourceFileHelper.getFile exception: &quot;, e);\n        }\n        return null;\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">String tmpDirPath = System.getProperty(&quot;java.io.tmpdir&quot;);\nFile tmpDir = new File(tmpDirPath + File.separator + &quot;kbank&quot;);\nif (!tmpDir.exists()) {\n    boolean mkdirs = tmpDir.mkdirs();\n    if (!mkdirs) new RuntimeException(&quot;no disk permission&quot;);\n}\n\nFile keyFile = new File(tmpDir, &quot;key.pem&quot;);\nkeyFile = resourceFileHelper.getFile(&quot;key.pem&quot;, keyFile);\n\nString key = keyFile != null ? keyFile.getAbsolutePath() : &quot;&quot;;\nlog.info(&quot;key: {}&quot;, key);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6765\u5b9e\u73b0 File file = ResourceUtils.getFile(&quot;classpath: [&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-1498","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\/1498","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=1498"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1498\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}