获得eclipse发布的web项目WEB-INFO下的路径(最淳朴的方法)

简介:

不解析,直接贴代码

public class TransformService {

	private Logger log = Logger.getLogger(TransformService.class);

	private final String TEMPLET = "templet";

	/**
	 * 转换类
	 * 
	 * @param xmlReq
	 * @param xslTempletName
	 * @return
	 */
	public String xmlTransform(String xmlReq, String xslTempletName) {
		// TODO Auto-generated method stub
		try {
			log.info("Para xml:" + xmlReq);
			log.info("Para xslTempletName:" + xslTempletName);
			Resource res = new ClassPathResource(TEMPLET + "/" + xslTempletName
					+ ".xsl");
			log.info("Templet URL:" + res.getFile() == null ? null : res
					.getFile().toString());
			Source templet = new StreamSource(res.getFile());
			Transformer transf = TransformerFactory.newInstance()
					.newTransformer(templet);
			Document req = DocumentHelper.parseText(xmlReq);
			Source xmlSource = new StreamSource(new StringReader(req.asXML()));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			Result result = new StreamResult(baos);
			transf.setOutputProperty(OutputKeys.METHOD, "xml");
			transf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transf.setOutputProperty(OutputKeys.INDENT, "yes");
			transf.transform(xmlSource, result);
			ByteArrayInputStream bais = new ByteArrayInputStream(
					baos.toByteArray());
			String path = res.getFile().toString();
			String rPath = path.substring(0, path.lastIndexOf("classes"));
			File folder = new File(rPath + "xmlFolder");
			log.info("XML_FOLD_DIR:" + folder == null ? null : folder
					.toString());
			if (!folder.exists()) {
				folder.mkdir();
			}
			File file = new File(folder + File.separator + xslTempletName + "_"
					+ new Date().getTime() + ".xml");
			log.info("XML_FILE_URL:" + file == null ? null : file.toString());
			FileOutputStream fis = new FileOutputStream(file);
			fis.write(baos.toByteArray());
			fis.close();
			SAXReader sax = new SAXReader();
			Document resXML = sax.read(bais);
			log.info(resXML.asXML());
			System.out.println(resXML.asXML());
			return resXML.asXML();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
		} catch (TransformerFactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return null;
	}

	public static void main(String[] args) {
		try {
			TransformService ts = new TransformService();
			Resource res = new ClassPathResource("templet/DY.xml");
			SAXReader sax = new SAXReader();
			Document resXML = sax.read(res.getFile());
			ts.xmlTransform(resXML.asXML(), "DY2HS");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}


目录
相关文章
|
8天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
40 7
|
3月前
|
缓存 JSON 监控
如何在项目中保证 Web 组件化的性能
保证 Web 组件化的性能需要从多个方面入手,综合运用各种优化方法和策略。通过持续的优化和改进,能够提高组件化的整体性能,为用户提供更好的体验,同时也有助于提高项目的开发效率和质量。
60 8
|
3月前
|
存储 前端开发 JavaScript
如何在项目中高效地进行 Web 组件化开发
高效地进行 Web 组件化开发需要从多个方面入手,通过明确目标、合理规划、规范开发、加强测试等一系列措施,实现组件的高效管理和利用,从而提高项目的整体开发效率和质量,为用户提供更好的体验。
55 7
|
3月前
|
中间件 Go API
Go语言中几种流行的Web框架,如Beego、Gin和Echo,分析了它们的特点、性能及适用场景,并讨论了如何根据项目需求、性能要求、团队经验和社区支持等因素选择最合适的框架
本文概述了Go语言中几种流行的Web框架,如Beego、Gin和Echo,分析了它们的特点、性能及适用场景,并讨论了如何根据项目需求、性能要求、团队经验和社区支持等因素选择最合适的框架。
228 1
|
Java Android开发
eclipse去掉try-catch,构造方法,get,set方法等里面自动生成的注释
eclipse去掉try-catch,构造方法,get,set方法等里面自动生成的注释
121 0
|
Android开发
eclipse括号对齐及自动添加方法注释
一、eclipse括号对齐 本人是特别讨厌eclipse默认的括号对齐方式,如下图。 如何将括号对应方式设置像C/C++的上下对齐方式? 第一步:Project->preferences->Java->Code Styl...
1388 0
|
9月前
|
XML Java Maven
eclipse 、idea 安装activiti插件
eclipse 、idea 安装activiti插件
224 0
|
9月前
|
Java Maven Android开发
在Eclipse里配置Maven插件
Maven是一款比较常用的Java开发拓展包,它相当于一个全自动jar包管理器,会导入用户开发时需要使用的相应jar包。使用Maven开发Java程序,可以极大提升开发者的开发效率。下面我就跟大家介绍一下如何在Eclipse里安装和配置Maven插件。
216 0
|
Java Android开发
eclipse安装SpringBoot插件的无敌办法
eclipse安装SpringBoot插件的无敌办法
197 0
|
Java Android开发 Spring
在 Eclipse 中安装 SpringTools 插件
在 Eclipse 中安装 SpringTools 插件
171 0

热门文章

最新文章

推荐镜像

更多