public void exportTxt(HttpServletResponse response,String text){
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain");
response.addHeader("Content-Disposition","attachment;filename=" + genAttachmentFileName( "文件名称", "JSON_FOR_UCC_")
+ ".txt");
BufferedOutputStream buff = null;
ServletOutputStream outStr = null;
try {
outStr = response.getOutputStream();
buff = new BufferedOutputStream(outStr);
buff.write(text.getBytes("UTF-8"));
buff.flush();
buff.close();
} catch (Exception e) {
} finally {
try {
buff.close();
outStr.close();
} catch (Exception e) {
}
}
}
public String genAttachmentFileName(String cnName, String defaultName) {
try {
cnName = new String(cnName.getBytes("gb2312"), "ISO8859-1");
} catch (Exception e) {
cnName = defaultName;
}
return cnName;
}
转自https://blog.csdn.net/i5200369/article/details/80757509