SpringMVC 文本文件下载的配置
页面:
<fieldset> <legend>Download annotator list</legend> <img src="pages/tools/listannotator/img/text.png" id="downloadAnnotatorListCsvImg"/> <img src="pages/tools/listannotator/img/xls.png"/> </fieldset>
JS代码:
$("#downloadAnnotatorListCsvImg").click( function(){ var wnd=openCenterWindow(APP_NAME+"downloadAnnotatorListCsv.html","Download Annotator List in CSV format",400,300); } ); var APP_NAME="/rttsbiz2/"; function openCenterWindow(url,windowName,width,height){ var left = (window.screen.availWidth-10-width)/2; var top = (window.screen.availHeight-30-height)/2; var wnd=window.open(url,windowName,"height="+height+",width="+width+",top="+top+",left="+left+",resizable=yes,scrollbars=yes,status=no,location=no,"); return wnd; }
Controller代码:
@RequestMapping("/downloadAnnotatorListCsv") public ModelAndView download(HttpServletRequest request,HttpServletResponse response){ String fileName="download-single.txt"; response.reset();// 不加这一句的话会出现下载错误 response.setHeader("Content-disposition", "attachment; filename="+fileName);// 设定输出文件头 response.setContentType("text/x-plain");// 定义输出类型 try { ServletOutputStream out = response.getOutputStream(); String path = System.getProperty("java.io.tmpdir") + "\\poem.txt"; File file = new File(path); FileOutputStream fos = new FileOutputStream(file); Writer writer = new OutputStreamWriter(fos, "utf-8"); String text="Hello!download!"; writer.write(text); writer.close(); fos.close(); FileInputStream fis = new java.io.FileInputStream(file); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(4096); byte[] cache = new byte[4096]; for (int offset = fis.read(cache); offset != -1; offset = fis.read(cache)) { byteOutputStream.write(cache, 0, offset); } byte[] bt = null; bt = byteOutputStream.toByteArray(); out.write(bt); out.flush(); out.close(); fis.close(); if(file.exists()){ file.delete(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
只是记录一下。
SpringMVC 文本文件下载的配置的更多相关文章
- JavaEE开发之SpringMVC中的路由配置及参数传递详解
在之前我们使用Swift的Perfect框架来开发服务端程序时,聊到了Perfect中的路由配置.而在SpringMVC中的路由配置与其也是大同小异的.说到路由,其实就是将URL映射到Java的具体类 ...
- 基于 Nginx XSendfile + SpringMVC 进行文件下载
转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...
- SpringMVC实现文件下载的两种方式及多文件下载
1.传统方法 @RequestMapping("/download") public String download( String fileName ,String filePa ...
- (转)springMVC+mybatis+ehcache详细配置
一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...
- 富文本编辑器kindeditor配置
<!--富文本编辑器kindeditor配置↓ --> <link type="text/css" rel="stylesheet" href ...
- 在线富文本编辑器FckEditor配置(.Net Framework 3.5)
进入FCKeditor文件夹,编辑 fckconfig.js 文件.1.上传设置 . var _FileBrowserLanguage = 'php' ; // a ...
- MVC5富文本编辑器CKEditor配置CKFinder
富文本编辑器CKEditor的使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- Spring-MVC开发步骤(入门配置)
Spring-MVC开发步骤(入门配置) Step1.导包 spring-webmvc Step2.添加spring配置文件 Step3.配置DispatcherServlet 在web.xml中: ...
- 1.Maven+SpringMVC+Eclipse软件安装配置,Maven报插件错误,Eclipse总是卡死的解决办法,导入一个maven工程后 一直显示importing maven project
使用Maven+SpringMVC+Eclipse软件安装配置过程中的问题: 1.Eclipse总是卡死的解决办法: 一:内存不足所以会卡死,配置一下eclipse.ini修改这几个值就好了-X ...
随机推荐
- bzoj 4443 [Scoi2015]小凸玩矩阵 网络流,二分
[Scoi2015]小凸玩矩阵 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1564 Solved: 734[Submit][Status][Di ...
- webpack最佳入门实践系列(6)
10.css模块化 10.1.什么是css模块? CSS模块就是所有的类名都只有局部作用域的CSS文件,当你在一个JavaScript模块中导入一个CSS文件时,CSS模块将会定义一个对象,将文件中类 ...
- HDU1251统计难题---Trie Tree
map巧过 #include <stdio.h> #include <string.h> #include <map> #include <string> ...
- bzoj3643 Phi的反函数
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3643 [题解] n = p1^a1*p2^a2*...*pm^am phi(n) = p1( ...
- switch与if 性能测试
测试结果:switch性能更高. 测试过程:新建一个Win32 Console Application, 在cpp文件中添加下面代码 #include "stdafx.h" #in ...
- VS2013 Sqlite3 操作指令
extern "C"{ #include "sqlite3.h" }; #pragma comment(lib,"sqlite.lib") ...
- MFC 实现打印机打印功能
Visual C++6.0是开发Windows应用程序的强大工具,但是要通过它实现程序的打印功能,一直是初学者的一个难点,经常有朋友询问如何在VC中实现打印功能,他们往往感到在MFC提供的框架内实现这 ...
- linux知识点小结
PATH环境变量,记录了所有可以直接执行的二进制命令的原件或者链接 harvey@ubuntu:/etc$ echo $PATH /usr/lib/lightdm/lightdm:/usr/local ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】
B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...
- 代码编辑器[0] -> Vim/gVim[0] -> 基于 Python 的 gVim 环境配置(Windows)
环境配置 / Environment Setup 基于Python开发的 gVim 环境配置(Windows) 使用方式参考 Vim 的使用. 1 基于vundle进行配置 Vim有多个扩展管理器, ...