• SpringMVC 文本文件下载的配置


    页面:

     <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;
        }
    }

    只是记录一下。

  • 相关阅读:
    devstack安装openstack
    Stacks of Flapjacks
    二、Reids基础命令--字符串
    数据库筛选用户,然后去掉一部分(列表求差),再随机返回一个用户。sqlalchemy + python集合(set) + random
    利用Powershell获取公司内部机器的资源信息,作为企业兴许资产管理的基本途径!
    《编程导论(Java)&#183;3.2.4 循环语句》
    Android自己主动检測版本号及自己主动升级
    基于bootstrap的富文本框——wangEditor【欢迎增加开发】
    找球号(三)
    #308 (div.2) B. Vanya and Books
  • 原文地址:https://www.cnblogs.com/heyang78/p/4128552.html
Copyright © 2020-2023  润新知