项目中需要导出一个txt文件
jsp中是这样
<%ImportTxt imt = new ImportTxt();
String filename = imt.buildFileName("","1234",FileType.TXT);
String res = imt.getSQL("eee","fdsaf");
System.out.print(res);
imt.importTxt(response,res,filename);
%>
ImportTxt.java
/**
* 在客户端生成文件。
*
* @param response
* @param content
* @param filename
* @return boolean
*/
public boolean importTxt(HttpServletResponse response,String content,String filename) {
boolean res = true ;
response.addHeader("Content-Disposition",
"attachment; filename=" + filename);
response.setContentType("application/octet-stream");
try {
response.getWriter().write(content);
response.flushBuffer();
} catch (IOException e) {
res = false ;
e.printStackTrace();
}
return res ;}
生成一个
<html>
<head>
<base href="http://172.19.74.100:7001/WebRoot/">
<title>My JSP 'improtTxt.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
</body>
</html>
文件,我的目的是生成一个<body>之间的内容就OK了!
哪位点一下!!!