springboot从外部xml文件导入配置出现中文乱码问题

最近做一个项目,遇到从xml文件读取配置,中文乱码问题,使用如下代码解决,在此分享一下,不到之处,请多指正!

	/**
	 * 将file类型的xml转换成对象
	 */
	@SuppressWarnings({ "rawtypes" })
	public static Object XmlFileToObject(Class clazz, String xmlPath) {
		Object xmlObject = null;
		if(xmlPath==null) {
			return null;	
		}
		try {
			JAXBContext context = JAXBContext.newInstance(clazz);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			Reader reader;
			try {
				reader = new InputStreamReader(new FileInputStream(xmlPath), "UTF-8");
				xmlObject = unmarshaller.unmarshal(reader);
				System.out.println(xmlObject);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		} catch (JAXBException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return xmlObject;
	}
}

<dependency>
	<groupId>org.jdom</groupId>
	<artifactId>jdom</artifactId>
	<version>2.0.2</version>
</dependency>

你可能感兴趣的:(分享)