JavaEE 工具代码片段(二)

Code Tools

FastJSON 设置不重复利用对象$.ref{}
JSON.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
JSON.toJSONStringWithDateFormat(data, DEFAULT_DATE_TIME_FORMAT, SerializerFeature.DisableCircularReferenceDetect);
使用Spring RequestContextHolder 获取请求 request 与 response
 public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public static HttpServletResponse getHttpServletResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }
利用spring来读取配置文件 - [简约]
 1.日常读取文件
         String kpath = new Object() {
         }.getClass().getEnclosingClass().getResource("/").toString()
                 .replace("/WEB-INF/classes/", "").replace("file:", "");
         log.info(" path  {}", kpath);
         File file = new File(kpath);
         kpath = file.getParentFile().getPath();
         kpath += File.separator + "classes" + File.separator + "config" + File.separator + config_file;
         file = new File(kpath);
         if (!file.exists()) {
             return null;
         }
         FileInputStream inputStream = new FileInputStream(file);
         int length = inputStream.available();
         byte bytes[] = new byte[length];
         inputStream.read(bytes);
         inputStream.close();
         String str = new String(bytes, StandardCharsets.UTF_8);
         return str;

  2.Spring方式的读取
        Resource resource = new ClassPathResource("/config/buildModel.txt");
        InputStream inputStream = resource.getInputStream();
        String text = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
        log.info("content  :  {}", text);
        return text;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值