心有半亩花田 發表於 2025-6-18 11:29:00

SpringBoot读取Resources下的文件

<h1 id="springboot读取resources下的文件">SpringBoot读取Resources下的文件</h1>
<h1 id="背景">背景</h1>
<p>在开发时候遇到需要通过 Resources 目录下某个 excel 文件作为模板生成文件。但遇到 POI 读取文件的时候发生了 <code>No valid entries or contents found, this is not a valid 00xML (office open XML) fil.</code>​ 的错误,该错误表示读取的文件格式是错误的。</p>
<p>通过对同一份文件从 Resources 下读取和外部读取发现,两者的文件大小不一致,且 Resources 下的文件已经损坏。</p>
<p>造成此问题可能的情况是(当时忘记查看 pom.xml,以下纯属猜测)</p>
<pre><code class="language-xml">        &lt;build&gt;
      &lt;resources&gt;
                        &lt;!-- 可能没设置该项 --&gt;
            &lt;resource&gt;
                &lt;directory&gt;src/main/resources&lt;/directory&gt;
                                &lt;!-- 此项设置成如下 --&gt;
                &lt;filtering&gt;true&lt;/filtering&gt;
            &lt;/resource&gt;
      &lt;/resources&gt;
    &lt;/build&gt;
</code></pre>
<h1 id="目标">目标</h1>
<ol>
<li>可以在运行时读取到 Resources 下的文件</li>
<li>消除 POI 读取的错误</li>
</ol>
<h1 id="具体操作">具体操作</h1>
<h2 id="修改pomxml文件">修改pom.xml文件</h2>
<pre><code class="language-xml">&lt;!-- 方案一 --&gt;
&lt;build&gt;
   &lt;resources&gt;
      &lt;resource&gt;
          &lt;directory&gt;src/main/resources&lt;/directory&gt;
          &lt;filtering&gt;false&lt;/filtering&gt;
      &lt;/resource&gt;
   &lt;/resources&gt;
&lt;/build&gt;

&lt;!-- 方案二 --&gt;
&lt;build&gt;
   &lt;plugins&gt;
        &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                &lt;nonFilteredFileExtensions&gt;
                        &lt;!--不加这一行,xlsx文件会被过滤,然后在maven build的时候,去target下看对应的xlsx就是损坏的--&gt;
                        &lt;nonFilteredFileExtension&gt;xlsx&lt;/nonFilteredFileExtension&gt;
                &lt;/nonFilteredFileExtensions&gt;
                &lt;/configuration&gt;
        &lt;/plugin&gt;
   &lt;/plugins&gt;
   &lt;resources&gt;
      &lt;resource&gt;
         &lt;directory&gt;src/main/resources&lt;/directory&gt;
         &lt;filtering&gt;true&lt;/filtering&gt;
      &lt;/resource&gt;
   &lt;/resources&gt;
&lt;/build&gt;
</code></pre>
<h2 id="读取resources下的文件">读取Resources下的文件</h2>
<pre><code class="language-java">// 方案一
File file = new File("D:\\1.xlsx");
InputStream resourceAsStream = ScheduleTask.class.getClassLoader().getResourceAsStream("1.xlsx");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(resourceAsStream.readAllBytes());
fileOutputStream.flush();
fileOutputStream.close();
resourceAsStream.close();

// 方案二
File file = ResourceUtils.getFile("classpath:1.xlsx");
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream("D:\\1.xlsx");
fileOutputStream.write(fileInputStream.readAllBytes());
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();
</code></pre>
<h1 id="总结">总结</h1>
<ol>
<li>POI读取错误主要是由于在build的时候文件没有被正确的写入进去,只需要调整pom.xml文件的调整即可使得POI正确的读取到文件。</li>
<li>实践中采用pom.xml修改中的第一种方案发现其与nacos貌似存在冲突,建议采用方案二。</li>
</ol>


</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:一块白板,转载请注明原文链接:https://www.cnblogs.com/ykbb/p/18934475/springboot-reads-files-under-resources-z1pwyth</p><br><br>
来源:https://www.cnblogs.com/ykbb/p/18934475/springboot-reads-files-under-resources-z1pwyth
頁: [1]
查看完整版本: SpringBoot读取Resources下的文件