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 ...
随机推荐
- College student reflects on getting started in open source(一)
I just completed the first semester of my second year in college, and I'm reflecting on what I learn ...
- 学习C++ -> 向量(vector)
一.向量的介绍 向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器. 与string相同, vector 同属于STL(Standard Template Lib ...
- linux低权限执行高权限
1.关于sudo不需要输密码,低权限执行高权限,在root下的命令visudo放开%wheel ALL:保存退出, 执行gpasswd -a yourusername wheel 2.脚本命令下的,权 ...
- 51Nod 1317 相似字符串对
题目链接 分析: 考虑两个串的关系:$A+C=C+B$,我们观察可以发现,$A$和$B$是循环同构的,如果$A=G+H$,那么$B=H+G$,证明略长懒得写了... 我们知道$A$串有$K^N$种,所 ...
- android 研究的环境搭建、高效工具、网站资源
================= 2015 年 10 月 14 号 更新 著名的android开源社区xda有一个帖子,详细描述了android开发和研究环境的初始搭建过程: http://for ...
- 使用clamav查杀病毒
cd ~ wget http://www.zlib.net/fossils/zlib-1.2.8.tar.gz .tar.gz cd zlib- ./configure --prefix=/usr/l ...
- (4) python--seaborn
seaborn封装了matplotlib的一些风格,简单的介绍一下
- J.U.C并发框架源码阅读(七)CyclicBarrier
基于版本jdk1.7.0_80 java.util.concurrent.CyclicBarrier 代码如下 /* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is ...
- SpringMVC + Hibernate + MySQL 的简易网页搭建(Control实现篇)
在完成Dao层的实现之后,接下来我们需要继续开发我们网页的Control层以及View层. 从开发网页的角度来说: 1. Control层之下的Dao层和Service层可以看做是一个网页的底层负责与 ...
- CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...