温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Java实现文件点击没反应的方法

发布时间:2021-04-15 12:26:17 来源:亿速云 阅读:180 作者:小新 栏目:编程语言

这篇文章主要介绍使用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实现文件点击没反应的方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI