修正读取配置文件utf-8编码的bug!

This commit is contained in:
Kane Wang 2022-04-26 23:17:24 +08:00
parent 9d57987f0c
commit 59977ec9b6
5 changed files with 83 additions and 26 deletions

View File

View File

@ -2,7 +2,7 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 10:53:49 * @Date: 2022-04-22 10:53:49
* @LastEditors: Kane * @LastEditors: Kane
* @LastEditTime: 2022-04-23 23:22:57 * @LastEditTime: 2022-04-26 22:33:14
* @FilePath: \DisasterWarning\src\main\java\AppMain.java * @FilePath: \DisasterWarning\src\main\java\AppMain.java
* @Description: 和风天气预警推送厦门太保公众号主程序 * @Description: 和风天气预警推送厦门太保公众号主程序
* *
@ -12,18 +12,37 @@ import com.cpic.xim.config.City;
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber; import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
import com.cpic.xim.config.WeatherDisasterNotifyConfig; import com.cpic.xim.config.WeatherDisasterNotifyConfig;
import com.cpic.xim.wechat.officalAccount.sendMessage;
import java.io.IOException; import java.io.IOException;
import java.util.Vector; import java.util.Vector;
import java.util.logging.*;
public class AppMain public class AppMain
{ {
private final static String LOG_FILE_PATH = "./app.log";
public static void main( String[] args ) public static void main( String[] args )
{ {
String json; String json;
WeatherDisasterNotifyConfig config = null; WeatherDisasterNotifyConfig config = null;
QWeatherDisasterWarning warning = null; QWeatherDisasterWarning warning = null;
Logger logger = null;
// 配置logger
try
{
setRootLogger();
logger = Logger.getLogger( "com.cpicxim" );
}
catch ( IOException error )
{
System.out.println( "配置logger失败原因" + error.getMessage() );
return;
}
// 读取配置 // 读取配置
try try
@ -35,6 +54,8 @@ public class AppMain
System.out.println( "读取配置文件失败!" ); System.out.println( "读取配置文件失败!" );
System.out.println( error.getMessage() ); System.out.println( error.getMessage() );
logger.log( Level.SEVERE, "读取配置文件失败:{0}", error.getMessage() );
return; return;
} }
@ -47,26 +68,56 @@ public class AppMain
{ {
try try
{ {
json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL, json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
userKey, city.getCityCode() ); userKey, city.getCityCode() );
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json ); warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
logger.log( Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[]
{ city.getCityName(), json} );
// 判断是否有警报 // 判断是否有警报
if ( warning.getWarning().isEmpty() == true) if ( warning.getWarning().isEmpty() == true)
{ {
continue; continue;
} }
sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(), logger.log( Level.INFO, "查询{0}天气预警,发送日志。", new Object[]
warning ); { city.getCityName()} );
// sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(),
// warning );
} }
catch ( IOException error ) catch ( IOException error )
{ {
System.out.println( "查询" + city.getCityName() + "出现异常!" ); System.out.println( "查询" + city.getCityName() + "出现异常!" );
System.out.println( error.getMessage() ); System.out.println( error.getMessage() );
logger.log( Level.SEVERE, "查询 {0} 出现异常:{1}。", new Object[]
{ city.getCityName(), error.getMessage()} );
continue; continue;
} }
} }
} }
/**
* 设置JUL的logger
*/
private static void setRootLogger() throws IOException
{
Logger rootLogger = Logger.getLogger( "com.cpicxim" );
ConsoleHandler consoleHandler = new ConsoleHandler();
FileHandler fileHandler = new FileHandler( LOG_FILE_PATH );
SimpleFormatter formatter = new SimpleFormatter();
consoleHandler.setFormatter( formatter );
fileHandler.setFormatter( formatter );
rootLogger.addHandler( consoleHandler );
rootLogger.addHandler( fileHandler );
rootLogger.setUseParentHandlers( false );
rootLogger.setLevel( Level.ALL );
}
} }

View File

