d2a762c8d8a4263c71f473cce5e104102cdd9dfc.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package UIB.UIB03;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.net.URL;
  12. import java.net.URLDecoder;
  13. import java.sql.Connection;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.util.ArrayList;
  17. import javax.servlet.ServletConfig;
  18. import javax.servlet.http.HttpServlet;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import UIB.COM.XmlSqlParsersFactory;
  22. import CoreFS.SA01.CoreIComponent;
  23. import CoreFS.SA06.CoreReturnObject;
  24. /**
  25. *
  26. * @desc 质保书模板管理
  27. * @author meiguiping
  28. * @date 2010 4:21:32 PM
  29. */
  30. public class UIB030310 extends CoreIComponent
  31. {
  32. /**
  33. * @desc 界面载入时获取配置所有文件
  34. * @return
  35. */
  36. public CoreReturnObject loadUIB030310()
  37. {
  38. CoreReturnObject cro = new CoreReturnObject();
  39. cro.setResult(getFileName());
  40. return cro;
  41. }
  42. /**
  43. * @desc 获取所有文件列表
  44. * @return
  45. */
  46. public String[] getFileName()
  47. {
  48. String filePath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/");//Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig";
  49. File file = new File(filePath);
  50. String[]fileDir = file.list();
  51. return fileDir;
  52. }
  53. /**
  54. * @desc 上传质保书、图片、配置文件等
  55. * @param tmplNo 模板号
  56. * @param bData 上传的文件,以byte数组形式传递
  57. * @parm extName 文件扩展名
  58. * @return
  59. * @throws Exception
  60. */
  61. public CoreReturnObject upLoadFile(String tmplNo , byte[] bData , String extName , String userName) throws Exception
  62. {
  63. /*
  64. File file=new File("E:\\a.xls");
  65. try {
  66. //创建文件输入流对象.指定要读取的文件对象
  67. FileInputStream fin=new FileInputStream(file);
  68. //创建字节数组,准备将文件流中的数据传给字节数组
  69. byte[] b=new byte[fin.available()];
  70. ByteArrayOutputStream out = new ByteArrayOutputStream();
  71. int n = 0;
  72. while ((n = fin.read(b)) != -1) {
  73. out.write(b, 0, n);
  74. }
  75. b=out.toByteArray();
  76. bData=b;
  77. fin.close();
  78. } catch (FileNotFoundException e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. } catch (IOException e) {
  82. // TODO Auto-generated catch block
  83. e.printStackTrace();
  84. }*/
  85. CoreReturnObject cro = new CoreReturnObject();
  86. this.getDao("KgDao").ExceuteNonQueryForBlobList(XmlSqlParsersFactory.getSql("UIB030310_02.UPDATE") , new Object[]{bData,extName,userName,tmplNo });
  87. //此处没有区分上传的是质保书模板或配置文件,无论上传什么文件,图片和配置文件均重新下载,刷新一次
  88. configFileDownload();
  89. // Connection con = null;
  90. // PreparedStatement pstm= null;
  91. // try
  92. // {
  93. // con = this.getDao("KgDao").getConnection();
  94. // con.setAutoCommit(false);
  95. //
  96. // pstm = con.prepareStatement(XmlSqlParsersFactory.getSql("UIB030310_02.SELECT"));
  97. // pstm.setString(1, tmplNo);
  98. // ResultSet rs = pstm.executeQuery();
  99. //
  100. // if(rs.next())
  101. // {
  102. // oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("TMPL_EXCEL");
  103. // OutputStream os = blob.getBinaryOutputStream();
  104. //// this.dbproxy.getLobHandler().getBlobAsBytes(rs , "TMPL_EXCEL");
  105. //
  106. //
  107. // InputStream is = new ByteArrayInputStream(bData);
  108. //
  109. // byte[] bb = new byte[blob.getBufferSize()];
  110. // int len = 0;
  111. // while((len = is.read(bb)) != -1)
  112. // {
  113. // os.write(bb , 0 , len);
  114. // }
  115. //
  116. // is.close();
  117. // os.flush();
  118. // os.close();
  119. // con.commit();
  120. // }
  121. // rs.close();
  122. // }catch(Exception ex)
  123. // {
  124. // throw new Exception("质保书上传异常!");
  125. // }
  126. // finally
  127. // {
  128. // try
  129. // {
  130. // if(pstm != null)
  131. // pstm.close();
  132. // if(con !=null)
  133. // con.close();
  134. // }catch(Exception e)
  135. // {
  136. // throw new Exception("数据库连接关闭异常!");
  137. // }
  138. // }
  139. return cro;
  140. }
  141. /**
  142. * @desc 下载质保书、图片、配置文件等
  143. * @param tmplNo 模板号
  144. * @return
  145. * @throws Exception
  146. */
  147. public CoreReturnObject downLoadFile(String tmplNo) throws Exception
  148. {
  149. Connection con = null;
  150. CoreReturnObject cro = new CoreReturnObject();
  151. PreparedStatement pstm = null;
  152. try
  153. {
  154. con = this.getDao("KgDao").getConnection();
  155. pstm = con.prepareStatement(XmlSqlParsersFactory.getSql("UIB030310_03.SELECT"));
  156. pstm.setString(1, tmplNo);
  157. ResultSet rs = pstm.executeQuery();
  158. if(rs.next())
  159. {
  160. // oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("TMPL_EXCEL");
  161. // cro.setResult(blob.getBytes(1 , (int)blob.length()));
  162. cro.setResult(this.dbproxy.getLobHandler().getBlobAsBytes(rs , "TMPL_EXCEL"));
  163. }
  164. rs.close();
  165. }catch(Exception ex)
  166. {
  167. throw new Exception("质保书下载异常!");
  168. }
  169. finally
  170. {
  171. try
  172. {
  173. if(pstm !=null)
  174. pstm.close();
  175. if(con != null)
  176. con.close();
  177. }catch(Exception e)
  178. {
  179. throw new Exception("数据库连接关闭异常!");
  180. }
  181. }
  182. return cro;
  183. }
  184. /**
  185. * @desc 配置文件等上传
  186. * @param bData
  187. * @deprecated 2011-9-9
  188. * @return
  189. */
  190. public CoreReturnObject xmlUpLoad(byte[] bData , String fName)
  191. {
  192. CoreReturnObject cro = new CoreReturnObject();
  193. OutputStream os = null;
  194. try
  195. {
  196. String filePath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/");//Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig";
  197. // URL url = new URL("http://localhost:8088");
  198. filePath =URLDecoder.decode(filePath,"utf-8"); //在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误,解决办法
  199. os = new FileOutputStream(filePath+File.separator+fName);
  200. os.write(bData);
  201. os.close();
  202. }
  203. catch(Exception ex)
  204. {
  205. ex.printStackTrace();
  206. }
  207. finally
  208. {
  209. try
  210. {
  211. os.close();
  212. }catch(Exception e)
  213. {
  214. e.printStackTrace();
  215. }
  216. }
  217. return cro;
  218. }
  219. /**
  220. * @dresc 下载文件
  221. * @param fileName
  222. * @deprecated 2011-9-9
  223. * @return
  224. */
  225. public CoreReturnObject xmlDownLoadFile(String fileName)
  226. {
  227. CoreReturnObject cro = new CoreReturnObject();
  228. String filePath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/");//Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig"+File.separator;
  229. File file = new File(filePath+fileName);
  230. InputStream is = null;
  231. byte[] bData = new byte[(int)file.length()];
  232. try
  233. {
  234. is = new FileInputStream(file);
  235. is.read(bData);
  236. is.close();
  237. }catch(Exception ex)
  238. {
  239. ex.printStackTrace();
  240. }
  241. finally
  242. {
  243. try
  244. {
  245. is.close();
  246. }catch(Exception e)
  247. {
  248. e.printStackTrace();
  249. }
  250. }
  251. cro.setResult(bData);
  252. return cro;
  253. }
  254. /**
  255. * @desc 配置文件、图片下载到应用服务器
  256. * 在应用服务器启动时运行一次,以及修改后运行一次
  257. * @throws Exception
  258. */
  259. public void configFileDownload()throws Exception
  260. {
  261. Connection con = null;
  262. CoreReturnObject cro = new CoreReturnObject();
  263. PreparedStatement pstm = null;
  264. try
  265. {
  266. con = this.getDao("KgDao").getConnection();
  267. FileOutputStream fos = null;
  268. ByteArrayInputStream bis = null;
  269. pstm = con.prepareStatement(XmlSqlParsersFactory.getSql("UIB030310_04.SELECT"));
  270. ResultSet rs = pstm.executeQuery();
  271. byte[] bytes = null;
  272. byte[] buffer = null;
  273. //String strpath = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig//";
  274. String queryPath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/").replaceAll("%20", " ");
  275. while(rs.next())
  276. {
  277. bytes = this.dbproxy.getLobHandler().getBlobAsBytes(rs , "TMPL_EXCEL");//读取到字节数组
  278. // in = new ByteArrayInputStream(bytes);//
  279. buffer = new byte[bytes.length];
  280. fos = new FileOutputStream(queryPath+rs.getString("TMPL_CFNM")+rs.getString("EXT_NAME"));
  281. int bytesum = 0;
  282. int byteread = 0;
  283. fos.write(bytes);
  284. fos.close();
  285. }
  286. rs.close();
  287. }catch(Exception ex)
  288. {
  289. throw new Exception("配置文件下载异常!");
  290. }
  291. finally
  292. {
  293. try
  294. {
  295. if(pstm !=null)
  296. pstm.close();
  297. if(con != null)
  298. con.close();
  299. }catch(Exception e)
  300. {
  301. throw new Exception("数据库连接关闭异常!");
  302. }
  303. }
  304. }
  305. }