511 lines
17 KiB
Java
511 lines
17 KiB
Java
package com.amarsoft.app.als.image;
|
||
|
||
|
||
import java.io.ByteArrayInputStream;
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Date;
|
||
|
||
import javax.servlet.ServletException;
|
||
import javax.servlet.http.HttpServlet;
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
|
||
import jbo.image.ECM_PAGE;
|
||
import jbo.image.IMAGE_CODE_INFO;
|
||
import sun.misc.BASE64Decoder;
|
||
import sun.misc.BASE64Encoder;
|
||
|
||
import com.amarsoft.app.als.sys.tools.JBOHelper;
|
||
import com.amarsoft.app.contentmanage.Content;
|
||
import com.amarsoft.app.contentmanage.ContentManager;
|
||
import com.amarsoft.app.contentmanage.DefaultContentManagerImpl;
|
||
import com.amarsoft.app.contentmanage.action.ContentManagerAction;
|
||
import com.amarsoft.are.ARE;
|
||
import com.amarsoft.are.jbo.BizObject;
|
||
import com.amarsoft.are.jbo.BizObjectManager;
|
||
import com.amarsoft.are.jbo.JBOException;
|
||
import com.amarsoft.are.jbo.JBOFactory;
|
||
import com.amarsoft.are.jbo.JBOTransaction;
|
||
import com.amarsoft.are.lang.DateX;
|
||
import com.amarsoft.are.lang.StringX;
|
||
import com.amarsoft.webservice.util.CodeTrans;
|
||
|
||
/**
|
||
* 影像图片传输存储
|
||
* @author zszhang
|
||
* @since 2015/01/07
|
||
*
|
||
*/
|
||
public class ActiveXServlet extends HttpServlet{
|
||
|
||
|
||
//用于生成图片文件名中的日期格式
|
||
private SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
|
||
//插入图片的日期格式
|
||
private SimpleDateFormat insertPicDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||
|
||
|
||
private static final long serialVersionUID = 332233L;
|
||
|
||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||
throws ServletException, IOException {
|
||
doPost(req, resp);
|
||
}
|
||
|
||
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
|
||
throws ServletException, IOException {
|
||
|
||
//String objType, String objNo, String typeNo, String ids
|
||
String ids = req.getParameter("Ids");
|
||
String objectType = req.getParameter("ObjectType");
|
||
String objectNo = req.getParameter("ObjectNo");
|
||
String typeNo = req.getParameter("TypeNo");
|
||
|
||
String images = req.getParameter("Images");
|
||
String userId = req.getParameter("UserId");
|
||
String orgId = req.getParameter("OrgId");
|
||
String method = req.getParameter("Method");
|
||
String imageType = req.getParameter("ImageType");
|
||
|
||
String str = null;
|
||
if(ids != null ){ //是获取图片
|
||
str = getImage( ids);
|
||
//resp.setContentType("text/plain");
|
||
}else if(images != null){ //是上传图片
|
||
if("Trans".equals(method)){
|
||
//批量扫描
|
||
str = transImage(userId,orgId,images,imageType);
|
||
}else{
|
||
//单扫保存图片
|
||
str = saveImage(objectType, objectNo, typeNo, images, userId, orgId);
|
||
}
|
||
}
|
||
resp.getWriter().write(str!=null?str:"");
|
||
resp.getWriter().flush();
|
||
resp.getWriter().close();
|
||
}
|
||
|
||
/**
|
||
* 批量传输影像图片数据录入系统
|
||
* @param userID
|
||
* @param orgID
|
||
* @param images
|
||
* @param imageType
|
||
* @return
|
||
*/
|
||
public String transImage(String userID,String orgID,String images,String imageType){
|
||
String[] imgs = images.split("\\|");
|
||
String barCodeNo = "";
|
||
String imageInfo = "batchScan";//local/scan/batchScan/batchScanCopy/barCode
|
||
String ObjectNo="UnKnow",ObjectType="UnKnow",ImageType="UnKnow";
|
||
for(int i = 0;i<imgs.length;i++){
|
||
imageInfo = "batchScan";
|
||
ObjectNo = "UnKnow";
|
||
ObjectType = "UnKnow";
|
||
ImageType = "UnKnow";
|
||
byte[] b = CodeTrans.String2Byte(imgs[i].replace(" ", "+"));
|
||
InputStream is = new ByteArrayInputStream(b);
|
||
String s = QRUtil.read(is);
|
||
//s = "2014081900000008";
|
||
if(s!=null && !"".equals(s)){
|
||
barCodeNo = s;
|
||
//识别到条形码,单张资料或堆叠的第一张
|
||
imageInfo = "batchScan";
|
||
ARE.getLog().debug("获取到条形码:"+s);
|
||
}else{
|
||
//该资料为堆叠资料,条码只贴在了第一张上
|
||
imageInfo = "batchScanCopy";
|
||
ARE.getLog().debug("该页面未获取到条形码,将默认为之前扫描到的条形码:"+barCodeNo);
|
||
}
|
||
|
||
ARE.getLog().debug("开始存储该项下影像数据...");
|
||
try{
|
||
//查找该条形码流水号是否在记录表中
|
||
BizObject bocl = JBOHelper.getBizObjectByCondition(IMAGE_CODE_INFO.CLASS_NAME, "o", "serialNo =:SerialNo", barCodeNo);
|
||
if(bocl == null){
|
||
ARE.getLog().warn("未找到条形码"+barCodeNo+"的相关信息,无法将后续影像进行归类");
|
||
barCodeNo = "";
|
||
ObjectNo = "UnKnow";
|
||
ObjectType = "UnKnow";
|
||
ImageType = "UnKnow";
|
||
}else{
|
||
if (bocl != null){
|
||
ObjectNo = bocl.getAttribute("ObjectNo").getString();
|
||
ObjectType = bocl.getAttribute("ObjectType").getString();
|
||
ImageType = bocl.getAttribute("ImageType").getString();
|
||
}
|
||
}
|
||
}catch(Exception e){
|
||
ARE.getLog().warn("获取图片信息出错!");
|
||
}
|
||
batchSaveImage(ObjectType, ObjectNo, ImageType, imgs[i], userID, orgID, imageType, imageInfo);
|
||
}
|
||
return "Finish";
|
||
}
|
||
|
||
/**
|
||
* 批量保存影像页面图片到系统
|
||
* @param ObjectType
|
||
* @param ObjectNo
|
||
* @param TypeNo
|
||
* @param image
|
||
* @param userId
|
||
* @param orgId
|
||
* @param pageNum
|
||
* @param imageType
|
||
* @param imageInfo
|
||
* @return
|
||
*/
|
||
public String batchSaveImage(String ObjectType, String ObjectNo, String TypeNo,
|
||
String image, String userId, String orgId,
|
||
String imageType, String imageInfo) {
|
||
|
||
if (!(ContentManagerAction.IsUseContentManager && ContentManagerAction.isConfCorrect))
|
||
return "";
|
||
ContentManager contentManager = ContentManagerAction.getContentManager();
|
||
if (contentManager == null)
|
||
return "";
|
||
String retStr = "Success";
|
||
|
||
Content tempContent = null;
|
||
|
||
int pageNum = 1;
|
||
String Ssql = null;
|
||
|
||
// ----新上传的图片信息
|
||
byte[] b = CodeTrans.String2Byte(image.replace(" ", "+"));
|
||
InputStream is = new ByteArrayInputStream(b);
|
||
tempContent = new Content();
|
||
tempContent.setInputStream(is);
|
||
|
||
tempContent.setDesc("");
|
||
String randomStr = String.valueOf(Math.random()).substring(2);
|
||
if (randomStr.length() < 8) {
|
||
randomStr += "0000000";
|
||
}
|
||
tempContent.setName(fileNameDateFormat.format(new Date()) + "_"
|
||
+ (randomStr.substring(0, 8)) + "." + imageType);
|
||
String uploadDocId = contentManager.save(tempContent,
|
||
ContentManager.FOLDER_IMAGE);
|
||
|
||
// 1.获取当前的pageNum---2.插入数据到ECM_PAGE中
|
||
JBOTransaction tx = null;
|
||
try {
|
||
tx = JBOFactory.createJBOTransaction();
|
||
BizObjectManager bomcl = JBOFactory.getBizObjectManager(ECM_PAGE.CLASS_NAME);
|
||
tx.join(bomcl);
|
||
BizObject bocl = null;
|
||
if(!"UnKnow".equals(ObjectNo)){
|
||
Ssql = "Select Max(PageNum) as v.max from o where ObjectNo=:ObjectNo and ObjectType=:ObjectType and TypeNo=:TypeNo";
|
||
bocl = bomcl.createQuery(Ssql).setParameter("ObjectNo",ObjectNo)
|
||
.setParameter("ObjectType",ObjectType)
|
||
.setParameter("TypeNo",TypeNo)
|
||
.getSingleResult(false);
|
||
if(bocl != null){
|
||
//实际第*张
|
||
pageNum = bocl.getAttribute("max").getInt()+1;
|
||
}else{
|
||
//初始化为第一张
|
||
pageNum = 1;
|
||
}
|
||
}
|
||
bocl = bomcl.newObject();
|
||
bocl.setAttributeValue("ObjectType", ObjectType);
|
||
bocl.setAttributeValue("ObjectNo", ObjectNo);
|
||
bocl.setAttributeValue("TypeNo", TypeNo);
|
||
bocl.setAttributeValue("DocumentId", uploadDocId);
|
||
bocl.setAttributeValue("PageNum", pageNum);
|
||
bocl.setAttributeValue("ModifyTime", insertPicDateFormat.format(new Date()));
|
||
bocl.setAttributeValue("ImageInfo", imageInfo);
|
||
bocl.setAttributeValue("OperateUser", userId);
|
||
bocl.setAttributeValue("OperateOrg", orgId);
|
||
bocl.setAttributeValue("Remark", "");
|
||
bomcl.saveObject(bocl);
|
||
tx.commit();
|
||
ARE.getLog().trace("插入一条记录: " + uploadDocId);
|
||
} catch (Exception e) {
|
||
try {
|
||
tx.rollback();
|
||
} catch (JBOException e1) {
|
||
ARE.getLog().error("回滚数据库连接出错", e);
|
||
}
|
||
ARE.getLog().trace("插入影像数据记录出错!",e);
|
||
} finally {
|
||
try {
|
||
tx.commit();
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error("关闭数据库连接出错", e);
|
||
}
|
||
}
|
||
ARE.getLog().trace(retStr);
|
||
return retStr;
|
||
}
|
||
|
||
/**
|
||
* 获取影像图片
|
||
* @param ids
|
||
* @return
|
||
*/
|
||
public String getImage( String ids) {
|
||
ARE.getLog().debug(ids);
|
||
if(StringX.isEmpty(ids) ||
|
||
!(ContentManagerAction.IsUseContentManager && ContentManagerAction.isConfCorrect ) ) return "";
|
||
String[] idArr = ids.split("\\|");
|
||
StringBuffer imageBuf = new StringBuffer();
|
||
for (int i = 0; i < idArr.length; i++) {
|
||
String singleImage = getSingleImage( idArr[i]);
|
||
if(! StringX.isEmpty(singleImage)){
|
||
imageBuf.append(singleImage);
|
||
if( i!=idArr.length-1 ) imageBuf.append("|");
|
||
}
|
||
}
|
||
return imageBuf.toString();
|
||
}
|
||
|
||
/**
|
||
* 获取单张影像图片
|
||
* @param id
|
||
* @return
|
||
*/
|
||
public String getSingleImage(String id){
|
||
String singleImageStr="";
|
||
if(StringX.isEmpty(id) ||
|
||
!(ContentManagerAction.IsUseContentManager && ContentManagerAction.isConfCorrect ) ) return "";
|
||
ContentManager contentManager = ContentManagerAction.getContentManager();
|
||
if(contentManager==null) return "";
|
||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||
byte[] bytes = new byte[10240];
|
||
|
||
Content content = contentManager.get(id);
|
||
if(content==null) return "";
|
||
|
||
InputStream is = content.getInputStream();
|
||
String desc = content.getDesc();
|
||
// 读取图片字节数组
|
||
try {
|
||
int k = -1;
|
||
while((k=is.read(bytes))>0){
|
||
bos.write(bytes, 0, k);
|
||
}
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}finally{
|
||
try {
|
||
is.close();
|
||
} catch (IOException e) { e.printStackTrace(); }
|
||
}
|
||
// 对字节数组Base64编码
|
||
BASE64Encoder encoder = new BASE64Encoder();
|
||
if(bos.size()>0) {
|
||
singleImageStr = encoder.encode(bos.toByteArray());
|
||
bos.reset();
|
||
}
|
||
String tempStr = encoder.encode(desc.getBytes());
|
||
//ARE.getLog().trace("*******encoder之前:"+desc+"\n之后:"+tempStr);
|
||
return singleImageStr+"^"+tempStr;
|
||
}
|
||
|
||
/**
|
||
* 保存影像页面图片到系统
|
||
* @param ObjectType
|
||
* @param ObjectNo
|
||
* @param TypeNo
|
||
* @param imageAndActiveXids
|
||
* @param userId
|
||
* @param orgId
|
||
* @return
|
||
*/
|
||
public String saveImage(String ObjectType, String ObjectNo, String TypeNo,
|
||
String imageAndActiveXids, String userId, String orgId) {
|
||
if(!(ContentManagerAction.IsUseContentManager && ContentManagerAction.isConfCorrect ) ) return "";
|
||
ContentManager contentManager = ContentManagerAction.getContentManager();
|
||
if(contentManager==null) return "";
|
||
String retStr = "";
|
||
//图片信息、IDs顺序字符串、需要删除的IDs、备注信息,这四项之间的分割符
|
||
String S1 = "\\|!";
|
||
//多个图片或多个id的分割符
|
||
String S2 = "\\|";
|
||
//infos中的每个id与info的分割符
|
||
String S3 = "\\^";
|
||
String[] imageStrArr = null;
|
||
String[] activeX_Ids = null, needDelIds=null, infos=null;
|
||
if(! StringX.isEmpty(imageAndActiveXids) ) {
|
||
String[] tempArr = imageAndActiveXids.split(S1, -1);
|
||
ARE.getLog().trace("要上传的图片(base64编码)*******"+ tempArr[0]);
|
||
ARE.getLog().trace("插件中各个图片的ID(或路径)的顺序*******"+ tempArr[1]);
|
||
ARE.getLog().trace("要删除的图片ID(或路径)*******"+ tempArr[2]);
|
||
ARE.getLog().trace("要设置的备注*******"+ tempArr[3]);
|
||
imageStrArr=tempArr[0].length()>0?tempArr[0].split(S2):null;
|
||
activeX_Ids = tempArr[1].split(S2, -1);
|
||
needDelIds = tempArr[2].length()>0?tempArr[2].split(S2):null;
|
||
infos = tempArr[3].length()>0?tempArr[3].split(S2):null;
|
||
}
|
||
Content tempContent = null;
|
||
JBOTransaction tx = null;
|
||
try {
|
||
tx = JBOFactory.createJBOTransaction();
|
||
} catch (JBOException e2) {
|
||
ARE.getLog().error("创建JBO事务出错!",e2);
|
||
}
|
||
BASE64Decoder decoder = new BASE64Decoder();
|
||
//----新上传的图片信息及其备注
|
||
for (int i = 0, j=0; imageStrArr!=null && activeX_Ids!=null && i<imageStrArr.length; i++) {
|
||
String[] arr = imageStrArr[i].split(S3, -1);
|
||
byte[] b = CodeTrans.String2Byte(arr[0].replace(" ", "+"));
|
||
InputStream is = new ByteArrayInputStream(b);
|
||
tempContent = new Content();
|
||
tempContent.setInputStream(is);
|
||
String tempImageInfo = "", tempImageFileType = "", tempLocalOrScan = "" ;
|
||
if(arr.length>=3){
|
||
try {
|
||
//备注
|
||
tempImageInfo = new String(decoder.decodeBuffer(arr[1].replace(" ", "+")));
|
||
//ARE.getLog().trace("************decoder之前:"+arr[1]+"\n之后:"+tempStr);
|
||
} catch (Exception e1) {
|
||
ARE.getLog().error("解码备注信息时,出错",e1);
|
||
tempImageInfo = arr[1];
|
||
}
|
||
try{
|
||
String[] strs = arr[2].split("@");
|
||
tempLocalOrScan = strs[0];
|
||
tempImageFileType = strs[1];
|
||
}catch(Exception e){
|
||
ARE.getLog().error("获取图片文件类型及图片是本地上传或从设备采集时,出错",e);
|
||
tempLocalOrScan = "scan";
|
||
tempImageFileType = "jpeg";
|
||
}
|
||
}
|
||
tempContent.setDesc(tempImageInfo);
|
||
String randomStr = String.valueOf(Math.random()).substring(2);
|
||
if(randomStr.length()<8){randomStr += "0000000"; }
|
||
tempContent.setName(fileNameDateFormat.format(new Date())+"_"+(randomStr.substring(0,8))+"."+tempImageFileType);
|
||
String uploadDocId = contentManager.save(tempContent, ContentManager.FOLDER_IMAGE );
|
||
//把新增的图片id更新回id顺序数组activeX_Ids中
|
||
for(int j2=j; j2<activeX_Ids.length; j2++){
|
||
if(StringX.isEmpty(activeX_Ids[j2])) {
|
||
activeX_Ids[j2] = uploadDocId;
|
||
j=j2+1;
|
||
break;
|
||
}
|
||
}
|
||
//---插入数据到ECM_PAGE中
|
||
try{
|
||
BizObjectManager bomcl = JBOFactory.getBizObjectManager(ECM_PAGE.CLASS_NAME);
|
||
tx.join(bomcl);
|
||
String SerialNo = DateX.format(new Date(), "yyyyMMddHHmmss") + String.valueOf(Math.random()).substring(2, 8);
|
||
BizObject bocl = bomcl.newObject();
|
||
bocl.setAttributeValue("ObjectType", ObjectType);
|
||
bocl.setAttributeValue("ObjectNo", ObjectNo);
|
||
bocl.setAttributeValue("TypeNo", TypeNo);
|
||
bocl.setAttributeValue("DocumentId", uploadDocId);
|
||
bocl.setAttributeValue("PageNum", i+1);
|
||
bocl.setAttributeValue("ModifyTime", insertPicDateFormat.format(new Date()));
|
||
bocl.setAttributeValue("ImageInfo", tempLocalOrScan);
|
||
bocl.setAttributeValue("OperateUser", userId);
|
||
bocl.setAttributeValue("OperateOrg", orgId);
|
||
bocl.setAttributeValue("Remark", tempImageInfo);
|
||
bocl.setAttributeValue("SerialNo", SerialNo);
|
||
bomcl.saveObject(bocl);
|
||
ARE.getLog().trace("插入一条记录: "+uploadDocId);
|
||
|
||
}catch(Exception e){
|
||
ARE.getLog().debug("插入影像数据记录出错!",e);
|
||
}
|
||
}
|
||
//----对原有图片的备注的修改,设置内容管理平台的备注
|
||
for(int info_i=0; infos!=null && info_i<infos.length; info_i++){
|
||
String[] info_arr = infos[info_i].split(S3);
|
||
String temp = null;
|
||
if(info_arr.length>=2){
|
||
try {
|
||
temp = new String(decoder.decodeBuffer(info_arr[1]));
|
||
} catch (Exception e1) {
|
||
temp = info_arr[1];
|
||
}
|
||
}
|
||
ARE.getLog().trace("设置文档对象(id:"+info_arr[0]+")的desc: "+ temp);
|
||
contentManager.setDesc(info_arr[0], info_arr.length>=2?temp:"");
|
||
//若未使用内容管理平台,则保存信息到ECM_PAGE表中的备注字段
|
||
if( contentManager instanceof DefaultContentManagerImpl){
|
||
try {
|
||
BizObjectManager bomcl = JBOFactory.getBizObjectManager(ECM_PAGE.CLASS_NAME);
|
||
tx.join(bomcl);
|
||
String Ssql = "update o set remark=:Remark where documentId=:DocumentId and objectNo=ObjectNo";
|
||
bomcl.createQuery(Ssql).setParameter("Remark",info_arr.length>=2?temp:"")
|
||
.setParameter("DocumentId",info_arr[0])
|
||
.setParameter("ObjectNo",ObjectNo).executeUpdate();
|
||
|
||
ARE.getLog().trace("更新一条记录:"+info_arr[0]+ "的备注信息:"+(info_arr.length>=2?temp:"") );
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
try {
|
||
BizObjectManager bomcl = JBOFactory.getBizObjectManager(ECM_PAGE.CLASS_NAME);
|
||
tx.join(bomcl);
|
||
for(int j=0; needDelIds!=null && j<needDelIds.length; j++){
|
||
contentManager.delete(needDelIds[j]);
|
||
}
|
||
String allIdStr = "";
|
||
for(int i =0; i<activeX_Ids.length; i++){
|
||
allIdStr = allIdStr +",'"+activeX_Ids[i]+"'";
|
||
}
|
||
|
||
if(needDelIds!=null && needDelIds.length>0){
|
||
String tempDelIds = "'";
|
||
for(String id : needDelIds){
|
||
tempDelIds = tempDelIds + id +"','";
|
||
}
|
||
tempDelIds = tempDelIds.substring(0, tempDelIds.length()-2);
|
||
String delSql = "delete from o where objectType=:ObjectType and objectNo=:ObjectNo and typeNo=:TypeNo and documentId in("+ tempDelIds +") ";
|
||
ARE.getLog().info(delSql);
|
||
bomcl.createQuery(delSql).setParameter("ObjectType",ObjectType)
|
||
.setParameter("ObjectNo",ObjectNo)
|
||
.setParameter("TypeNo",TypeNo).executeUpdate();
|
||
}
|
||
|
||
if(activeX_Ids!=null && activeX_Ids.length>0){
|
||
for(int i =0; activeX_Ids!=null && i<activeX_Ids.length; i++){
|
||
if(StringX.isEmpty(activeX_Ids[i])){
|
||
continue;
|
||
}
|
||
String Ssql = "update o set pageNum=:PageNum where documentId=:DocumentId and objectNo=:ObjectNo";
|
||
bomcl.createQuery(Ssql).setParameter("PageNum",i+1)
|
||
.setParameter("DocumentId",activeX_Ids[i])
|
||
.setParameter("ObjectNo",ObjectNo).executeUpdate();
|
||
|
||
ARE.getLog().trace("更新"+activeX_Ids[i]+ "的编号为"+(i+1));
|
||
retStr = retStr+"|"+activeX_Ids[i];
|
||
}
|
||
}
|
||
tx.commit();
|
||
} catch (Exception e) {
|
||
try {
|
||
tx.rollback();
|
||
} catch (JBOException e1) {
|
||
ARE.getLog().error("事务回滚出错!",e);
|
||
}
|
||
ARE.getLog().error("更新DocId出错",e);
|
||
return "";
|
||
} finally {
|
||
try {
|
||
if(tx!=null){
|
||
tx.commit();
|
||
}
|
||
} catch (JBOException e) {
|
||
ARE.getLog().error("关闭数据库连接出错",e);
|
||
}
|
||
}
|
||
retStr = retStr.length()>0?retStr.substring(1):"";
|
||
ARE.getLog().trace(retStr);
|
||
return retStr;
|
||
}
|
||
}
|
||
|