下载功能

版权声明:版权声明:本文为博主原创文章,未经允许请勿转载,谢谢 https://blog.csdn.net/a972669015/article/details/86625762

下载保存图片

图片默认情况下, 浏览器进行内联(inline)显示, 如果需要实现下载功能, 则需要设置一个响应头 Content-Disposition

具体请参看: rfc2616的 19.5.1 章节

下载图片的原理是:

Servlet代码:

@RequestMapping(value="/download.do",
        produces="image/png")
@ResponseBody
public byte[] download(
        HttpServletResponse response)
    throws IOException {
    String file = URLEncoder.encode(
            "实例1.png", "UTF-8");
    response.setHeader(
            "Content-Disposition", 
            "attachment; filename=\""
            +file+"\"");
    byte[] bytes=readFile("D:/1.png");
    return bytes;
}

下载Excel

  1. 导入Excel API

    <!-- POI 是Apache组织提供的开源 Office API -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>
    
  2. 编写控制器

    @RequestMapping(value="/excel.do",
            produces="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    @ResponseBody
    public byte[] excel(
            HttpServletResponse response)
        throws IOException {
        String file = URLEncoder.encode(
                "实例1.xlsx", "UTF-8");
        response.setHeader(
                "Content-Disposition", 
                "attachment; filename=\""
                +file+"\"");
        byte[] bytes=createExcel();
        return bytes;
    }
    
    private byte[] createExcel()
        throws IOException{
        //创建工作簿(就是Excel文件)
        XSSFWorkbook workbook =
                new XSSFWorkbook();
        //在工作簿中创建工作表
        XSSFSheet sheet=
                workbook.createSheet("演示");
        //在工作表中创建行, 参数是行号 0 1 2 3...
        XSSFRow row = sheet.createRow(0);
        //在行中创建格子, 参数列号 0 1 2 3...
        XSSFCell cell=row.createCell(0);
        //在格子中添加数据
        cell.setCellValue("Hello  World!");
        ByteArrayOutputStream out=
                new ByteArrayOutputStream();
        workbook.write(out);
        workbook.close();
        out.close();
        byte[] bytes=out.toByteArray();
        return bytes;
    }
    
  3. 编写客户端:

    <h2>下载</h2>
    <p>
        <a href="download.do">下载图片</a> 
        <a href="excel.do">下载Excel</a> 
    </p>

添加用户列表导出功能

  1. 编写控制器:

    @RequestMapping(value="/export.do",
            produces="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    @ResponseBody
    public byte[] export(
            HttpServletResponse response)
        throws IOException {
        String file = URLEncoder.encode(
                "用户列表.xlsx", "UTF-8");
        response.setHeader(
                "Content-Disposition", 
                "attachment; filename=\""
                +file+"\"");
        byte[] bytes=usersExcel();
        return bytes;
    }
    
    private byte[] usersExcel() 
        throws IOException {
        XSSFWorkbook workbook = 
                new XSSFWorkbook();
        XSSFSheet sheet = 
                workbook.createSheet("用户列表");
        //创建表头
        XSSFRow header=sheet.createRow(0);
        header.createCell(0).setCellValue("编号");
        header.createCell(1).setCellValue("姓名");
        header.createCell(2).setCellValue("电话");
        header.createCell(3).setCellValue("邮箱");
        //将用户信息填充到excel表格中
        List<User> list=userDAO.findAll();
        int n = 1;
        for(User user:list){
            XSSFRow row=sheet.createRow(n++);
            row.createCell(0).setCellValue(
                    user.getId());
            row.createCell(1).setCellValue(
                    user.getUsername());
            row.createCell(2).setCellValue(
                    user.getPhone());
            row.createCell(3).setCellValue(
                    user.getEmail());
        }
        //将Excel数据保存到 byte[] 数组中
        ByteArrayOutputStream out=
                new ByteArrayOutputStream();
        workbook.write(out);
        workbook.close();
        out.close();
        byte[] bytes=out.toByteArray();
        return bytes; 
    }
    

猜你喜欢

转载自blog.csdn.net/a972669015/article/details/86625762