Java获取服务器系统默认编码格式

本文介绍两种方法来获取项目的系统属性,特别是文件编码(file.encoding)。方法一通过创建JSP页面并使用内嵌的Java代码输出属性值;方法二则是在Spring管理类中将系统属性写入指定文件,便于查阅。
大佬教的,做个笔记
方法一(推荐):
新建一个jsp页面在webapp下
然后添加
<%
out.print(System.getProperties().getProperty("file.encoding"));
%>
访问即可


方法二:打印到文件

//Spring管理类中获取requets.attributes
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attrs == null) {
// throw new IllegalStateException("当前线程中不存在 Request 上下文");
}else{
//信息源
Properties properties = System.getProperties();
//获取真实项目路径('.../webapps' + '/images/testLog.txt')
String logRealPath = attrs.getRequest().getServletContext().getRealPath("/images/testLog.txt");
//开启流
OutputStream out=new FileOutputStream(logRealPath,true);
//拿到系统信息
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
//打印到文件当中
for (Map.Entry entry:entries
) {
out.write(entry.getKey().toString().getBytes("UTF-8"));
out.write("-->".getBytes("UTF-8"));
out.write(entry.getValue().toString().getBytes("UTF-8"));
out.write("\n".getBytes("UTF-8"));
}

//关闭流
out.close();
}

//浏览器输入项目路径/images/testLog.txt查看
查找“file.encoding”


转载于:https://www.cnblogs.com/zou-rong/p/11006187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值