java 无反应_Java实现文件点击没反应的方法

jsp页面链接,点击访问action用IO流去下载服务器上的文件,问题是任凭怎么点击都没反应,日志也不报错。

前台ajax代码

Ext.Ajax.request({

url : '/yjy/training/TrainingTimeAction.do?method=downLoadAttchById',

params : {

timeId : timeids

},

success : function(response,options){

var result = Ext.util.JSON.decode(response.responseText);

Ext.Msg.alert("下载成功");

},

failure :function(response,options){

var result = Ext.util.JSON.decode(response.responseText);

Ext.Msg.alert("下载失败"+result.message);

}

});

后台action代码

String timeId = request.getParameter("timeId");

String sql = "select doc_name from CPER.EHRTRAIN_item_DOCUMENT where item_id = ?";

DbHelper dbHelper = new DbHelper();

Object[] params = new Object[]{timeId};

String fileName = (String)dbHelper.runSQLScalar(sql, params);

String filePath = ServerPathUtil.getPathRoot()+"WEB-INF/cache/train_item_file/train_item_file_"+timeId+"/"+fileName;

File file = new File(filePath);

if(!file.exists()){

logger.debug("文件不存在");

throw new IOException("the file not exists");

}

response.setContentLength((int) file.length());

OutputStream o = response.getOutputStream();

byte b[] = new byte[5000];

//response.setContentType("application/x-msdownload");

response.setContentType("application/vnd.ms-excel");

response.setContentLength((int)file.length());

response.setHeader("Content-Disposition","attachment; filename="+fileName);

FileInputStream in = new FileInputStream(file);

int n;

while ((n = in.read(b)) != -1) {

o.write(b, 0, n);

}

in.close();

}catch(Exception e){

e.printStackTrace();

}

解决方法:文件的下载,在前台请求的时候,只能是form表单请求,或者用window.open的方式,最后我采用了window.open的方式

window.open('/yjy/training/TrainingTimeAction.do?method=downLoadAttchById&timeId=' + timeids);

注:采用这种方式页面会弹出一个空白窗口,下载之后窗口自动关闭,如果不想显示这个窗口,使用form提交的方式

总结

以上所述是小编给大家介绍的Java实现文件点击没反应的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值