点击按钮下载文件

 
@RequestMapping("/download.do")
    public void download(HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        String filePath = "文件路径";
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            fis = new FileInputStream(filePath);
            os = response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            response.setHeader("Content-disposition", "attachment; filename="
                    + filePath.substring(filePath.lastIndexOf("/") + 1));// 设定输出文件头
            response.setContentType("application/x-download");
            byte[] mybyte = new byte[8192];
            int len = 0;
            while ((len = fis.read(mybyte)) != -1) {
                os.write(mybyte, 0, len);
            }
            os.close();
        } catch (IOException e) {
            throw e;
        }
    }
posted @ 2015-08-31 15:34  ordinary01  阅读(322)  评论(0编辑  收藏  举报