获取WEB-INF的绝对路径和读改XML文件

获取WEB-INF的绝对路径和读改XML文件

 

package com.oaking.fund.cmb.util;

import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.*;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.xml.sax.SAXException;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import java.io.File;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
* <p>Title:密钥读改 </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: HISUN</p>
*
* @author hwj
* @version 1.0
*/
public class KeyXMLTool {
    //保存密钥的XML文件
    private static String CMBKeyFileName="cmbKey.xml";
    //得到WEB-INF的绝对路径
    private static String web_inf_Path=new File(KeyXMLTool.class.getResource("/").getPath()).getParent()+"/WEB-INF";
    private static String CMBKeyFilePath=web_inf_Path+"/"+CMBKeyFileName;
    private static DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
    /**
     * 获取一Document对象
     * @param fileName String
     * @return Document
     */
    private static Document getDocumentByFileName(final String fileName){
        Document doc=null;
        try {
            DocumentBuilder dombuilder = domfac.newDocumentBuilder();
            InputStream in=new FileInputStream(fileName);
//            InputStream in=ClassLoader.getSystemResourceAsStream(fileName);
//            InputStream in = KeyXMLTool.class.getResourceAsStream("/Key.xml");
            doc=dombuilder.parse(in);
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
        return doc;
    }
    public static String getPINKey(){
//        System.out.println(">>>>>当前类所在绝对目录 "+KeyXMLTool.class.getResource(".").getPath());
//        System.out.println(">>>>>当前类所在绝对目录 "+KeyXMLTool.class.getResource("").getPath());
//        System.out.println("TEST PATH="+KeyXMLTool.class.getResource("/").getPath());
        String pinKey=null;
        Document doc=getDocumentByFileName(CMBKeyFilePath);
        NodeList pinKeyNodeList=doc.getElementsByTagName("pinkey");
        Node pinKeyNode=pinKeyNodeList.item(0);
        pinKey=pinKeyNode.getFirstChild().getNodeValue();
        return pinKey;
    }
    public static synchronized void setPINKEY(final String pinKey){
        Document doc=getDocumentByFileName(CMBKeyFilePath);
        NodeList pinKeyNodeList=doc.getElementsByTagName("pinkey");
        Node pinKeyNode=pinKeyNodeList.item(0);
        pinKeyNode.getFirstChild().setNodeValue(pinKey);
        doc2XmlFile(doc,CMBKeyFilePath);
    }
   
    public static String getMACKEY(){
        String macKey=null;
        Document doc=getDocumentByFileName(CMBKeyFilePath);
        NodeList pinKeyNodeList=doc.getElementsByTagName("mackey");
        Node pinKeyNode=pinKeyNodeList.item(0);    
        macKey=pinKeyNode.getFirstChild().getNodeValue();
        return macKey;

    }
   
    public static synchronized void setMACKEY(final String macKey){       
        Document doc=getDocumentByFileName(CMBKeyFilePath);
        NodeList pinKeyNodeList=doc.getElementsByTagName("mackey");
        Node pinKeyNode=pinKeyNodeList.item(0);    
        pinKeyNode.getFirstChild().setNodeValue(macKey);
        doc2XmlFile(doc,CMBKeyFilePath);
    }
   
    /** 将document中的内容写入文件中   */
    public static boolean doc2XmlFile(Document document,String filename){
      boolean flag = true;
      try {
             TransformerFactory tFactory = TransformerFactory.newInstance();   
             Transformer transformer = tFactory.newTransformer();
            /** 编码 */
            //transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
             DOMSource source = new DOMSource(document);
             StreamResult result = new StreamResult(new File(filename));   
             transformer.transform(source, result);
         }catch(Exception ex)
         {
             flag = false;
             ex.printStackTrace();
         }
        return flag;      
    }


    public static void main(String[] args){
//        File tFile=new File(KeyXMLTool.class.getResource("/").getPath());
//        System.out.println("父目录"+tFile.getParent());
//        System.out.println(tFile.getAbsolutePath());
//        System.out.println(">>>>>根目录 "+KeyXMLTool.class.getResource("/").getPath());
        System.out.println(">>>PINKEY="+getPINKey());
        setPINKEY("");
        System.out.println(">>>PINKEY22="+getPINKey());
        System.out.println(">>>MACKEY="+getMACKEY());
        setMACKEY("8#$$$$$$iio");
         System.out.println(">>>MACKEY22="+getMACKEY());
    }

}

 

 

XML如下

<?xml version='1.0' encoding="UTF-8" ?>
<key>
<pinkey>121212</pinkey>
<mackey></mackey>
</key>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值