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;i0){ 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=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=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 && j0){ 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 && i0?retStr.substring(1):""; ARE.getLog().trace(retStr); return retStr; } }