在Spring的applicationContext.xml文件中设置和读取properties资源文件时,如果properties文件是UTF-8编码的,并且需要读取的内容包含中文,那么采取默认的property-placeholder标签配置,则读取属性时便会出现乱码!
通过度娘找到很多相似的问题,但几乎都只有一个解决办法,就是使用jdk自带的native2ascii.exe工具,将properties文件中的中文变为编码的形式,若采用这种办法,那么properties文件几乎是没有可读性的!
查阅Spring的官方手册(spring-framework-reference.pdf),发现配置文件中的context:property-placeholder标签对应的是PropertyPlaceholderConfigurer类,再去查Spring的javadoc-api,发现这个类中有一个setFileEncoding方法,可以设置读取properties文件的编码格式,有戏啊!那么context:property-placeholder标签中有没有对应的attribute可以设置呢?再次查阅applicationContext.xml文件针对context:property-placeholder标签的xsd校验文件(spring-context-4.0.xsd),发现propertyPlaceholder元素有个file-encoding属性,于是乎马上用到配置文件中,乱码问题终于解决了!
也就是将原来的:
- <context:property-placeholder location="classpath:*.properties"/> 改成如下
- <context:property-placeholder location="classpath:*.properties" file-encoding="UTF-8"/>
String sign = new String(signVar.getBytes("utf-8"), "utf-8");
/**
* Post请求,访问短信网关
*
* @param name
* 用户名
* @param pwd
* 密码
* @param mobile
* 电话号码字符串,中间用英文逗号间隔
* @param content
* 内容字符串
* @param sign
* 签名
* @param stime
* 追加发送时间,可为空,为空为及时发送
* @param extno
* 扩展码,必须为数字 可为空
* @return
* @throws Exception
*/
public static String doPost(String url, String name, String pwd, String mobile,
String content, String sign, String stime, String extno)
throws Exception {
StringBuffer param = new StringBuffer();
param.append("name=" + name);
param.append("&pwd=" + pwd);
param.append("&mobile=").append(mobile);
param.append("&content=").append(URLEncoder.encode(content,"UTF-8"));
param.append("&stime=" + stime);
param.append("&sign=").append(URLEncoder.encode(sign,"UTF-8"));
param.append("&type=pt");
param.append("&extno=").append(extno);
URL localURL = new URL(url + "?");
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length",
String.valueOf(param.length()));
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
String resultBuffer = "";
try {
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(param.toString());
outputStreamWriter.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception(
"HTTP Request is not success, Response code is "
+ httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
resultBuffer = convertStreamToString(inputStream);
} finally {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer;
}