用struts+extJs实现:
JS代码:
/**
* 导出脚本
*/
publishScriptExport = function(objId,objType) {
Lkg.Ajax.request({
url : 'ppm.common.queryTableByStatementName',
jsonData : {
StatementName : 'taskMgrDAO.checkObjIsPublic',
parameterObject : {
objId : objId
}
},
success : function(res) {
var resJson = Ext.util.JSON.decode(res.responseText);
var flag = resJson.list[0].RESAULT;
if (flag >0 ) {
var frm = document.createElement('form');
frm.id = 'frmDummy';
frm.name = 'frmDummy';
frm.className = 'x-hidden';
frm.method = 'post';
frm.action = "../file/exportScriptToFile.action";
document.body.appendChild(frm);
var hidevalue = document.createElement('input');
hidevalue.id = 'orderId';
hidevalue.name = 'orderId';
hidevalue.type = 'hidden';
var param = {};
param['objId'] = objId;
param['objType'] = objType;
hidevalue.value = Ext.util.JSON.encode(param);
frm.appendChild(hidevalue);
Ext.fly('frmDummy').dom.submit();
document.body.removeChild(frm);
} else {
Ext.MessageBox.alert('提示','该对象尚未下发,无法导出脚本');
}
}
});
}
后台:
public String exportScriptToFile() throws Exception {
// 返回结果到前端
HashMap<String,String> reqMap = new HashMap<String,String>();
JSONObject parameter = JSONObject.fromObject(this.orderId);
reqMap.put("objId", parameter.getString("objId"));
reqMap.put("objType", parameter.getString("objType"));
//得到的文件信息
String res = getConfigWizardSMO().getExportScriptInfo(reqMap);
this.initResponseAsFile("产品脚本.txt","MIME/octet-strea",res.toString());
return null;
}
private void initResponseAsFile(String fileName, String fileType,String res)
throws Exception {
HttpServletResponse resp = this.getResponse();
String[] names = fileName.split("\\.");
String filename = "attachment; filename="+ new String(names[0].getBytes("GBK"), "ISO8859_1") + "."+ names[1];
//resp.setCharacterEncoding("charset=GBK");
// 设定输出文件头
resp.setHeader("Content-Disposition", filename);
// 定义输出类型
if (fileType.equals("excel")) {
resp.setContentType("application/vnd.ms-excel");
} else if (fileType.equals("xml")) {
resp.setContentType("application/vnd.ms-xml");
} else if (fileType.equals("txt")) {
resp.setContentType("text/plain");
} else if (fileType.equals("csv")) {
resp.setContentType("application/CSV");
} else if (fileType.equals("MIME/octet-strea")) {
resp.setContentType("text/plain");
} else {
resp.setContentType("application/vnd.ms-excel");
}
resp.getWriter().write(res);
}