extjs+spring 下載文件

1.客戶端:

var realTimeUserChartPanel = Ext.create('Ext.panel.Panel', {

    title: '在線用戶圖表1',

    width: 200,

    items:[{

    xtype:'button',

    text: 'download',

    href:"realTime/download",

    hrefTarget:"_self"

    }]

});

2.服務器:

@RequestMapping(value = "/download")  

    public void downloadFile(HttpServletResponse response){

String fileName = "/Users/lixin/tmp/result.txt";

        response.setCharacterEncoding("utf-8");  

        response.setContentType("multipart/form-data");  

  

        response.setHeader("Content-Disposition", "attachment;fileName="+fileName);  

        try {  

            File file=new File(fileName);  

            System.out.println(file.getAbsolutePath());  

            InputStream inputStream=new FileInputStream(file);  

            OutputStream os=response.getOutputStream();  

            byte[] b=new byte[1024];  

            int length;  

            while((length=inputStream.read(b))>0){  

                os.write(b,0,length);  

            }  

            inputStream.close();  

        } catch (FileNotFoundException e) {  

            e.printStackTrace();  

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章