辽宁网站建设企业wordpress安全插件
2026/4/6 11:00:18 网站建设 项目流程
辽宁网站建设企业,wordpress安全插件,四川网站推广公司,广州商务网站建设电话哈喽#xff0c;大家好#xff0c;今天给大家带来SpringBoot获取resources目录下文件的常用方法#xff0c;示例中的方法是读取resources目录下的txt和xlsx文件#xff0c;并将xlsx导出到excel的简单写法。完整代码放在最后。通过this.getClass()方法获取method1 - method4…哈喽大家好今天给大家带来SpringBoot获取resources目录下文件的常用方法示例中的方法是读取resources目录下的txt和xlsx文件并将xlsx导出到excel的简单写法。完整代码放在最后。通过this.getClass()方法获取method1 - method4都是通过这个方法获取文件的写法这四种写法在idea中都可以正常运行jar包执行后method1和method2报错提示找不到文件method3和method4可以正常运行通过ClassPathResource获取method5是通过这种方法实现idea中可以正常运行打包后的jar中提示找不到文件通过hutool工具类ResourceUtil获取method6是通过这种方法实现和method情况一样同样是idea中可以正常运行导出的jar中提示找不到文件总结不想折腾的同学可以直接用method3和method4的方法来使用也可以将模板和资源文件外置通过绝对路径获取对应文件。有好的方法也欢迎大家一起交流沟通~代码import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.resource.ClassPathResource; import cn.hutool.core.io.resource.ResourceUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.enums.WriteDirectionEnum; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.List; RestController RequestMapping(/temp) public class TemplateController { /** * this.getClass()方法获取 * param response * throws IOException */ RequestMapping(/method1) public void method1(HttpServletResponse response) throws IOException { System.out.println(----------method1 start); String filename template.xlsx; String bashPatch this.getClass().getClassLoader().getResource().getPath(); System.out.println(bashPatch); String textFile template.txt; String textPath this.getClass().getClassLoader().getResource().getPath(); ListString dataList FileUtil.readUtf8Lines(textPath /template/ textFile); for (String data : dataList) { System.out.println(data); } try (ExcelWriter excelWriter EasyExcel.write(response.getOutputStream()) .autoCloseStream(false) // 不要自动关闭交给 Servlet 自己处理 // .withTemplate(resource.getFile().getAbsolutePath()) .withTemplate(bashPatch /template/ filename) .build() ) { WriteSheet writeSheet EasyExcel.writerSheet(0).build(); FillConfig userFillConfig FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig titleFillConfig FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); excelWriter.finish(); } try { response.addHeader(Content-Disposition, attachment;filename URLEncoder.encode(filename, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentType(application/vnd.ms-excel;charsetUTF-8); } RequestMapping(/method2) public void method2(HttpServletResponse response) throws IOException { System.out.println(----------method2 start); String filename template.xlsx; String bashPatch this.getClass().getClassLoader().getResource(template).getPath(); System.out.println(bashPatch); String textFile template.txt; String textPath this.getClass().getClassLoader().getResource(template).getPath(); ListString dataList FileUtil.readUtf8Lines(textPath / textFile); for (String data : dataList) { System.out.println(data); } try (ExcelWriter excelWriter EasyExcel.write(response.getOutputStream()) .autoCloseStream(false) // 不要自动关闭交给 Servlet 自己处理 // .withTemplate(resource.getFile().getAbsolutePath()) .withTemplate(bashPatch / filename) .build() ) { WriteSheet writeSheet EasyExcel.writerSheet(0).build(); FillConfig userFillConfig FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig titleFillConfig FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); excelWriter.finish(); } try { response.addHeader(Content-Disposition, attachment;filename URLEncoder.encode(filename, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentType(application/vnd.ms-excel;charsetUTF-8); } RequestMapping(/method3) public void method3(HttpServletResponse response) throws IOException { System.out.println(----------method3 start); String filename template.xlsx; InputStream inputStream this.getClass().getClassLoader().getResourceAsStream(template / filename); // System.out.println(inputStream); String textFile template.txt; InputStream textStream this.getClass().getClassLoader().getResourceAsStream(template / textFile); BufferedReader reader new BufferedReader(new InputStreamReader(textStream)); String line; try { while ((line reader.readLine()) ! null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); // 异常处理 } try (ExcelWriter excelWriter EasyExcel.write(response.getOutputStream()) .autoCloseStream(false) // 不要自动关闭交给 Servlet 自己处理 // .withTemplate(resource.getFile().getAbsolutePath()) .withTemplate(inputStream) .build() ) { WriteSheet writeSheet EasyExcel.writerSheet(0).build(); FillConfig userFillConfig FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig titleFillConfig FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); excelWriter.finish(); } try { response.addHeader(Content-Disposition, attachment;filename URLEncoder.encode(filename, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentType(application/vnd.ms-excel;charsetUTF-8); } RequestMapping(/method4) public void method4(HttpServletResponse response) throws IOException { System.out.println(----------method4 start); String filename template.xlsx; InputStream inputStream this.getClass().getResourceAsStream(/template / filename); // System.out.println(inputStream); String textFile template.txt; InputStream textStream this.getClass().getResourceAsStream(/template / textFile); BufferedReader reader new BufferedReader(new InputStreamReader(textStream)); String line; try { while ((line reader.readLine()) ! null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); // 异常处理 } try (ExcelWriter excelWriter EasyExcel.write(response.getOutputStream()) .autoCloseStream(false) // 不要自动关闭交给 Servlet 自己处理 // .withTemplate(resource.getFile().getAbsolutePath()) .withTemplate(inputStream) .build() ) { WriteSheet writeSheet EasyExcel.writerSheet(0).build(); FillConfig userFillConfig FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig titleFillConfig FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); excelWriter.finish(); } try { response.addHeader(Content-Disposition, attachment;filename URLEncoder.encode(filename, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentType(application/vnd.ms-excel;charsetUTF-8); } /** * 通过ClassPathResource获取 * param response * throws IOException */ RequestMapping(/method5) public void method5(HttpServletResponse response) throws IOException { System.out.println(----------method5 start); String filename template.xlsx; ClassPathResource classPathResource new ClassPathResource(template / filename); String textFile template.txt; ClassPathResource textResource new ClassPathResource(template / textFile); ListString dataList FileUtil.readUtf8Lines(textResource.getAbsolutePath()); for (String data : dataList) { System.out.println(data); } try (ExcelWriter excelWriter EasyExcel.write(response.getOutputStream()) .autoCloseStream(false) // 不要自动关闭交给 Servlet 自己处理 .withTemplate(classPathResource.getAbsolutePath()) .build() ) { WriteSheet writeSheet EasyExcel.writerSheet(0).build(); FillConfig userFillConfig FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig titleFillConfig FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); excelWriter.finish(); } try { response.addHeader(Content-Disposition, attachment;filename URLEncoder.encode(filename, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentType(application/vnd.ms-excel;charsetUTF-8); } /** * 通过hutool工具类ResourceUtil获取 * param response * throws IOException */ RequestMapping(/method6) public void method6(HttpServletResponse response) throws IOException { System.out.println(----------method6 start); String filename template.xlsx; String filePath ResourceUtil.getResource(template / filename).getPath(); String textFile template.txt; String textPath ResourceUtil.getResource(template / textFile).getPath(); ListString dataList FileUtil.readUtf8Lines(textPath); for (String data : dataList) { System.out.println(data); } try (ExcelWriter excelWriter EasyExcel.write(response.getOutputStream()) .autoCloseStream(false) // 不要自动关闭交给 Servlet 自己处理 .withTemplate(filePath) .build() ) { WriteSheet writeSheet EasyExcel.writerSheet(0).build(); FillConfig userFillConfig FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig titleFillConfig FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); excelWriter.finish(); } try { response.addHeader(Content-Disposition, attachment;filename URLEncoder.encode(filename, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentType(application/vnd.ms-excel;charsetUTF-8); } }pom依赖编程语言Cm.jingdaocn.comc语言的魅力编程语言Cm.tianfuyoushi.comc语言的魅力编程语言Cm.lijidecoration.comc语言的魅力编程语言Cm.xingyuanad.comc语言的魅力编程语言Cm.ezeghr.comc语言的魅力编程语言Cm.zgyglp.comc语言的魅力编程语言Cm.lazipig.netc语言的魅力编程语言Cm.fzdzjzs.comc语言的魅力编程语言Cm.happystudio.cnc语言的魅力编程语言Cm.jtlspray.comc语言的魅力编程语言Cm.hnrjhnt.cnc语言的魅力编程语言Cm.wxrsjh.comc语言的魅力编程语言Cwww.yuehanjianzhu.cnc语言的魅力编程语言Cwww.blog.yuehanjianzhu.cnc语言的魅力编程语言Cwww.share.yuehanjianzhu.cnc语言的魅力编程语言Cread.share.yuehanjianzhu.cnc语言的魅力编程语言Cm.yuehanjianzhu.cnc语言的魅力编程语言Cwww.mingkejiaoyu.com.cnc语言的魅力编程语言Cwww.blog.mingkejiaoyu.com.cnc语言的魅力编程语言Cwww.share.mingkejiaoyu.com.cnc语言的魅力编程语言Cread.share.mingkejiaoyu.com.cnc语言的魅力编程语言Cm.mingkejiaoyu.com.cnc语言的魅力

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询