@ -1,6 +1,7 @@
package com.cpic.xim.config; package com.cpic.xim.config;
import java.io.FileReader; import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Vector; import java.util.Vector;
@ -11,6 +12,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategy;
public class WeatherDisasterNotifyConfig public class WeatherDisasterNotifyConfig
{ {
private static final int BUFFER_SIZE = 1024; private static final int BUFFER_SIZE = 1024;
private static final String CONFIG_FILE_CHARSET = "UTF-8";
private static final String CONFIG_FILE_PATH = "./config.json"; private static final String CONFIG_FILE_PATH = "./config.json";
private static WeatherDisasterNotifyConfig appConfig = null; private static WeatherDisasterNotifyConfig appConfig = null;
@ -23,7 +25,8 @@ public class WeatherDisasterNotifyConfig
} }
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
FileReader configFile = null; FileInputStream configFile = null;
InputStreamReader in = null;
StringBuffer json = null; StringBuffer json = null;
char[] buffer = new char[BUFFER_SIZE]; char[] buffer = new char[BUFFER_SIZE];
@ -32,16 +35,17 @@ public class WeatherDisasterNotifyConfig
try try
{ {
configFile = new FileReader( CONFIG_FILE_PATH ); configFile = new FileInputStream( CONFIG_FILE_PATH );
in = new InputStreamReader( configFile, CONFIG_FILE_CHARSET );
json = new StringBuffer(); json = new StringBuffer();
int length = configFile.read( buffer ); int length = in.read( buffer );
while (length != -1) while (length != -1)
{ {
json.append( buffer ); json.append( buffer );
length = configFile.read( buffer ); length = in.read( buffer );
} }
appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class ); appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
@ -135,6 +139,4 @@ public class WeatherDisasterNotifyConfig
private String wechatOfficalAccountURL; private String wechatOfficalAccountURL;
private Vector<City> cities; private Vector<City> cities;
private Vector<CpicxmStuff> notifyStuffs; private Vector<CpicxmStuff> notifyStuffs;
} }

View File

@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.*;
public class OracleConfigLoader public class OracleConfigLoader
{ {
private static final String CONFIG_FILE_PATH = "./db.json"; private static final String CONFIG_FILE_PATH = "./db.json";
private static final String CONFIG_FILE_CHARSET = "UTF-8";
private static final int BUFFER_SIZE = 1024; private static final int BUFFER_SIZE = 1024;
private static OracleConfig dbConfig = null; private static OracleConfig dbConfig = null;
@ -25,7 +26,8 @@ public class OracleConfigLoader
public OracleConfig getOracleConfig() throws IOException public OracleConfig getOracleConfig() throws IOException
{ {
FileReader file = null; FileInputStream file = null;
InputStreamReader in = null;
StringBuffer json = null; StringBuffer json = null;
char[] buffer = new char[BUFFER_SIZE]; char[] buffer = new char[BUFFER_SIZE];
@ -36,16 +38,18 @@ public class OracleConfigLoader
try try
{ {
file = new FileReader( CONFIG_FILE_PATH ); file = new FileInputStream( CONFIG_FILE_PATH );
in = new InputStreamReader( file, CONFIG_FILE_CHARSET);
json = new StringBuffer(); json = new StringBuffer();
int count = file.read( buffer );
int count = in.read( buffer );
while (count != -1) while (count != -1)
{ {
json.append( buffer ); json.append( buffer );
count = file.read( buffer ); count = in.read( buffer );
} }
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();

View File

@ -2,7 +2,7 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 10:53:49 * @Date: 2022-04-22 10:53:49
* @LastEditors: Kane * @LastEditors: Kane
* @LastEditTime: 2022-04-24 11:31:50 * @LastEditTime: 2022-04-26 23:16:46
* @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java * @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java
* @Description: * @Description:
* *
@ -16,7 +16,7 @@ import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.cpic.xim.httpUtil.*; import com.cpic.xim.httpUtil.*;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; //import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -73,8 +73,8 @@ public class sendMessageTest
QWeatherDisasterWarning warning = QWeatherDisasterWarning warning =
mapper.readValue( warningJSON, QWeatherDisasterWarning.class ); mapper.readValue( warningJSON, QWeatherDisasterWarning.class );
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" ); // SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
String putTime = format.format( warning.getUpdateTime() ); // String putTime = format.format( warning.getUpdateTime() );
sendMessage.sendWeatherDisasterWarning( url, warning ); sendMessage.sendWeatherDisasterWarning( url, warning );
} }