页面:

 <fieldset>
     <legend>Download annotator list</legend>
     <img src="pages/tools/listannotator/img/text.png" id="downloadAnnotatorListCsvImg"/>&nbsp;
     <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 文本文件下载的配置的更多相关文章

  1. JavaEE开发之SpringMVC中的路由配置及参数传递详解

    在之前我们使用Swift的Perfect框架来开发服务端程序时,聊到了Perfect中的路由配置.而在SpringMVC中的路由配置与其也是大同小异的.说到路由,其实就是将URL映射到Java的具体类 ...

  2. 基于 Nginx XSendfile + SpringMVC 进行文件下载

    转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...

  3. SpringMVC实现文件下载的两种方式及多文件下载

    1.传统方法 @RequestMapping("/download") public String download( String fileName ,String filePa ...

  4. (转)springMVC+mybatis+ehcache详细配置

    一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...

  5. 富文本编辑器kindeditor配置

    <!--富文本编辑器kindeditor配置↓ --> <link type="text/css" rel="stylesheet" href ...

  6. 在线富文本编辑器FckEditor配置(.Net Framework 3.5)

    进入FCKeditor文件夹,编辑 fckconfig.js 文件.1.上传设置  .  var _FileBrowserLanguage         = 'php' ;         // a ...

  7. MVC5富文本编辑器CKEditor配置CKFinder

    富文本编辑器CKEditor的使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  8. Spring-MVC开发步骤(入门配置)

    Spring-MVC开发步骤(入门配置) Step1.导包 spring-webmvc Step2.添加spring配置文件 Step3.配置DispatcherServlet 在web.xml中: ...

  9. 1.Maven+SpringMVC+Eclipse软件安装配置,Maven报插件错误,Eclipse总是卡死的解决办法,导入一个maven工程后 一直显示importing maven project

     使用Maven+SpringMVC+Eclipse软件安装配置过程中的问题: 1.Eclipse总是卡死的解决办法: 一:内存不足所以会卡死,配置一下eclipse.ini修改这几个值就好了-X ...

随机推荐

  1. 平面ray trace的数据结构加速

    yy了一个数据结构.. 首先考虑到,平面ray trace对应的scene是planar graph with coordinates,特点是除端点外无相交. 我们考虑对所有端点建立kd-tree,注 ...

  2. 转:Android Log

    在调试代码的时候我们需要查看调试信息,那我们就需要用Android Log类. android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以 ...

  3. js,将日期时分秒等格式化和转化

    1.将js Date对象格式化为指定格式,添加一个原型方法 /** * 返回指定format的string * format eg:'yyyy-MM-dd hh:mm:ss' **/ Date.pro ...

  4. linux mint 自动挂载windows的D盘和E盘

    终端敲udisksctl mount -p block_devices/sda后双击tab键补全分区,如下:    如我的E盘是sda6,执行     udisksctl mount -p block ...

  5. HDU5037 Frog

    Once upon a time, there is a little frog called Matt. One day, he came to a river. The river could b ...

  6. strtol函數的用法 atof, atoi, atol, strtod, strtoul

    相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...

  7. VS2013 MFC C++ CString ,const char , char, string 类型转换

    VS2013 测试 以下测试加入头文件: # include <string>#include <cstdlib>using namespace std; //-------- ...

  8. Kotlin来了

    kotlin优点(主要是代码简洁度) 1.避免空指针异常,其次在一个文件中java方法必须写在class里面,而kotlin则没要求 2.在同一个文件中,有class a则不允许在class外的方法( ...

  9. 开始gentoo之旅

    买了一台i7笔记本,dell 15游戏匣7559,老笔记本dell n4030装了gentoo. 用了两天,先用着,再慢慢学内核编译.

  10. Codeforces Round #445 B. Vlad and Cafes【时间轴】

    B. Vlad and Cafes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...