springboot上传文件到resources_[转]SpringBoot读取Resource下文件的几种方式

本文展示了如何在SpringBoot中读取Resource下的文件,包括使用ClassPathResource读取静态资源文件,并通过示例代码详细解释了读取过程。此外,虽然未涉及上传文件到resources,但文章为理解SpringBoot文件操作提供了基础。
摘要由CSDN通过智能技术生成

1 packagetb.view.sbmsm.easyuidemo.controller;2

3 importorg.apache.commons.lang3.StringUtils;4 importorg.springframework.core.io.ClassPathResource;5 importorg.springframework.stereotype.Controller;6 importorg.springframework.web.bind.annotation.RequestMapping;7 importorg.springframework.web.bind.annotation.ResponseBody;8 importtb.helper.IOHelper;9

10 importjavax.servlet.http.HttpServletRequest;11 importjavax.servlet.http.HttpServletResponse;12 importjava.io.IOException;13 importjava.io.InputStream;14 importjava.util.regex.Matcher;15 importjava.util.regex.Pattern;16

17 /**

18 *

19 *20 * 
21 *22 *@authorwangyunpeng23 * @date 2020/1/19 13:5524 */

25

26 @Controller27 @RequestMapping("/Resources")28 public classResourcesController {29 /**

30 *http://stackoverflow.com/questions/19721820/pattern-matching-to-get-only-class-names-from-css-file

31 */

32 private final Pattern _regex = Pattern.compile("\\.(.*)\\s?\\{", Pattern.MULTILINE);33 privateString _icons;34 /**

35 * Logger for this class36 */

37 private final org.slf4j.Logger _logger = org.slf4j.LoggerFactory.getLogger(this.getClass());38

39 @RequestMapping(value = "IconData", produces = "application/json;charset=UTF-8")40 @ResponseBody41 public String iconData(HttpServletRequest request, HttpServletResponse response) throwsIOException {42 if (this._icons == null) {43

44 //region 第一种方式:springboot项目获取resources路径(相对路径),只能开发用,部署路径不知道什么45 // //https://blog.csdn.net/qq_29669265/article/details/89678077?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task46 //String physicalUserDir = System.getProperty("user.dir");//项目的物理路径47 //File virtualPathFile = new File("src/main/resources/static/Content/jquery.easyUI/1.5.1/themes/icon.css");//相对路径,只能开发用,部署路径不知道什么48 //String physicalPath = virtualPathFile.getCanonicalPath();//物理路径49 //if (!IOHelper.isExistFilePath(physicalPath)) {50 //return "[]";51 //}52 //String iconsText = null;53 //try {54 //iconsText = IOHelper.readAllText(physicalPath);55 //} catch (IOException e) {56 //this._logger.error("ResourcesController.iconData()", e);57 //}58 //endregion59

60 //region SpringBoot读取Resource下文件的几种方式(通过类加载器读取文件流,类加载器可以读取jar包中的编译后的class文件,当然也是可以读取jar包中的文件流了)61 //https://www.jianshu.com/p/7d7e5e4e8ae3

62 ClassPathResource classPathResource = new ClassPathResource("static/Content/jquery.easyUI/1.5.1/themes/icon.css");63 if (classPathResource == null) {64 return "[]";65 }66 String iconsText = null;67 try (InputStream inputStream =classPathResource.getInputStream()) {68 iconsText =IOHelper.readAllText(inputStream);69 } catch(IOException e) {70 this._logger.error("ResourcesController.iconData()", e);71 return "[]";72 }73 //endregion

74

75 StringBuilder sb = new StringBuilder("[{\"text\":\"无\",\"value\":\"\"}");76 if(StringUtils.isNotBlank(iconsText)) {77 Matcher matcher = this._regex.matcher(iconsText);78 String value = null;79 while(matcher.find()) {80 value = matcher.group(1).trim();81 sb.append(String.format(",{\"text\":\"%s\",\"value\":\"%s\"}", value, value));82 }83 }84 sb.append("]");85 this._icons =sb.toString();86 sb = null;87 }88 return this._icons;89 }90 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值