我用的是DB2数据库。查询出数据资料SELECT * FROM DB.TABLE WHERE NAME BETWEEN 1 AND 10比如这样的10笔数据,显示在一个TEXTAREA里,现在想让它生成个TXT文档供下载,请问该如何做呢?

解决方案 »

  1.   

    把记录组合成数据流,然后filewrite到指定地方,取名,记下path和name
    然后提供download就可以了啊
      

  2.   

    大致上可以这样:提交到一个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();
        }
      }}
      

  3.   


    <%@ 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的绝对路径文件名即可