网页下载word与Excel代码

本文提供了一个简单的示例,展示如何通过Java代码实现从服务器下载指定的文件,包括文档模板和Excel模板。具体步骤包括获取输入流、读取文件内容、设置HTTP响应头,并返回包含文件数据的响应实体。
/**
     * 下载用户信息填写模板使用说明
     * @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;
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值