SpringBoot读取resources下的文件以及resources的资源路径

容器和服务器中都可以获取:

String curdJsonVm = readCurdJsonVm("classpath:/jsonVm/crudJson.vm");
public String readCurdJsonVm(String jsonVmPath) {
    // 读取resources下的文件以及resources的资源路径
    org.springframework.core.io.Resource resource = resourceLoader.getResource(jsonVmPath);
    // 获1.txt的取相对路径
    try {
      InputStream is = resource.getInputStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      StringBuilder stringBuilder = new StringBuilder();
      String data;
      while ((data = br.readLine()) != null) {
        stringBuilder.append(data);
      }
      br.close();
      isr.close();
      is.close();
      if (isBlank(stringBuilder)) {
        // 文件内容未配置
        throw new BizException(PAGE__NULL);
      }
      return String.valueOf(stringBuilder);
    } catch (IOException e) {
      log.error("读取json.vm文件失败: {}", e.getMessage());
      throw new BizException(
          READ_FILE_ERROR.getCode(), READ_FILE_ERROR.getMessage() + ":" + e.getMessage());
    }
  }

你可能感兴趣的:(Java,Web,spring,boot,java,spring)