修正读取配置文件utf-8编码的bug!
This commit is contained in:
		
							
								
								
									
										0
									
								
								code/java/DisasterWarning/app.log.lck
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								code/java/DisasterWarning/app.log.lck
									
									
									
									
									
										Normal file
									
								
							@@ -2,7 +2,7 @@
 | 
			
		||||
 * @Author: Kane
 | 
			
		||||
 * @Date: 2022-04-22 10:53:49
 | 
			
		||||
 * @LastEditors: Kane
 | 
			
		||||
 * @LastEditTime: 2022-04-23 23:22:57
 | 
			
		||||
 * @LastEditTime: 2022-04-26 22:33:14
 | 
			
		||||
 * @FilePath: \DisasterWarning\src\main\java\AppMain.java
 | 
			
		||||
 * @Description: 和风天气预警推送厦门太保公众号主程序!
 | 
			
		||||
 * 
 | 
			
		||||
@@ -12,29 +12,50 @@ import com.cpic.xim.config.City;
 | 
			
		||||
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
 | 
			
		||||
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
 | 
			
		||||
import com.cpic.xim.config.WeatherDisasterNotifyConfig;
 | 
			
		||||
import com.cpic.xim.wechat.officalAccount.sendMessage;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.Vector;
 | 
			
		||||
