服务端
@GetMapping("/export")
public void exportFile(MarkText markText, HttpServletResponse response) throws UnsupportedEncodingException {
try (BufferedOutputStream buff = new BufferedOutputStream(response.getOutputStream())) {
//校验
if (StringUtils.isBlank(markText.getFileName())) {
return;
}
//查询数据
List<MarkText> list = markTextService.exportFile(markText);
/*
* 导出
*/
response.setContentType("text/plain");
String fileName = markText.getFileName();
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
StringBuilder write = new StringBuilder();
String enter = "\r\n";
String tab = "\t";
// 把内容写入文件
if (list.size() > 0) {
for (MarkText text : list) {
write.append(text.getOrginText()).append(tab)
.append(text.getLabeledText()).append(tab)
.append(text.getSequencedText()).append(enter);
}
}
buff.write(write.toString().getBytes(StandardCharsets.UTF_8));
buff.flush();
} catch (Exception e) {
log.error(e.toString(), e);
}
}
web端
vue elementUI
exportFile(fileName) {
const url = '/similar/export?fileName=' + fileName
const a = document.createElement('a')
a.download = 'test'
a.href = url
a.click()
}