struts 数据从数据库获得保存、下载在用户机、客户端上

开始打算用的SWT,无奈SWT还要装第三方软件,所以就改了一下,现在的原理是,点了保存按钮后,先把数据写到服务器的硬盘上,保存成一个文件,保存完之后出现下载对话框,把刚才保存在服务器上的文件下载到客户端上,虽然很麻烦很牵强,但是暂时也只能用这个办法了,下面贴出保存在服务器跟下载到客户端的代码

保存和下载都没有用第三方jar包,这样比较好移植

保存功能

// 测试
 public boolean sqlDriverQuerySaveFileSqlBeanPageCeShi(HttpServletResponse response,String sessionDriverQueryNameInput,String sessionDriverQueryIdentityCardNumberInput,String sessionDriverQueryRecordNumberInput,String sessionDriverQueryAccumulativeTotalIntegralInput,String sessionDriverQueryPermitDriverModelInput,String sessionDriverQueryManageDepartmentInput,Object sessionPageStartRow,Object sessionPageEndRow,String sessionUserName) throws Exception
 {
  boolean sqlDriverQuerySaveFileSqlError = false ;
  ResultSet rs = null;

  String xm ;
  String sfzmhm ;
  String dabh ;
  String ljjf ;
  String zjcx ;
  String glbm ;

  if(sessionDriverQueryNameInput!=null)
  {
   xm = "and p.xm ="+"'"+sessionDriverQueryNameInput+"'"  ;
  }
  else
  {
   xm = "" ;
  }
  if(sessionDriverQueryIdentityCardNumberInput!=null)
  {
   sfzmhm = "and d.sfzmhm ="+"'"+sessionDriverQueryIdentityCardNumberInput+"'" ;
  }
  else
  {
   sfzmhm = "" ;
  }
  if(sessionDriverQueryRecordNumberInput!=null)
  {
   dabh = "and d.dabh ="+"'"+sessionDriverQueryRecordNumberInput+"'" ;
  }
  else
  {
   dabh = "" ;
  }
  
  if(sessionDriverQueryAccumulativeTotalIntegralInput!=null)
  {
   ljjf = "and d.ljjf ="+"'"+sessionDriverQueryAccumulativeTotalIntegralInput+"'" ;
  }
  else
  {
   ljjf = "" ;
  }
  if(sessionDriverQueryPermitDriverModelInput!=null)
  {
   zjcx = "and d.zjcx ="+"'"+sessionDriverQueryPermitDriverModelInput+"'" ;
  }
  else
  {
   zjcx = "" ;
  }
  
  if(sessionDriverQueryManageDepartmentInput!=null)
  {
   glbm = "and d.glbm ="+"'"+sessionDriverQueryManageDepartmentInput+"'" ;
  }
  else
  {
   glbm = "" ;
  }
  try 
  {
          final String SQL = "select p.xm,d.sfzmhm,p.djzsxxdz,d.dabh,d.zjcx,d.yxqs,d.yxqz,d.ljjf,d.glbm from drv_admin.drivinglicense d ,drv_admin.person p where p.sfzmhm = d.sfzmhm "+xm+" "+sfzmhm+" "+dabh+" "+ljjf+" "+zjcx+" "+glbm+" " ;
          System.out.println(SQL);
   
          rs = PageDBConnection.getResultSet(SQL);

          byte[] b = null;
          
          File file = new File("e:\\filename"+sessionUserName+".txt");
          FileOutputStream fos = new FileOutputStream(file);
          OutputStreamWriter out = new OutputStreamWriter(fos);//写入文件流
  
          ResultSetMetaData meta = rs.getMetaData();//获得ResultSetMeataData对象。所有方法的参数都是列的索引号,即第几列,从1开始

          for(int i=1; i<meta.getColumnCount();i++)//获得该ResultSet所有列的数目
          {
           b = (meta.getColumnName(i) + "\t").getBytes();//获得i列对应数据类型的类,输出列
           fos.write(b);//写入文件
          }
          // thisPageStartRow是获得了当前页面的起始行,thisPageEndRow是当前页面的结束行
          int thisPageStartRow = Integer.parseInt(sessionPageStartRow.toString());
          int thisPageEndRow = Integer.parseInt(sessionPageEndRow.toString());
          //利用thisPageStartRowOne判断应该保存的是哪页
          int thisPageStartRowOne = 0 ;
          while(rs.next())
          {
           //一直循环到想保存的页,保存页之前的不进行操作
           if(thisPageStartRowOne<thisPageStartRow)
           {
            thisPageStartRowOne++ ;
           }
           //判断是否是需要保存的页
           else if(thisPageStartRowOne>=thisPageStartRow&&thisPageStartRowOne<=thisPageEndRow)
           {
            thisPageStartRowOne++;
            fos.write("\r\n".getBytes());//让数据换行
            for(int i=1;i<meta.getColumnCount();i++)
            {
              b = (rs.getString(i) + "\t").getBytes();//输出数据,循环一次数据一个字段
              fos.write(b);
            }
           }
           //一旦保存完毕,则退出循环
           if(thisPageStartRowOne>=thisPageEndRow)
           {
            sqlDriverQuerySaveFileSqlError = true;
            break ;
           }
          }
          fos.close();//该关的关
          out.close();
  }
  catch (SQLException e) 
  {
   e.printStackTrace();
  } 
  finally 
  {
   if (rs != null) 
   {
    try 
    {
     rs.close();
    } 
    catch (SQLException e) 
    {
     e.printStackTrace();
    }
   }
  }
  return sqlDriverQuerySaveFileSqlError;
 }

 下载功能

try
      {
       response.setContentType("application/vnd.ms-excel");     
       response.setHeader("Content-disposition","attachment; filename=dzjc.xls");
       //这里的abc是保存时的默认文件名
       BufferedInputStream bis = null;
       BufferedOutputStream bos = null;
       try 
       {
           bis = new BufferedInputStream(new FileInputStream("E:\\filename"+sessionUserName+".txt"));
        bos = new BufferedOutputStream(response.getOutputStream());

        byte[] buff = new byte[1024];
        int bytesRead;

        while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) 
        {
         bos.write(buff,0,bytesRead);
        }
        if (bis != null)
         bis.close();
        if (bos != null)
         bos.close();
       } 
       catch(final IOException e) 
       {
        System.out.println (  e );
       } finally 
       {
        if (bis != null)
         bis.close();
        if (bos != null)
         bos.close();
       }
       return null;
      }
      catch (Exception e)
      {
       System.out.println(e);
      }

 

黑色头发  http://heisetoufa.iteye.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值