<form id="tagRuleDownloadForm" action=""></form>
Js代码
//下载规则文件接口
function downloadTagRuleFile(fileName){
var url = '/tagrule?fileName=' + fileName;
Ext.Ajax.request({ //下载之前先判断一下文件存不存在;
url: url + "&checkFileExist=checkFileExist",
params: {checkFileExist: 'checkFileExist'}, //这样参数没有传过去
method: 'PUT', //这里自己改,因为需要才写成这样的!
callback: function(op, suc, resp){
alert(resp.responseText);
if(suc){ //'success':false,'message':'file not found!'
var ret = eval("(" + resp.responseText + ")");
if(ret.success){
//下载文件;
var downloadForm = document.getElementById("tagRuleDownloadForm");
downloadForm.action = url;
downloadForm.method = "POST";
downloadForm.submit();
}else{
Ext.Msg.alert(TagRulerRes.TIP, ret.message);
}
}else{
Ext.Msg.alert(TagRulerRes.TIP, TagRulerRes.DOWNLOAD_FAILURE);
}
}
});
}
//下载规则文件接口 function downloadTagRuleFile(fileName){ var url = '/tagrule?fileName=' + fileName; Ext.Ajax.request({ //下载之前先判断一下文件存不存在; url: url + "&checkFileExist=checkFileExist", params: {checkFileExist: 'checkFileExist'}, //这样参数没有传过去 method: 'PUT', //这里自己改,因为需要才写成这样的! callback: function(op, suc, resp){ alert(resp.responseText); if(suc){ //'success':false,'message':'file not found!' var ret = eval("(" + resp.responseText + ")"); if(ret.success){ //下载文件; var downloadForm = document.getElementById("tagRuleDownloadForm"); downloadForm.action = url; downloadForm.method = "POST"; downloadForm.submit(); }else{ Ext.Msg.alert(TagRulerRes.TIP, ret.message); } }else{ Ext.Msg.alert(TagRulerRes.TIP, TagRulerRes.DOWNLOAD_FAILURE); } } }); } 下的servlet方法:Java代码 /**
* 文件下载
* 查看,修改规则时下载规则文件;
*/
public void downloadFile(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
String fileName = request.getParameter("fileName");
if(fileName == null || fileName.length() == 0)
{
log.info("文件不存在! fileName is null !");
this.responseText("{'success':false,'message':'" +
ResourceCfg.getResourceMessage(String.valueOf(ErrorCode.ADFILE_NOT_EXISTS)) + "'}", response);
return ;
}
String checkFileExist = request.getParameter("checkFileExist");
String svrFilePath = (MATRIX_BAK_DIRMATRIX_BAK_DIR + fileName).replace("\\", "/");
File file = new File(svrFilePath);
if (!file.exists())
{
log.info("文件不存在! path:" + file.getAbsolutePath());
this.responseText("{'success':false,'message':'" +
ResourceCfg.getResourceMessage(String.valueOf(ErrorCode.ADFILE_NOT_EXISTS)) + "'}", response);
}
else
{
if (checkFileExist != null && checkFileExist.equals("checkFileExist"))
{
log.info("文件存在! path:" + file.getAbsolutePath());
this.responseText("{'success':true,'message':''}", response);
}
else
{
//写文件到网流;
responseFile(response, fileName, svrFilePath);
}
}
}