在JSP页面上生成TXT的问题求助!!!! 我用的是DB2数据库。查询出数据资料SELECT * FROM DB.TABLE WHERE NAME BETWEEN 1 AND 10比如这样的10笔数据,显示在一个TEXTAREA里,现在想让它生成个TXT文档供下载,请问该如何做呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把记录组合成数据流,然后filewrite到指定地方,取名,记下path和name然后提供download就可以了啊 大致上可以这样:提交到一个servlet,取TEXTAREA内容,然后下载。public class FileDownload extends HttpServlet { protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // 取文本内容 String txt = req.getParameter("txt"); res.setContentType("application/txt"); res.setHeader("Content-disposition", "attachment;filename=table.txt"); BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(res.getOutputStream()); bos.write(txt.getBytes()); } catch (IOException e) { throw e; } finally { if (bos != null) bos.close(); } }} <%@ page contentType="text/html;charset=gb2312" %><%@ page import="java.net.URLEncoder"%><%@ page import="java.util.Random"%><%@ page import="com.jspsmart.upload.*" %><%@ page import="common.Tools"%><%@ page import="java.io.*"%><% //request.setCharacterEncoding("GB2312"); //System.out.println( "contentLenth=="+(long)request.getContentLength()); SmartUpload su = new SmartUpload(); su.initialize(pageContext); String contentLink = Tools.checkNull(request.getParameter("contentLink")); //解决文件名中有空格的问题 contentLink = contentLink.replaceAll("255"," "); java.io.File file = new java.io.File(contentLink); if(file.isFile()) { Random rd = new Random(); String desFileName = rd.nextInt(10000)+contentLink.substring(contentLink.lastIndexOf(".")-1); //int index = contentLink.lastIndexOf('\\'); //String desFileName = contentLink.substring(index); su.setContentDisposition(null); System.out.println("登陆主机为:"+request.getRemoteHost()); System.out.println("下载文件为:"+contentLink); //su.downloadFile(contentLink); su.downloadFile(contentLink,"attachment",desFileName); } else if(file.isDirectory()) { String [] fileNames = file.list(); for(int i=0;i<fileNames.length;i++) { String fileRealPath = file.getPath()+"\\"+fileNames[i]; Random rd = new Random(); String desFileName = rd.nextInt()+fileRealPath.substring(fileRealPath.lastIndexOf(".")-1); su.setContentDisposition(null); System.out.println(fileRealPath); //su.downloadFile(contentLink); su.downloadFile(fileRealPath,"attachment",desFileName); } }%>这个页面是download.jsp,是我之前写的一个用smartUpload.jar的通用下载页面,直接往这个页面的contentLink参数传WEB的绝对路径文件名即可 想用ZK做一个像这个范例里的组件,大家有什么好建议吗? JMF的一些问题? struts。。在普通javabean中连接数据库 关于classpath的问题 关于link的问题? 求大神指教。为什么不能算出购物车结果 tomcat问题!!! nullpointexception是什么错误? 如何将Tomcat整合到IIS中? 菜鸟初级问题,我的tomcat端口是6060 但我的server它调试总是往1234那个端口发请求,请问怎么改成6060端口 请问,谁知道关于JAVA制图的东西吗?????????????? 迷茫~!
然后提供download就可以了啊
throws ServletException, IOException {
// 取文本内容
String txt = req.getParameter("txt");
res.setContentType("application/txt");
res.setHeader("Content-disposition", "attachment;filename=table.txt");
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(res.getOutputStream());
bos.write(txt.getBytes());
} catch (IOException e) {
throw e;
} finally {
if (bos != null)
bos.close();
}
}}
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.util.Random"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="common.Tools"%>
<%@ page import="java.io.*"%>
<%
//request.setCharacterEncoding("GB2312");
//System.out.println( "contentLenth=="+(long)request.getContentLength());
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
String contentLink = Tools.checkNull(request.getParameter("contentLink"));
//解决文件名中有空格的问题
contentLink = contentLink.replaceAll("255"," ");
java.io.File file = new java.io.File(contentLink);
if(file.isFile())
{
Random rd = new Random();
String desFileName = rd.nextInt(10000)+contentLink.substring(contentLink.lastIndexOf(".")-1);
//int index = contentLink.lastIndexOf('\\');
//String desFileName = contentLink.substring(index);
su.setContentDisposition(null);
System.out.println("登陆主机为:"+request.getRemoteHost());
System.out.println("下载文件为:"+contentLink); //su.downloadFile(contentLink);
su.downloadFile(contentLink,"attachment",desFileName);
}
else if(file.isDirectory())
{
String [] fileNames = file.list();
for(int i=0;i<fileNames.length;i++)
{
String fileRealPath = file.getPath()+"\\"+fileNames[i];
Random rd = new Random();
String desFileName = rd.nextInt()+fileRealPath.substring(fileRealPath.lastIndexOf(".")-1);
su.setContentDisposition(null);
System.out.println(fileRealPath);
//su.downloadFile(contentLink);
su.downloadFile(fileRealPath,"attachment",desFileName);
}
}%>这个页面是download.jsp,是我之前写的一个用smartUpload.jar的通用下载页面,直接往这个页面的contentLink参数传WEB的绝对路径文件名即可