当前位置: 首页 > 文档资料 > easyopen 中文文档 >

文件下载(v1.11.1)

优质
小牛编辑
144浏览
2023-12-01
// 下载文件,可用Postman请求
    /*
        postman设置:
        POST URL:http://localhost:8080/api
        body raw application/json
        {
            "name": "download.test",
            "version": "",
            "data": "%7B%22goods_name%22%3A%22iphoneX%22%7D"
        }

        点击sand and download
      */
    @Api(name = "download.test", ignoreValidate = true, noReturn = true)
    public void download(GoodsParam param) throws IOException {
        // 获取response
        HttpServletResponse response = ApiContext.getResponse();
        String fileName = "文件.txt";
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));

        // 下载application.properties文件
        ClassPathResource resource = new ClassPathResource("application.properties");
        InputStream inputStream = resource.getInputStream();
        OutputStream outputStream = response.getOutputStream();
        IOUtils.copy(inputStream, outputStream);
    }

注意设置noReturn属性为true,表示不需要返回。