JSP 中Spring的Resource类读写中文Properties
摘要: Spring对Properties的读取进行了完善而全面的封装,对于写则仍需配合FileOutputStream进行。
package com.oolong.common.util; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import java.io.*; import java.util.*; public class UserVar { private static String configFile = "classpath*:param.properties"; private static org.springframework.core.io.Resource resourceWritable; private static Properties p; /** * 注意事项: * 1、properties放在source目录下 * 2、param.properties至少有一对键值对 */ static { p = new Properties(); org.springframework.core.io.Resource[] resources = null; try { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); resources = resolver.getResources(configFile); if (resources != null) { for (org.springframework.core.io.Resource r : resources) { if (r != null) { p.load(r.getInputStream()); resourceWritable = r; } } } } catch (IOException e1) { e1.printStackTrace(); } } public static String get(String key) { String v = (String) p.get(key); if (v != null) { try { return new String(v.getBytes("ISO-8859-1"), "GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } return null; } public static void set(String key,String value){ if (null != resourceWritable) { try { OutputStream fos = new FileOutputStream(resourceWritable.getFile()); Properties p = new Properties(); p.load(resourceWritable.getInputStream()); value = new String(value.getBytes("GBK"),"ISO-8859-1"); p.setProperty(key, value); p.store(fos, null); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
MySQL的binlog相信大家都有所耳闻,但是可能没有真正日常使用过。 因此,本文结...
博主最近在做一个个人的博客网站,准备用 thymeleaf 实现一个动态加载一二级文章...
一、GIF图 二、前台代码 // 调用方法 hotlineLine(); // 定时刷新 setInterval(f...
详解JSP中使用过滤器进行内容编码的解决办法 问题 当通过JSP页面,向数据库中插...
前言 静态文件(如 HTML、CSS、图像和 JavaScript)等是Web程序的重要组成部分。...
为什么我们需要它 不得不说,在知道这个命令的时,以及之后的使用中,我都超级热...
本文转载自微信公众号「三太子敖丙」,作者三太子敖丙。转载本文请联系三太子敖...
目录中出现 jsconfig.json 文件表明该目录是 JavaScript 项目的根目录。 Json 文...
通过ImageMagickObject的identify获取图片的信息,在命令行下好用,但是放到程序...
端点路由(Endpoint Routing)最早出现在ASP.NET Core2.2,在ASP.NET Core3.0提升...