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;
    }
}
复制代码

只是记录一下。

posted @   逆火狂飙  阅读(12407)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2013-11-28 利用有道翻译实现英汉互译
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示