extjs代码:
{
text : '上传',
type : 'submit',
handler : function() {
var fileform =this.ownerCt.ownerCt;
var furl=fileform.form.findField('upload').getValue();// 文件物理地址
var sysCd = fileform.form.findField('sysCd');
var gridArray = new Array();
var isCheckSys = false;
for(var i = 0; i<sysCd.items.length; i++) {
if (sysCd.items.get(i).getValue() == true) {
isCheckSys = true;
var data = {};
data['sysCd'] = Ext.isDefined(sysCd.items.get(i).sysCd)?sysCd.items.get(i).sysCd :'';
gridArray[gridArray.length] = data;
}
}
if (isCheckSys) {
//把选择的系统赋值到orderId表单中,action层读取
fileform.form.findField('orderId').setValue(Ext.util.JSON.encode(gridArray));
var type=furl.substring(furl.length-3).toLowerCase();
if (furl==""||furl==null) {return;}
if(type!='txt'){
Ext.MessageBox.alert('警告','仅支持txt格式的文件!');
return;
}
fileform.form.submit({
url : '../file/uploadScriptFile.action',// 请求的url地址
method : 'POST',
waitMsg : '正在上传......',
waitTitle : '请等待',
success : function(form, action) {
var res = eval('(' + action.response.responseText+ ')');
if (res.success) {
Ext.MessageBox.alert('提示','上传成功,脚本执行成功!');
} else {
Ext.MessageBox.alert('警告','上传失败!原因:'+res.msgInfo);
}
},
failure : function(form, action) {
fileform.form.reset();
var res = eval('(' + action.response.responseText+ ')');
if (action.failureType == Ext.form.Action.SERVER_INVALID)
Ext.MessageBox.alert('警告','上传失败!原因:'+res.msgInfo);
}
});
} else {
Ext.MessageBox.alert('提示','至少选择一个下发系统!');
return false;
}
}
}
后台代码:
public String uploadScriptFile() {
// 返回结果到前端
JSONObject res = new JSONObject();
// 初始值
res.put("success", false);
try {
// 字符编码(可解决中文乱码问题 )
String encoding = "UTF-8";
String oldFileName = uploadFileName.get(0);
String newFileName = getUUIDName(oldFileName);
JSONArray sysCds = JSONArray.fromObject(this.orderId);
//Linux下路径
FileUtils.copyFile(upload.get(0),new File(PPMDomain.FILE_UPLOADDOWNLOAD_PATH_FOR_LINUX + newFileName));
File file = new File(PPMDomain.FILE_UPLOADDOWNLOAD_PATH_FOR_LINUX + newFileName);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);
List<String> scripts = new ArrayList<String>();
try {
BufferedReader bufferedReader = new BufferedReader(read);
String lineSql = null;
while ((lineSql = bufferedReader.readLine()) != null) {
scripts.add(lineSql.toString().trim());
}
res.put("success", true);
} finally {
read.close();
}
//执行发布脚本
executePublicScriptToDescSys(sysCds,scripts);
}
} catch (Exception e) {
res.put("success", false);
res.put("msgInfo", e.toString());
}finally{
try {
this.writeToResponse(res.toString());
} catch (Exception e) {
res.put("success", false);
res.put("msgInfo", e.toString());
}
}
return null;
}