文件下载(v1.11.1)

  1. // 下载文件,可用Postman请求
  2. /*
  3. postman设置:
  4. POST URL:http://localhost:8080/api
  5. body raw application/json
  6. {
  7. "name": "download.test",
  8. "version": "",
  9. "data": "%7B%22goods_name%22%3A%22iphoneX%22%7D"
  10. }
  11. 点击sand and download
  12. */
  13. @Api(name = "download.test", ignoreValidate = true, noReturn = true)
  14. public void download(GoodsParam param) throws IOException {
  15. // 获取response
  16. HttpServletResponse response = ApiContext.getResponse();
  17. String fileName = "文件.txt";
  18. response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
  19. // 下载application.properties文件
  20. ClassPathResource resource = new ClassPathResource("application.properties");
  21. InputStream inputStream = resource.getInputStream();
  22. OutputStream outputStream = response.getOutputStream();
  23. IOUtils.copy(inputStream, outputStream);
  24. }

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