服务端

  1. @GetMapping("/export")
  2. public void exportFile(MarkText markText, HttpServletResponse response) throws UnsupportedEncodingException {
  3. try (BufferedOutputStream buff = new BufferedOutputStream(response.getOutputStream())) {
  4. //校验
  5. if (StringUtils.isBlank(markText.getFileName())) {
  6. return;
  7. }
  8. //查询数据
  9. List<MarkText> list = markTextService.exportFile(markText);
  10. /*
  11. * 导出
  12. */
  13. response.setContentType("text/plain");
  14. String fileName = markText.getFileName();
  15. response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
  16. StringBuilder write = new StringBuilder();
  17. String enter = "\r\n";
  18. String tab = "\t";
  19. // 把内容写入文件
  20. if (list.size() > 0) {
  21. for (MarkText text : list) {
  22. write.append(text.getOrginText()).append(tab)
  23. .append(text.getLabeledText()).append(tab)
  24. .append(text.getSequencedText()).append(enter);
  25. }
  26. }
  27. buff.write(write.toString().getBytes(StandardCharsets.UTF_8));
  28. buff.flush();
  29. } catch (Exception e) {
  30. log.error(e.toString(), e);
  31. }
  32. }

web端

vue elementUI

  1. exportFile(fileName) {
  2. const url = '/similar/export?fileName=' + fileName
  3. const a = document.createElement('a')
  4. a.download = 'test'
  5. a.href = url
  6. a.click()
  7. }