【SSM 下载】下载文件

你离开我真会死。 提交于 2020-02-03 21:25:32

111111111

    /**
     * 导出客户数据
     */
    @RequestMapping("exportCustomer")
    public ResponseEntity<Object> exportCustomer(CustomerVo customerVo, HttpServletResponse response) {
        List<Customer> customers = customerService.queryAllCustomerForList(customerVo);
        String fileName = "客户数据.xls";
        String sheetName = "客户数据";
        org.apache.commons.io.output.ByteArrayOutputStream bos = ExprotCustomerUtils.exportCustomer(customers, sheetName);

        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");//处理文件名乱码
            //创建封装响应头信息的对象
            HttpHeaders header = new HttpHeaders();
            //封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
            header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置下载的文件的名称
            header.setContentDispositionFormData("attachment", fileName);
            return new ResponseEntity<Object>(bos.toByteArray(), header, HttpStatus.CREATED);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

1111

    <!--POI excel导出-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.1.0</version>
    </dependency>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!