JAVA导出txt文件出错?
本帖最后由 zhxc87 于 2013-05-31 00:03:10 编辑
我的代码:
String txt = abc.getstr(no);
response.setHeader("Content-Disposition", "attachment; filename=10000"+today+".txt");
response.setHeader("Content-Type ", "application/octet-stream");
ServletOutputStream servletOS = response.getOutputStream();
IOUtils.write(txt, servletOS);
servletOS.flush();
public static String getstr(String no)
{
StringBuffer strb = new StringBuffer();
String sql = " select a1,a2,a3,a4from tablewhere no= '"+ no+ "'";
try
{
DBAccess dbAgent = new DBAccess();
dbAgent.setConnection("ora");
dbAgent.connect();
Connection conn = dbAgent.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next())
{
String a1 = rs.getString("a1");
String a2= rs.getString("a2");
String a3= rs.getString("a3");
String a4= rs.getString("a4");
strb.append(a1);
strb.append(" ");
strb.append(a2);
strb.append(" ");
strb.append(a3);
strb.append(" ");
strb.append(a4);
strb.append("\r\n");
Process ps;
try
{
ps = Runtime.getRuntime().exec(
"/home/tomcat/bin/11 " + a1+ " "+ a2+ "");
BufferedReader br = new BufferedReader(
new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null)
{
sb.append(line).append("");
}
String result = sb.toString();
if(result.length()==16)
{
try
{
String ret = update(dbAgent,a1,result);
}
catch (Exception e)
{
e.printStackTrace();
}
}
br.close();
ps.destroy();
ps = null;
}
catch (
IOException e)
{
e.printStackTrace();
}
}
rs.close();
st.close();
conn.close();
}
catch (
SQLException e)
{
e.printStackTrace();
}
return strb.toString();
}
我在导出生成strb的时候,需要去执行Linux上的一个脚