JSP中如何利用outputStream下载一个dbase III文件?
<%@ page language="java" import="java.io.*"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="com.sun.rowset.CachedRowSetImpl"%>
<%@ page import="com.linuxense.javadbf.*"%>
<%@ page import="att_I_ZhongShanAttendance"%>
<jsp:useBean id="query" class="com.goldpeak.ehrm.web.zhongshan.eHRM2ZSDBFHandler">
<jsp:setProperty name="query" property="request" value="<%=request%>"/>
<jsp:setProperty name="query" property="session" value="<%=session%>"/>
</jsp:useBean>
<c:set var="resultlist" value="${query.query}" scope="request">
</c:set>
<%
try
{
//System.out.println(request.getRealPath("/"));
//String path=request.getRealPath("/");
String tmppath =System.getProperty("java.io.tmpdir");
CachedRowSetImpl crset = (CachedRowSetImpl)request.getAttribute("resultlist");
att_I_ZhongShanAttendance ZS = new att_I_ZhongShanAttendance();
DBFWriter writer = new DBFWriter();
writer.setFields(ZS.fields);
while (crset.next())
{
Object rowData[] = new Object[ZS.fields.length];
java.sql.ResultSetMetaData meta = crset.getMetaData();
for (int i = 0; i <ZS.fields.length ; i++)
{
System.out.println(i+":"+ZS.fields[i].getName()+":");
if(meta.getColumnTypeName(i+1).equals("DECIMAL"))
{
rowData[i] = new Double(crset.getDouble(ZS.fields[i].getName().trim()));
}else
{
rowData[i] = crset.getString(ZS.fields[i].getName().trim());
}
}
writer.addRecord(rowData);
}
//response.setContentType("application/x-msexcel");
response.setHeader("Content-Type","application/octet-stream");
PersonHelper person =(PersonHelper)session.getAttribute("personHelper");
java.util.Calendar ca=java.util.Calendar.getInstance();
String termstartdate=(String)request.getParameter("startdate");
String termenddate=(String)request.getParameter("enddate");
String filename=person.getFactoryCode()+"_"+person.getUserID()+"_"+
"_"+termstartdate+"_"+termenddate+".dbf";
//System.out.println(filename);
response.setHeader("Content-Disposition","attachment; filename="+filename);
response.setHeader("Connection","close");
tmppath = tmppath+System.currentTimeMillis()+".dbf";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writer.write(baos);
javax.servlet.ServletOutputStream outs = response.getOutputStream();
baos.writeTo(outs);
outs.flush();
baos.close();
outs.close();
}catch(Exception e)
{
e.printStackTrace();
}
%>
注意:利用了JDBF 0.40来实现DBF.