import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
*擷取配置密碼XML
* @param attrName
* @return
*/
protected String getStandardPwd(String attrName) {
if (e == null) {
try {
ServletContext s = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();//擷取伺服器路徑
File f = new File(s.getRealPath("/") + "resources" + File.separatorChar + "useconfig.xml");//擷取XML檔案路徑
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(f);//
e = doc.getRootElement().getChild("userconfig");//擷取根節(getRootElement)點下的PureFtpd(getChild)子節點
} catch (Exception ex) {
return null;
}
}
return e.getChild(attrName).getAttribute("value").getValue();//擷取結點的屬性值
}
/**
* 寫XML檔案
* @param attrName
*/
protected void setStandardPwd(String attrName, String randNumber) {
try {
ServletContext s = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String path = s.getRealPath("/") + "resources" + File.separatorChar + "useconfig.xml";//擷取XML檔案路徑
FileInputStream fi = new FileInputStream(path);//將檔案寫入流
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(fi);//
e = doc.getRootElement().getChild("userconfig");//擷取根節(getRootElement)點下的PureFtpd(getChild)子節點
e.getChild(attrName).getAttribute("value").setValue(randNumber);//將屬性設定為隨機數
FileOutputStream fz = null;
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent, newLines, "GBK");
fz = new FileOutputStream(path);//輸出資料流
outp.output(doc, fz);//儲存XML檔案修改
fi.close();
fz.close();
} catch (Exception ex) {
System.out.println(ex.toString());
}
}