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 }