byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
out.write(tmp, 0, l);
}
out.flush();
out.close();
System.out.println("****************************** ");
System.out.println("");
System.out.println("*************** 恭喜! 报告创建成功 结束 ***************");
System.out.println("");
}else{
msg = "8";//说明word创建成功,但是数据没有保存成功
response = null;
}
}else{
msg = "2";
}
} catch (ClientProtocolException e) {
msg = "7";
e.printStackTrace();
} catch (IOException e) {
msg = "7";
logger.error("数据库报告服务器地址配置错误或网络不通!!2.连接是否超时" + e.getMessage());
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
msg = "7";
logger.error("数据库报告服务器地址配置错误或网络不通!!2.连接是否超时" + e.getMessage());
e.printStackTrace();
}
}
}
}
long time2 = System.currentTimeMillis();
long numTime = time2 - time1;
if(docType.toString().equals(XmlPathDef.SPOTTEST_DOC) && docType.toString().equals(XmlPathDef.FTEST_DOC)){
if(numTime >= 2401000){
msg = "9";
}
}else{
if(numTime >= 601000){
msg = "9";
}
}
System.out.println("");
String loggerinfo = "********* 报告类型为 :" + docType + " 执行时间为: " + (time2 - time1) /1000 + " 秒!***************";
System.out.println(loggerinfo);
System.out.println("");
System.out.println("*****************************");
logger.info(loggerinfo);
return msg;
}
这个方法还有待优化和调整,现在我主要说明他的作用:
首先使用了synchronized 关键字 意思说使用同步的方式,让每次只有一个线程运行这个方法(其实如果是大数量的并发,就不能这样写了,自己去摸索吧)。然后在方法体中,使用了一个httpclient技术,这个主要是调用远程的服务返回对象流,在我这里就是调用另外一个服务器的代码,去生成报告,
然后将word报告的流返回来就ok,如果不明白httpclient的用法,请上网学一下,具体我就不说了。(下节我会贴出相应的代码)
最后通过httpclient的返回流写成word文档就行了(其实里面还有一些判断什么的,比如:最后返回流是否是word流啊,什么的 都需要判断的,我都去掉了,代码太多),返回一个状态码给action就完毕。
摘自 tanghui2qinghong的专栏