/** * 下载 * @return */ def download(){ def fileObj = ValuationHistoryForNSQ.get(params["id"]) def file_name = fileObj.getFile_name() def file_type = fileObj.getFile_type() def root_path = fileObj.getRoot_path() def file_path = fileObj.getFile_path() def downloadPath = root_path+file_path+file_name def bis = new BufferedInputStream(new FileInputStream(downloadPath)) def bos = new BufferedOutputStream(response.outputStream) long fileLength = new File(downloadPath).length() response.setCharacterEncoding("UTF-8") response.setContentType("multipart/form-data") String userAgent = request.getHeader("User-Agent").toLowerCase() if( userAgent.contains("msie") || userAgent.contains("like gecko") ){ // win10 ie edge 浏览器 和其他系统的ie file_name = URLEncoder.encode(file_name, "UTF-8") }else { file_name = new String(file_name.getBytes("UTF-8"), "iso-8859-1") } response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", file_name)) response.setHeader("Content-Length", String.valueOf(fileLength)) byte[] buff = new byte[2048] int bytesRead while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead) } bis.close() bos.close() }
/** * 查看图片 * @return */ def look(){ def fileObj = ValuationHistoryForNSQ.get(params["id"]) def file_name = fileObj.getFile_name() def file_type = fileObj.getFile_type() def root_path = fileObj.getRoot_path() def file_path = fileObj.getFile_path() def downloadPath = root_path+file_path+file_name //读取本地图片输入流 FileInputStream inputStream = new FileInputStream(downloadPath); int i = inputStream.available(); //byte数组用于存放图片字节数据 byte[] buff = new byte[i]; inputStream.read(buff); //记得关闭输入流 inputStream.close(); //设置发送到客户端的响应内容类型 response.setContentType("image/*"); OutputStream out = response.getOutputStream(); out.write(buff); //关闭响应输出流 out.close(); }
查看图片前台方法:
<a href="javascript:void(0)" class="btn btn-xs btn-primary btn-outline" onclick="look(${it?.id})">查看</a>
function look(id) { var srcPath = '/valuationHistoryForNSQ/look?id='+id var imgPath = '<img src="'+srcPath+'" width="800px" height="500px">' layer.open({ type: 1 //Page层类型 ,area: ['800px', '542px'] ,title: '查看' ,scrollbar: false //屏蔽浏览器滚动条 ,shade: 0.6 //遮罩透明度 ,maxmin: false //允许全屏最小化 ,anim: 5 //0-6的动画形式,-1不开启 ,content: imgPath }); }
grails文件下载,注意中文乱码问题,我的文件路径、文件名称是从数据库查出来的。
我用的win10系统,值得注意的是win10 ie和edge与其他浏览器的区别
相关推荐
The+Definitive+Guide+to+Grails+Second+Edition
grails 中文文档+grails-fckeditor-0.9.5.zip插件
Grails参考手册 Grails学习资料 groovy 小白学习grails+groovy
相当不错的入门级书,适合刚刚接触grails的新手学习用。附带书中提到相当的源码,每个阶段的都有。
本教程介绍 Grails,这是一个搭建在动态语言 Groovy 之上的开源 MVC 快速 Web 开发框架。使用 Grails 可以提高 Web 开发的效率,降低 Web 开发的复杂度。本文 从 Grails 自动生成代码入手,以示例为中心逐步增加深度...
grails+Xfire webservice
Grails+ZK文档,Grails+ZK文档,Grails+ZK文档,Grails+ZK文档
grails中的上传,下载,需要的请下载
Grails入门指南书籍和源码----下载不扣分,回帖加1分,欢迎下载,童叟无欺 getting started with grails chinese Grails入门指南书籍和源码----下载不扣分,回帖加1分,欢迎下载,童叟无欺 getting started with ...
grails文件 很好的资源啊
grails 用户登录的例子,必须登录后才可以查看信息,并且只有admin用户才可以创建用户,此例子,只要将grails 的环境配好,就可以用了,用的是自带的数据库,如果需要mysql 可以更改。
grails3.3下载,grails-3.3.0.zip
Grails Grails Grails Grails Grails
grails3.2.8 part1
grails-2
Grails1.1中文文档 目前国内的grails资料较少,这个帮助文档很有作用
Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南
该资源在官方的网站上下载速度太慢,现在放在这里共各位下载。
grails-5.1.2.zip
详情地主:https://blog.csdn.net/qq_38425662/article/details/82703128