java下载txt文本文件
public class TextDownloader {
private Logger logger = Logger.getLogger(TextDownloader.class);
public void exportTxt(String fileName, HttpServletResponse response, String text) {
logger.info("导出中.............");
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain");
response.addHeader("Content-Disposition", "attachment;filename="
+ genAttachmentFileName(fileName, "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) {
}
logger.info("导出结束.............");
}
}
public String genAttachmentFileName(String cnName, String defaultName) {
try {
cnName = new String(cnName.getBytes("gb2312"), "ISO8859-1");
} catch (Exception e) {
cnName = defaultName;
}
return cnName;
}
}