123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- package com.sinosoft.lz.common.file;
- import java.awt.geom.AffineTransform;
- import java.awt.image.AffineTransformOp;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.math.BigDecimal;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import javax.imageio.ImageIO;
- import org.apache.commons.codec.binary.Base64;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.persistence.service.JDBCHelper;
- import com.persistence.service.PersistenceFactory;
- import com.persistence.service.SysPersistence;
- import com.persistence.service.assitant.DataObject;
- import com.persistence.service.assitant.generator.IdentityGenerator;
- import com.persistence.service.exception.PersistenceException;
- import com.sinosoft.lz.common.Util;
- import com.sysmodel.datamodel.xmlmodel.ModelFactory;
- import com.sysmodel.datamodel.xmlmodel.able.SysModel;
- import com.sinosoft.lz.system.configer.service.ServerConfigure;
- public class FileImpl implements Fileable{
- private Log log = LogFactory.getLog(FileImpl.class);
- private SysModel sysmodel = ModelFactory.getSysmodel();
- private SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
- ServerConfigure sc = ServerConfigure.getServerConfigerInstance();
- Connection myconn = null;
- public void setPersistence(SysPersistence persistence) {
- this.persistence = persistence;
- }
- /**
- * 刘岩 2008-11-29 PDA事件中的附件与创建的事件进行绑定
- *
- * @param helper
- * 数据库辅助类
- * @param pdaId
- * pda的id
- * @param eventId
- * 业务id
- * @param stepId
- * 业务类别id
- * @return 布尔值
- * @throws PersistenceException
- */
- public boolean setPdaFileBondingEvent(JDBCHelper helper, String pdaId, String eventId,
- String stepId) throws Exception {
- boolean flag = false;
- int num = persistence.getFunctionNumber(110,
- "select count(*) from SYS_COMM_FILEIMG where pdaId ='" + pdaId
- + "' and Is_Del='0' ");
- String sql = "update SYS_COMM_FILEIMG set MainId ='" + eventId + "',StepId='" + stepId
- + "',UpdateDate='" + Util.getTimestampDateTime() + "' where pdaId ='" + pdaId + "'";
- log.info(sql);
- if (num > 0) {
- if (helper.executeUpdateSQL(sql) > 0) {
- flag = true;
- }
- } else {
- flag = true;
- }
- return flag;
- }
- /**
- * 合并文件块 2009-05-12 吴潇
- *
- * @throws Exception
- */
- public int amalgamateFileBlock() throws Exception {
- boolean flag = false;
- int num = 0;
- ArrayList<DataObject> fileTempList = persistence.searchAllData(114, " where Is_Del='0'");
- for (int i = 0; i < fileTempList.size(); i++) {
- DataObject fileTemp = fileTempList.get(i);
- BigDecimal Total_Block = (BigDecimal) fileTemp.getValue("Total_Block");
- String sql = "select count(*) from SysComm_FileBlock where Main_Id='"
- + fileTemp.getObjectID() + "' and Is_Del='0'";
- int n = persistence.getFunctionNumber(118, sql);
- if (Total_Block.intValue() == n) {
- flag = this.createFileImg(fileTemp);
- if (flag) {
- num++;
- }
- }
- }
- if (myconn != null) {
- try {
- myconn.close();
- } catch (SQLException e1) {
- log.error(e1);
- }
- }
- return num;
- }
- private boolean createFileImg(DataObject fileTemp) throws Exception {
- boolean flag = false;
- final String SysComm_FileBlock = "select * from SysComm_FileBlock where Main_Id='"
- + fileTemp.getObjectID() + "' order by File_Index";
- try {
- myconn = sc.getDataSource().getConnection();
- // 取消事物自动提交功能,编程控制事物提交,保证主表--附表或是附件表 在一个事物中。
- myconn.setAutoCommit(false);
- } catch (SQLException e) {
- log.error(e);
- try {
- myconn.close();
- } catch (SQLException e1) {
- log.error(e1);
- }
- flag = false;
- }
- PreparedStatement ps = myconn.prepareStatement(SysComm_FileBlock);
- ResultSet rs = ps.executeQuery();
- File file = new File(ServerConfigure.getServerConfigerInstance().getImage_HM()
- + fileTemp.getObjectID() + ".jpg");
- FileOutputStream output = new FileOutputStream(file);
- while (rs.next()) {
- InputStream blobinputStream = rs.getBinaryStream("Contents");
- byte[] buffer = new byte[blobinputStream.available()];
- int nread = 0;
- while ((nread = blobinputStream.read(buffer)) != -1)
- output.write(buffer, 0, nread);
- output.flush();
- blobinputStream.close();
- }
- output.close();
- ps.close();
- rs.close();
- if (myconn != null) {
- try {
- myconn.close();
- } catch (SQLException e1) {
- log.error(e1);
- }
- }
- FileInputStream input = new FileInputStream(file);
- byte[] fileBytes = new byte[input.available()];
- input.read(fileBytes);
- input.close();
- JDBCHelper helper = new JDBCHelper();
- helper.begin();
- // DataObject object = new DataObject();
- // object.setClassid(110);
- //
- // object.addAttribute(Util.getFieldObj("UserId",fileTemp.getValue("UserId")));
- // object.addAttribute(Util.getFieldObj("MainId",fileTemp.getValue("MainId")));
- // object.addAttribute(Util.getFieldObj("Description",fileTemp.getValue("Description")));
- // object.addAttribute(Util.getFieldObj("Type",fileTemp.getValue("Type")));
- // object.addAttribute(Util.getFieldObj("UploadTime",fileTemp.getValue("UploadTime")));
- // object.addAttribute(Util.getFieldObj("Contents",fileBytes));
- // object.addAttribute(Util.getFieldObj("UpdateDate",Util.getDateTime()));
- //
- // String con = helper.addObjectData(object);
- //
- // if(con.length() == 30){
- // flag = true;
- // }
- // log.info("-----------------------wuxiao");
- flag = this.saveFileInfo(fileTemp, fileBytes, helper);
- if (flag) {
- flag = helper.deleteData(fileTemp.getObjectID(), 114);
- }
- if (flag) {
- String delSql = "delete from SysComm_FileBlock where Main_Id='"
- + fileTemp.getObjectID() + "'";
- int num = helper.executeUpdateSQL(delSql);
- if (num > 0) {
- flag = true;
- } else {
- flag = true;
- }
- }
- if (flag) {
- helper.commit();
- } else {
- helper.rollback();
- }
- file.delete();
- return flag;
- }
- /**
- * 刘岩 2009-9-14 根据图片实体SysCommFileImg类型往分局里面插入图片数据
- *
- * @param 图片实体对象
- * @param 数据库操作helper
- * @return 布尔值--是否插入成功
- */
- @SuppressWarnings("unused")
- public boolean saveFileInfo(DataObject fileTemp, byte[] fileBytes, JDBCHelper helper) {
- boolean falg = false;
- Connection con = helper.getConnection();
- PreparedStatement ps = null;
- int count = 0;
- Base64 b64 = new Base64();
- try {
- String sql = "insert into SYS_COMM_FILEIMG (" + "FD_OBJECTID" + ",Name " + ",FileExt "
- + ",MainId " + ",StepId " + ",UserId "
- + ",Type,Contents,UploadTime,Description,Is_Del,UpdateDate"
- + ")values(?,?,?,?,?,?,?,?,?,?,?,?)";
- ps = con.prepareStatement(sql);
- String img_Id = (String) fileTemp.getValue("Img_Id") == null ? "" : (String) fileTemp
- .getValue("Img_Id");
- if (img_Id.equals("")) {
- IdentityGenerator identityGenerator = IdentityGenerator.getIdentityGenerator();
- img_Id = identityGenerator.gerenalIdentity(110);
- }
- ps.setString(1, img_Id); // FD_OBJECTID CHAR(30) NOT
- // NULL,
- ps.setString(2, (String) fileTemp.getValue("Name")); // Name
- // VARCHAR(100),
- ps.setString(3, (String) fileTemp.getValue("FileExt")); // FileExt
- // VARCHAR(10),
- ps.setString(4, (String) fileTemp.getValue("MainId")); // MainId
- // VARCHAR(30),
- ps.setString(5, (String) fileTemp.getValue("StepId")); // StepId
- // VARCHAR(25),
- ps.setString(6, (String) fileTemp.getValue("UserId")); // UserId
- // VARCHAR(25),
- ps.setString(7, (String) fileTemp.getValue("Type")); // Type
- // VARCHAR(25),
- // TODO Base64 encode byte[] should convert to raw byte[]
- // 从base64的字节数组转换成二进制原始格式
- ps.setBlob(8, new javax.sql.rowset.serial.SerialBlob(fileBytes));
- if (fileTemp.getValue("UploadTime") == null) {
- ps.setTimestamp(9, null);
- } else {
- ps.setTimestamp(9, Timestamp.valueOf((String) fileTemp.getValue("UploadTime")));
- }
- ps.setString(10, (String) fileTemp.getValue("Description")); // Description
- // VARCHAR(500),
- ps.setString(11, (String) fileTemp.getValue("Is_Del")); // Type
- // VARCHAR(25),
- ps.setTimestamp(12, Util.getTimestampDateTime());
- count = ps.executeUpdate();
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if (count > 0) {
- falg = true;
- }
- return falg;
- }
- @SuppressWarnings("unused")
- public boolean deleteFileByEventId(String id, JDBCHelper helper, boolean realDel)
- throws PersistenceException {
- String delSql = "delete from SYS_COMM_FILEIMG where MainId='" + id + "'";
- boolean flag = true;
- String sql = "";
- Connection con = helper.getConnection();
- PreparedStatement ps = null;
- try {
- int count = 0;
- if (realDel) {
- sql = "delete from SYS_COMM_FILEIMG where MainId ='" + id + "'";
- ps = con.prepareStatement(sql);
- count = ps.executeUpdate();
- } else {
- sql = "update SYS_COMM_FILEIMG set is_Del=?,UpdateDate=? where MainId ='" + id
- + "'";
- ps = con.prepareStatement(sql);
- ps.setString(1, "1");
- ps.setTimestamp(2, Util.getTimestampDateTime());
- count = ps.executeUpdate();
- }
- if (count <= 0) {
- String search = "select count(*) from SYS_COMM_FILEIMG where MainId ='" + id + "'";
- ps = con.prepareStatement(search);
- ResultSet rs = ps.executeQuery();
- if (rs.next())
- flag = rs.getInt(1) == 0;
- rs.close();
- }
- ps.close();
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- }
- return flag;
- }
- @SuppressWarnings("unused")
- public byte[] diminishImg(File bigFile, File smallFile) {
- byte[] Contents = null;
- try {
- AffineTransform transform = new AffineTransform();
- BufferedImage bis = ImageIO.read(bigFile);
- int w = bis.getWidth();
- int h = bis.getHeight();
- double scale = (double) w / h;
- int nw = 300;
- int nh = (nw * h) / w;
- if (nh > 300) {
- nh = 300;
- nw = (nh * w) / h;
- }
- double sx = (double) nw / w;
- double sy = (double) nh / h;
- transform.setToScale(sx, sy);
- AffineTransformOp ato = new AffineTransformOp(transform, null);
- BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
- ato.filter(bis, bid);
- ImageIO.write(bid, "jpeg", smallFile);
- FileInputStream Content = new FileInputStream(smallFile);
- Contents = new byte[Content.available()];
- Content.read(Contents);
- Content.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return Contents;
- }
- public boolean rotatePic(String objectID, String rot) throws Exception {
- boolean flag = true;
- final String SysComm_FileBlock = "select * from SYS_COMM_FILEIMG where FD_OBJECTID='"
- + objectID + "'";
- try {
- myconn = sc.getDataSource().getConnection();
- // 取消事物自动提交功能,编程控制事物提交,保证主表--附表或是附件表 在一个事物中。
- myconn.setAutoCommit(false);
- } catch (SQLException e) {
- log.error(e);
- try {
- myconn.close();
- } catch (SQLException e1) {
- log.error(e1);
- }
- flag = false;
- }
- PreparedStatement ps = myconn.prepareStatement(SysComm_FileBlock);
- ResultSet rs = ps.executeQuery();
- String filePath = ServerConfigure.getServerConfigerInstance().getImage_HM() + objectID
- + ".jpg";
- File file = new File(filePath);
- FileOutputStream output = new FileOutputStream(file);
- while (rs.next()) {
- InputStream blobinputStream = rs.getBinaryStream("Contents");
- byte[] buffer = new byte[blobinputStream.available()];
- int nread = 0;
- while ((nread = blobinputStream.read(buffer)) != -1)
- output.write(buffer, 0, nread);
- output.flush();
- blobinputStream.close();
- }
- output.close();
- ps.close();
- rs.close();
- if (myconn != null) {
- try {
- myconn.close();
- } catch (SQLException e1) {
- log.error(e1);
- }
- }
- String filePath_ = ServerConfigure.getServerConfigerInstance().getImage_HM() + objectID
- + "_r.jpg";
- File rot_file = new File(filePath_); // 将要转换出的小图文件
- BufferedImage bis = ImageIO.read(file);
- BufferedImage bid = Rotate.rotateJ2D(bis, new Integer(rot).intValue(), null);
- ImageIO.write(bid, "jpeg", rot_file);
- FileInputStream input = new FileInputStream(rot_file);
- byte[] fileBytes = new byte[input.available()];
- input.read(fileBytes);
- input.close();
- DataObject object = new DataObject();
- object.setClassid(110);
- object.setObjectID(objectID);
- object.addAttribute(Util.getFieldObj("Contents", fileBytes));
- object.addAttribute(Util.getFieldObj("UpdateDate", Util.getDateTime()));
- file.delete();
- rot_file.delete();
- flag = persistence.updateObjectData(object);
- return flag;
- }
- public static void main(String argv[]) {
- try {
- FileImpl FileImpl = new FileImpl();
- FileImpl.rotatePic("110124565892398400001000110109", "");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|