6d2db53152ad907c6fb1cbbcbe0a82e0163254fd.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. package com.sinosoft.lz.common.file;
  2. import java.awt.geom.AffineTransform;
  3. import java.awt.image.AffineTransformOp;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.InputStream;
  9. import java.math.BigDecimal;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Timestamp;
  15. import java.util.ArrayList;
  16. import javax.imageio.ImageIO;
  17. import org.apache.commons.codec.binary.Base64;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import com.persistence.service.JDBCHelper;
  21. import com.persistence.service.PersistenceFactory;
  22. import com.persistence.service.SysPersistence;
  23. import com.persistence.service.assitant.DataObject;
  24. import com.persistence.service.assitant.generator.IdentityGenerator;
  25. import com.persistence.service.exception.PersistenceException;
  26. import com.sinosoft.lz.common.Util;
  27. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  28. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  29. import com.sinosoft.lz.system.configer.service.ServerConfigure;
  30. public class FileImpl implements Fileable{
  31. private Log log = LogFactory.getLog(FileImpl.class);
  32. private SysModel sysmodel = ModelFactory.getSysmodel();
  33. private SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  34. ServerConfigure sc = ServerConfigure.getServerConfigerInstance();
  35. Connection myconn = null;
  36. public void setPersistence(SysPersistence persistence) {
  37. this.persistence = persistence;
  38. }
  39. /**
  40. * 刘岩 2008-11-29 PDA事件中的附件与创建的事件进行绑定
  41. *
  42. * @param helper
  43. * 数据库辅助类
  44. * @param pdaId
  45. * pda的id
  46. * @param eventId
  47. * 业务id
  48. * @param stepId
  49. * 业务类别id
  50. * @return 布尔值
  51. * @throws PersistenceException
  52. */
  53. public boolean setPdaFileBondingEvent(JDBCHelper helper, String pdaId, String eventId,
  54. String stepId) throws Exception {
  55. boolean flag = false;
  56. int num = persistence.getFunctionNumber(110,
  57. "select count(*) from SYS_COMM_FILEIMG where pdaId ='" + pdaId
  58. + "' and Is_Del='0' ");
  59. String sql = "update SYS_COMM_FILEIMG set MainId ='" + eventId + "',StepId='" + stepId
  60. + "',UpdateDate='" + Util.getTimestampDateTime() + "' where pdaId ='" + pdaId + "'";
  61. log.info(sql);
  62. if (num > 0) {
  63. if (helper.executeUpdateSQL(sql) > 0) {
  64. flag = true;
  65. }
  66. } else {
  67. flag = true;
  68. }
  69. return flag;
  70. }
  71. /**
  72. * 合并文件块 2009-05-12 吴潇
  73. *
  74. * @throws Exception
  75. */
  76. public int amalgamateFileBlock() throws Exception {
  77. boolean flag = false;
  78. int num = 0;
  79. ArrayList<DataObject> fileTempList = persistence.searchAllData(114, " where Is_Del='0'");
  80. for (int i = 0; i < fileTempList.size(); i++) {
  81. DataObject fileTemp = fileTempList.get(i);
  82. BigDecimal Total_Block = (BigDecimal) fileTemp.getValue("Total_Block");
  83. String sql = "select count(*) from SysComm_FileBlock where Main_Id='"
  84. + fileTemp.getObjectID() + "' and Is_Del='0'";
  85. int n = persistence.getFunctionNumber(118, sql);
  86. if (Total_Block.intValue() == n) {
  87. flag = this.createFileImg(fileTemp);
  88. if (flag) {
  89. num++;
  90. }
  91. }
  92. }
  93. if (myconn != null) {
  94. try {
  95. myconn.close();
  96. } catch (SQLException e1) {
  97. log.error(e1);
  98. }
  99. }
  100. return num;
  101. }
  102. private boolean createFileImg(DataObject fileTemp) throws Exception {
  103. boolean flag = false;
  104. final String SysComm_FileBlock = "select * from SysComm_FileBlock where Main_Id='"
  105. + fileTemp.getObjectID() + "' order by File_Index";
  106. try {
  107. myconn = sc.getDataSource().getConnection();
  108. // 取消事物自动提交功能,编程控制事物提交,保证主表--附表或是附件表 在一个事物中。
  109. myconn.setAutoCommit(false);
  110. } catch (SQLException e) {
  111. log.error(e);
  112. try {
  113. myconn.close();
  114. } catch (SQLException e1) {
  115. log.error(e1);
  116. }
  117. flag = false;
  118. }
  119. PreparedStatement ps = myconn.prepareStatement(SysComm_FileBlock);
  120. ResultSet rs = ps.executeQuery();
  121. File file = new File(ServerConfigure.getServerConfigerInstance().getImage_HM()
  122. + fileTemp.getObjectID() + ".jpg");
  123. FileOutputStream output = new FileOutputStream(file);
  124. while (rs.next()) {
  125. InputStream blobinputStream = rs.getBinaryStream("Contents");
  126. byte[] buffer = new byte[blobinputStream.available()];
  127. int nread = 0;
  128. while ((nread = blobinputStream.read(buffer)) != -1)
  129. output.write(buffer, 0, nread);
  130. output.flush();
  131. blobinputStream.close();
  132. }
  133. output.close();
  134. ps.close();
  135. rs.close();
  136. if (myconn != null) {
  137. try {
  138. myconn.close();
  139. } catch (SQLException e1) {
  140. log.error(e1);
  141. }
  142. }
  143. FileInputStream input = new FileInputStream(file);
  144. byte[] fileBytes = new byte[input.available()];
  145. input.read(fileBytes);
  146. input.close();
  147. JDBCHelper helper = new JDBCHelper();
  148. helper.begin();
  149. // DataObject object = new DataObject();
  150. // object.setClassid(110);
  151. //
  152. // object.addAttribute(Util.getFieldObj("UserId",fileTemp.getValue("UserId")));
  153. // object.addAttribute(Util.getFieldObj("MainId",fileTemp.getValue("MainId")));
  154. // object.addAttribute(Util.getFieldObj("Description",fileTemp.getValue("Description")));
  155. // object.addAttribute(Util.getFieldObj("Type",fileTemp.getValue("Type")));
  156. // object.addAttribute(Util.getFieldObj("UploadTime",fileTemp.getValue("UploadTime")));
  157. // object.addAttribute(Util.getFieldObj("Contents",fileBytes));
  158. // object.addAttribute(Util.getFieldObj("UpdateDate",Util.getDateTime()));
  159. //
  160. // String con = helper.addObjectData(object);
  161. //
  162. // if(con.length() == 30){
  163. // flag = true;
  164. // }
  165. // log.info("-----------------------wuxiao");
  166. flag = this.saveFileInfo(fileTemp, fileBytes, helper);
  167. if (flag) {
  168. flag = helper.deleteData(fileTemp.getObjectID(), 114);
  169. }
  170. if (flag) {
  171. String delSql = "delete from SysComm_FileBlock where Main_Id='"
  172. + fileTemp.getObjectID() + "'";
  173. int num = helper.executeUpdateSQL(delSql);
  174. if (num > 0) {
  175. flag = true;
  176. } else {
  177. flag = true;
  178. }
  179. }
  180. if (flag) {
  181. helper.commit();
  182. } else {
  183. helper.rollback();
  184. }
  185. file.delete();
  186. return flag;
  187. }
  188. /**
  189. * 刘岩 2009-9-14 根据图片实体SysCommFileImg类型往分局里面插入图片数据
  190. *
  191. * @param 图片实体对象
  192. * @param 数据库操作helper
  193. * @return 布尔值--是否插入成功
  194. */
  195. @SuppressWarnings("unused")
  196. public boolean saveFileInfo(DataObject fileTemp, byte[] fileBytes, JDBCHelper helper) {
  197. boolean falg = false;
  198. Connection con = helper.getConnection();
  199. PreparedStatement ps = null;
  200. int count = 0;
  201. Base64 b64 = new Base64();
  202. try {
  203. String sql = "insert into SYS_COMM_FILEIMG (" + "FD_OBJECTID" + ",Name " + ",FileExt "
  204. + ",MainId " + ",StepId " + ",UserId "
  205. + ",Type,Contents,UploadTime,Description,Is_Del,UpdateDate"
  206. + ")values(?,?,?,?,?,?,?,?,?,?,?,?)";
  207. ps = con.prepareStatement(sql);
  208. String img_Id = (String) fileTemp.getValue("Img_Id") == null ? "" : (String) fileTemp
  209. .getValue("Img_Id");
  210. if (img_Id.equals("")) {
  211. IdentityGenerator identityGenerator = IdentityGenerator.getIdentityGenerator();
  212. img_Id = identityGenerator.gerenalIdentity(110);
  213. }
  214. ps.setString(1, img_Id); // FD_OBJECTID CHAR(30) NOT
  215. // NULL,
  216. ps.setString(2, (String) fileTemp.getValue("Name")); // Name
  217. // VARCHAR(100),
  218. ps.setString(3, (String) fileTemp.getValue("FileExt")); // FileExt
  219. // VARCHAR(10),
  220. ps.setString(4, (String) fileTemp.getValue("MainId")); // MainId
  221. // VARCHAR(30),
  222. ps.setString(5, (String) fileTemp.getValue("StepId")); // StepId
  223. // VARCHAR(25),
  224. ps.setString(6, (String) fileTemp.getValue("UserId")); // UserId
  225. // VARCHAR(25),
  226. ps.setString(7, (String) fileTemp.getValue("Type")); // Type
  227. // VARCHAR(25),
  228. // TODO Base64 encode byte[] should convert to raw byte[]
  229. // 从base64的字节数组转换成二进制原始格式
  230. ps.setBlob(8, new javax.sql.rowset.serial.SerialBlob(fileBytes));
  231. if (fileTemp.getValue("UploadTime") == null) {
  232. ps.setTimestamp(9, null);
  233. } else {
  234. ps.setTimestamp(9, Timestamp.valueOf((String) fileTemp.getValue("UploadTime")));
  235. }
  236. ps.setString(10, (String) fileTemp.getValue("Description")); // Description
  237. // VARCHAR(500),
  238. ps.setString(11, (String) fileTemp.getValue("Is_Del")); // Type
  239. // VARCHAR(25),
  240. ps.setTimestamp(12, Util.getTimestampDateTime());
  241. count = ps.executeUpdate();
  242. ps.close();
  243. } catch (SQLException e) {
  244. e.printStackTrace();
  245. }
  246. if (count > 0) {
  247. falg = true;
  248. }
  249. return falg;
  250. }
  251. @SuppressWarnings("unused")
  252. public boolean deleteFileByEventId(String id, JDBCHelper helper, boolean realDel)
  253. throws PersistenceException {
  254. String delSql = "delete from SYS_COMM_FILEIMG where MainId='" + id + "'";
  255. boolean flag = true;
  256. String sql = "";
  257. Connection con = helper.getConnection();
  258. PreparedStatement ps = null;
  259. try {
  260. int count = 0;
  261. if (realDel) {
  262. sql = "delete from SYS_COMM_FILEIMG where MainId ='" + id + "'";
  263. ps = con.prepareStatement(sql);
  264. count = ps.executeUpdate();
  265. } else {
  266. sql = "update SYS_COMM_FILEIMG set is_Del=?,UpdateDate=? where MainId ='" + id
  267. + "'";
  268. ps = con.prepareStatement(sql);
  269. ps.setString(1, "1");
  270. ps.setTimestamp(2, Util.getTimestampDateTime());
  271. count = ps.executeUpdate();
  272. }
  273. if (count <= 0) {
  274. String search = "select count(*) from SYS_COMM_FILEIMG where MainId ='" + id + "'";
  275. ps = con.prepareStatement(search);
  276. ResultSet rs = ps.executeQuery();
  277. if (rs.next())
  278. flag = rs.getInt(1) == 0;
  279. rs.close();
  280. }
  281. ps.close();
  282. } catch (SQLException e) {
  283. throw new PersistenceException(e.getMessage());
  284. }
  285. return flag;
  286. }
  287. @SuppressWarnings("unused")
  288. public byte[] diminishImg(File bigFile, File smallFile) {
  289. byte[] Contents = null;
  290. try {
  291. AffineTransform transform = new AffineTransform();
  292. BufferedImage bis = ImageIO.read(bigFile);
  293. int w = bis.getWidth();
  294. int h = bis.getHeight();
  295. double scale = (double) w / h;
  296. int nw = 300;
  297. int nh = (nw * h) / w;
  298. if (nh > 300) {
  299. nh = 300;
  300. nw = (nh * w) / h;
  301. }
  302. double sx = (double) nw / w;
  303. double sy = (double) nh / h;
  304. transform.setToScale(sx, sy);
  305. AffineTransformOp ato = new AffineTransformOp(transform, null);
  306. BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
  307. ato.filter(bis, bid);
  308. ImageIO.write(bid, "jpeg", smallFile);
  309. FileInputStream Content = new FileInputStream(smallFile);
  310. Contents = new byte[Content.available()];
  311. Content.read(Contents);
  312. Content.close();
  313. } catch (Exception e) {
  314. e.printStackTrace();
  315. }
  316. return Contents;
  317. }
  318. public boolean rotatePic(String objectID, String rot) throws Exception {
  319. boolean flag = true;
  320. final String SysComm_FileBlock = "select * from SYS_COMM_FILEIMG where FD_OBJECTID='"
  321. + objectID + "'";
  322. try {
  323. myconn = sc.getDataSource().getConnection();
  324. // 取消事物自动提交功能,编程控制事物提交,保证主表--附表或是附件表 在一个事物中。
  325. myconn.setAutoCommit(false);
  326. } catch (SQLException e) {
  327. log.error(e);
  328. try {
  329. myconn.close();
  330. } catch (SQLException e1) {
  331. log.error(e1);
  332. }
  333. flag = false;
  334. }
  335. PreparedStatement ps = myconn.prepareStatement(SysComm_FileBlock);
  336. ResultSet rs = ps.executeQuery();
  337. String filePath = ServerConfigure.getServerConfigerInstance().getImage_HM() + objectID
  338. + ".jpg";
  339. File file = new File(filePath);
  340. FileOutputStream output = new FileOutputStream(file);
  341. while (rs.next()) {
  342. InputStream blobinputStream = rs.getBinaryStream("Contents");
  343. byte[] buffer = new byte[blobinputStream.available()];
  344. int nread = 0;
  345. while ((nread = blobinputStream.read(buffer)) != -1)
  346. output.write(buffer, 0, nread);
  347. output.flush();
  348. blobinputStream.close();
  349. }
  350. output.close();
  351. ps.close();
  352. rs.close();
  353. if (myconn != null) {
  354. try {
  355. myconn.close();
  356. } catch (SQLException e1) {
  357. log.error(e1);
  358. }
  359. }
  360. String filePath_ = ServerConfigure.getServerConfigerInstance().getImage_HM() + objectID
  361. + "_r.jpg";
  362. File rot_file = new File(filePath_); // 将要转换出的小图文件
  363. BufferedImage bis = ImageIO.read(file);
  364. BufferedImage bid = Rotate.rotateJ2D(bis, new Integer(rot).intValue(), null);
  365. ImageIO.write(bid, "jpeg", rot_file);
  366. FileInputStream input = new FileInputStream(rot_file);
  367. byte[] fileBytes = new byte[input.available()];
  368. input.read(fileBytes);
  369. input.close();
  370. DataObject object = new DataObject();
  371. object.setClassid(110);
  372. object.setObjectID(objectID);
  373. object.addAttribute(Util.getFieldObj("Contents", fileBytes));
  374. object.addAttribute(Util.getFieldObj("UpdateDate", Util.getDateTime()));
  375. file.delete();
  376. rot_file.delete();
  377. flag = persistence.updateObjectData(object);
  378. return flag;
  379. }
  380. public static void main(String argv[]) {
  381. try {
  382. FileImpl FileImpl = new FileImpl();
  383. FileImpl.rotatePic("110124565892398400001000110109", "");
  384. } catch (Exception e) {
  385. e.printStackTrace();
  386. }
  387. }
  388. }