<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[springboot打包jar文件运行后无法读取jar目录中的Excel文件]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Wed, 30 Nov 2022 14:28:11 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	原因：SpringBoot内嵌web容器，其特点是只有一个jar文件，在容器启动后不会解压缩。<br/><br/>解决方式：<br/><br/>1. 必须使用相对路径读取文件；<br/><br/>假设你的模板文件放在了 resources —&gt; templates —&gt; test.xlsx<br/><br/>2. 只能使用流去读取，不能用file;<br/><br/>&nbsp;&nbsp;// jar里面文件读取方式：<br/>&nbsp;&nbsp;ClassPathResource classPathResource = new ClassPathResource(&quot;templates/test.xlsx&quot;);<br/>&nbsp;&nbsp;// 获取文件流<br/>&nbsp;&nbsp;classPathResource .getInputStream();<br/><br/>如果要将流存储到数组中如下：<br/><br/>&nbsp;&nbsp;/**<br/>&nbsp;&nbsp;&nbsp;&nbsp; * 输出流转字节数组<br/>&nbsp;&nbsp;&nbsp;&nbsp; * @param input 输出流<br/>&nbsp;&nbsp;&nbsp;&nbsp; * @return 字节数组<br/>&nbsp;&nbsp;&nbsp;&nbsp; */<br/>&nbsp;&nbsp;&nbsp;&nbsp;public static byte[] toByteArray(InputStream input) throws IOException &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByteArrayOutputStream output = new ByteArrayOutputStream();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte[] buffer = new byte[1024 * 4];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int size = 0;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (-1 != (size = input.read(buffer))) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.write(buffer, 0, size);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return output.toByteArray();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] springboot打包jar文件运行后无法读取jar目录中的Excel文件]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>