import java.util.logging.*;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class AppMain
 | 
			
		||||
{
 | 
			
		||||
    private final static String LOG_FILE_PATH = "./app.log";
 | 
			
		||||
 | 
			
		||||
    public static void main( String[] args )
 | 
			
		||||
    {
 | 
			
		||||
        String json;
 | 
			
		||||
        WeatherDisasterNotifyConfig config = 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
 | 
			
		||||
        {
 | 
			
		||||
            config = WeatherDisasterNotifyConfig.getConfig();
 | 
			
		||||
        }
 | 
			
		||||
        catch (IOException error)
 | 
			
		||||
        catch ( IOException error )
 | 
			
		||||
        {
 | 
			
		||||
            System.out.println( "读取配置文件失败!" );
 | 
			
		||||
            System.out.println( error.getMessage() );
 | 
			
		||||
 | 
			
		||||
            logger.log( Level.SEVERE, "读取配置文件失败:{0}", error.getMessage() );
 | 
			
		||||
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -43,30 +64,60 @@ public class AppMain
 | 
			
		||||
        String userKey = config.getKey();
 | 
			
		||||
 | 
			
		||||
        // 遍历所有城市,查询是否有警报,有则推送。
 | 
			
		||||
        for ( City city : cities)
 | 
			
		||||
        for ( City city : cities )
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
 | 
			
		||||
                        userKey, city.getCityCode() );
 | 
			
		||||
                warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
 | 
			
		||||
 | 
			
		||||
                logger.log( Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[]
 | 
			
		||||
                { city.getCityName(), json} );
 | 
			
		||||
 | 
			
		||||
                // 判断是否有警报
 | 
			
		||||
                if ( warning.getWarning().isEmpty() == true)
 | 
			
		||||
                {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(),
 | 
			
		||||
                        warning );
 | 
			
		||||
                logger.log( Level.INFO, "查询{0}天气预警,发送日志。", new Object[]
 | 
			
		||||
                { city.getCityName()} );
 | 
			
		||||
 | 
			
		||||
                // sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(),
 | 
			
		||||
                // warning );
 | 
			
		||||
            }
 | 
			
		||||
            catch (IOException error)
 | 
			
		||||
            catch ( IOException error )
 | 
			
		||||
            {
 | 
			
		||||
                System.out.println( "查询" + city.getCityName() + "出现异常!" );
 | 
			
		||||
                System.out.println( error.getMessage() );
 | 
			
		||||
 | 
			
		||||
                logger.log( Level.SEVERE, "查询 {0} 出现异常:{1}。", new Object[]
 | 
			
		||||
                { city.getCityName(), error.getMessage()} );
 | 
			
		||||
 | 
			
		||||
                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 );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package com.cpic.xim.config;
 | 
			
		||||
 | 
			
		||||
import java.io.FileReader;
 | 
			
		||||
import java.io.InputStreamReader;
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.Vector;
 | 
			
		||||
 | 
			
		||||
@@ -11,6 +12,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategy;
 | 
			
		||||
public class WeatherDisasterNotifyConfig
 | 
			
		||||
{
 | 
			
		||||
    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 WeatherDisasterNotifyConfig appConfig = null;
 | 
			
		||||
 | 
			
		||||
@@ -23,7 +25,8 @@ public class WeatherDisasterNotifyConfig
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ObjectMapper mapper = new ObjectMapper();
 | 
			
		||||
        FileReader configFile = null;
 | 
			
		||||
        FileInputStream configFile = null;
 | 
			
		||||
        InputStreamReader in = null;
 | 
			
		||||
        StringBuffer json = null;
 | 
			
		||||
        char[] buffer = new char[BUFFER_SIZE];
 | 
			
		||||
 | 
			
		||||
@@ -32,16 +35,17 @@ public class WeatherDisasterNotifyConfig
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            configFile = new FileReader( CONFIG_FILE_PATH );
 | 
			
		||||
            configFile = new FileInputStream( CONFIG_FILE_PATH );
 | 
			
		||||
            in = new InputStreamReader( configFile, CONFIG_FILE_CHARSET );
 | 
			
		||||
            json = new StringBuffer();
 | 
			
		||||
 | 
			
		||||
            int length = configFile.read( buffer );
 | 
			
		||||
            int length = in.read( buffer );
 | 
			
		||||
 | 
			
		||||
            while (length != -1)
 | 
			
		||||
            {
 | 
			
		||||
                json.append( buffer );
 | 
			
		||||
 | 
			
		||||
                length = configFile.read( buffer );
 | 
			
		||||
                length = in.read( buffer );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
 | 
			
		||||
@@ -54,7 +58,7 @@ public class WeatherDisasterNotifyConfig
 | 
			
		||||
                {
 | 
			
		||||
                    configFile.close();
 | 
			
		||||
                }
 | 
			
		||||
                catch (IOException e)
 | 
			
		||||
                catch ( IOException e )
 | 
			
		||||
                {
 | 
			
		||||
                    e.printStackTrace();
 | 
			
		||||
                }
 | 
			
		||||
@@ -135,6 +139,4 @@ public class WeatherDisasterNotifyConfig
 | 
			
		||||
    private String wechatOfficalAccountURL;
 | 
			
		||||
    private Vector<City> cities;
 | 
			
		||||
    private Vector<CpicxmStuff> notifyStuffs;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.*;
 | 
			
		||||
public class OracleConfigLoader
 | 
			
		||||
{
 | 
			
		||||
    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 OracleConfig dbConfig = null;
 | 
			
		||||
 | 
			
		||||
@@ -25,7 +26,8 @@ public class OracleConfigLoader
 | 
			
		||||
 | 
			
		||||
    public OracleConfig getOracleConfig() throws IOException
 | 
			
		||||
    {
 | 
			
		||||
        FileReader file = null;
 | 
			
		||||
        FileInputStream file = null;
 | 
			
		||||
        InputStreamReader in = null;
 | 
			
		||||
        StringBuffer json = null;
 | 
			
		||||
        char[] buffer = new char[BUFFER_SIZE];
 | 
			
		||||
 | 
			
		||||
@@ -36,16 +38,18 @@ public class OracleConfigLoader
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            file = new FileReader( CONFIG_FILE_PATH );
 | 
			
		||||
            file = new FileInputStream( CONFIG_FILE_PATH );
 | 
			
		||||
            in = new InputStreamReader( file, CONFIG_FILE_CHARSET);
 | 
			
		||||
            json = new StringBuffer();
 | 
			
		||||
 | 
			
		||||
            int count = file.read( buffer );
 | 
			
		||||
 | 
			
		||||
            int count = in.read( buffer );
 | 
			
		||||
 | 
			
		||||
            while (count != -1)
 | 
			
		||||
            {
 | 
			
		||||
                json.append( buffer );
 | 
			
		||||
 | 
			
		||||
                count = file.read( buffer );
 | 
			
		||||
                count = in.read( buffer );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            ObjectMapper mapper = new ObjectMapper();
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 * @Author: Kane
 | 
			
		||||
 * @Date: 2022-04-22 10:53:49
 | 
			
		||||
 * @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
 | 
			
		||||
 * @Description:
 | 
			
		||||
 *
 | 
			
		||||
@@ -16,7 +16,7 @@ import org.junit.Test;
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
import com.cpic.xim.httpUtil.*;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.text.SimpleDateFormat;
 | 
			
		||||
//import java.text.SimpleDateFormat;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
@@ -33,7 +33,7 @@ public class sendMessageTest
 | 
			
		||||
        {
 | 
			
		||||
            // sendMessage.postNotifyMessageJSON( url, "警报", "警报标题", "警报内容!" );
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception error)
 | 
			
		||||
        catch ( Exception error )
 | 
			
		||||
        {
 | 
			
		||||
            fail( "测试失败!" );
 | 
			
		||||
        }
 | 
			
		||||
@@ -55,7 +55,7 @@ public class sendMessageTest
 | 
			
		||||
        {
 | 
			
		||||
            HttpUtils.postHttpRequest( url, headers, param );
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception error)
 | 
			
		||||
        catch ( Exception error )
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -73,12 +73,12 @@ public class sendMessageTest
 | 
			
		||||
            QWeatherDisasterWarning warning =
 | 
			
		||||
                    mapper.readValue( warningJSON, QWeatherDisasterWarning.class );
 | 
			
		||||
 | 
			
		||||
            SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
 | 
			
		||||
            String putTime = format.format( warning.getUpdateTime() );
 | 
			
		||||
            // SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
 | 
			
		||||
            // String putTime = format.format( warning.getUpdateTime() );
 | 
			
		||||
 | 
			
		||||
            sendMessage.sendWeatherDisasterWarning( url, warning );
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception error)
 | 
			
		||||
        catch ( Exception error )
 | 
			
		||||
        {
 | 
			
		||||
            fail( error.getMessage() );
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user