网页下载word与Excel代码

/**
     * 下载用户信息填写模板使用说明
     * @param httpServletResponse response对象
     * @return
     * @throws Exception
     */
    @RequestMapping("/downloadReadFile")
    public ResponseEntity<byte[]> downloadReadFile(HttpServletResponse httpServletResponse) throws Exception {
        byte [] body = null;
        //获得输入流
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("excel/说明.docx");
        body = new byte[in.available()];
        in.read(body);
        
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=" + new String("说明.docx".getBytes(), "ISO-8859-1"));
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> response =  new ResponseEntity<byte[]>(body, headers, statusCode);
        return response;
    }
    
    /**
     * 下载用户信息填写模板
     * @param httpServletResponse response对象
     * @return
     * @throws Exception
     */
    @RequestMapping("/downloadExcel")
    public ResponseEntity<byte[]> downloadExcel(HttpServletResponse httpServletResponse) throws Exception {
        byte [] body = null;
        //获得输入流
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("excel/用户模板.xls");
        body = new byte[in.available()];
        in.read(body);
        
        HttpHeaders headers = new HttpHeaders();
       headers.add("Content-Disposition", "attachment;filename=" + new String("用户模板.xls".getBytes(), "ISO-8859-1"));
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> response =  new ResponseEntity<byte[]>(body, headers, statusCode);
        return response;
    }

猜你喜欢

转载自blog.csdn.net/zhang137107/article/details/79454403