前台JS:注意:url后的 ?disc=baseLineTemplate 是必须要加的,因为要走先处理一下
var serialNumberUrl = "";
$(function() {
$("#buttonSubmit").bind("click", function() {
var serialNumberFileVal = $("#serialNumberFile").val();
serialNumberFileVal = serialNumberFileVal.substring(serialNumberFileVal.lastIndexOf('.') + 1);
if (serialNumberFileVal != "xml") {
showAlertDialog("提示", "请选择xml文件上传");
return false;
} else {
serialNumberUrl = "${basePath}/genuineSerialNumberManagementAction_importSerialNumber.do?disc=baseLineTemplate";
document.serialNumber_importSerialNumber.action = serialNumberUrl;
serialNumber_importSerialNumber.submit();
}
});
/** 验证文件是否导入成功 */
$("#serialNumber_importSerialNumber").ajaxForm(function(data){
setMessage(data);
});
});
后台解析数据:
//导入模板
public void importSerialNumber(){
this.msg = RESULT_ERROR;
try{
// 记录文件上传的MAP对象
User currentUser = SessionUtil.getInstance().getUserFromSession(this.getRequest());
final String name = currentUser.getAccount().trim();// 加入实际项目后,获取文件上传的系统用户名称,即登录用记名称,唯一性
for (Entry<String, String[]> entry : FileUploadConstant.fileOperator_baseTemplate.entrySet()) {
File file = new File(entry.getValue()[1]);
this.msg = genuineSerialNumberManagementService.importTemplate(file);
FileUploadConstant.fileOperator_baseTemplate.remove(entry.getKey());
}
FileUploadConstant.fileOperator_baseTemplate.clear();
genuineSerialNumberManagementService.createXML();
}catch(Exception e){
log.error("导入规则库失败!", e);
this.msg = RESULT_ERROR;
}
print(this.msg);
}
public String importTemplate(File file) {
String message = "error";
try {
//获取文件
Document doc = XMLUtil.getDocument(file.getPath());
// 获取根节点
Element root = doc.getRootElement();
//根据根节点判断type值
Integer type = null;
if("SoftLicW".equals(root.getQName().getName())){
type = 1;
}else if("SoftLicB".equals(root.getQName().getName())){
type = 0;
}
// 获取Software节点
Iterator<?> ruleNode = root.elementIterator("Software");
// 循环Software节点,判断是否存在
while (ruleNode.hasNext()) {
Element ruleElement = (Element) ruleNode.next();
Attribute ruleXmlId = ruleElement.attribute("SoftId");
Attribute ruleXmlName = ruleElement.attribute("DisplayName");
// 获取license节点
Iterator<?> license = ruleElement.elementIterator("license");
// 循环license节点,判断是否存在
while (license.hasNext()) {
Element licenseElement = (Element) license.next();
Attribute licenseVersion = licenseElement.attribute("Version");
Attribute licenseValue = licenseElement.attribute("value");
GenuineSerialNumberManagement serialNumber = new GenuineSerialNumberManagement();
serialNumber.setType(type);
serialNumber.setSoftId(Integer.valueOf(ruleXmlId.getText()));
serialNumber.setSoftDisplayName(ruleXmlName.getText());
serialNumber.setVersion(licenseVersion.getText());
serialNumber.setSerialNumber(licenseValue.getText());
genuineSerialNumberManagementDao.save(serialNumber);//保存解析出来的数据到数据库
}
}
message = "success";
} catch (Exception e) {
e.printStackTrace();
message = "error";
}
return message;
}