`

Java操作dll等active控件的使用,即jacob包的使用

    博客分类:
  • Java
阅读更多

简要说明:

jacob.dll 放到 C:\WINDOWS\system32 下
jacob.dll 放到 %JAVA_HOME%\bin 下,这个比较关键,决定你项目启动时候是否报错的问题
jacob.jar 放到应用开发项目的 lib里面

我使用的Jacob版本是jacob_1.9

 

以下是我在实际开发中写的一个实现类

package cn.com.ivisions.management;

import java.io.*;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ivisions.dipper2.Dipper;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import cn.com.ivisions.management.DocManage;

public class COMBridge 
{
	private static final Log log = LogFactory.getLog(COMBridge.class);
	lugb.util.NamedIndexedHashMap constants = Dipper.getConfigInfo().getConstants();
	 //方正公章服务器地址
	
	private String Founder_Host = constants.get("SEAL_SERVER").toString();

	  //判断CEB是否盖章文件
	  public boolean isCertifiedCEB(String fileurl)
	  {
	  	try
	  	{
		  	String test1=getCEBID(fileurl);
		  	if(test1==null)
		  	{
		  		return false;
		  	}
		  	if(getCEBID(fileurl).equals(getCEBID(fileurl)))
		  	{
		  		return true;
		  	}
		  	else
		  	{
		  		return false;
		  	}
	  	}
	  	catch(Exception e)
	  	{
	  		return false;
	  	}
	  }
	  
	  //得到CEB的UUID
	  public String getCEBID(String fileurl)
	  {
	  	try
	  	{
	  		ActiveXComponent objArchSend = new ActiveXComponent("PrintURLChangeSvr.ChangePrintURL");
	    	Dispatch myCom = (Dispatch) objArchSend.getObject();
	    	String returnString = Dispatch.call(myCom, "ChangeURL",fileurl, "http://"+Founder_Host).getString();
	    	//objArchSend.release();
	    	return returnString;
	 	}
	 	catch(Exception e)
	 	{
	 		return "";
	 	}
	  }
	  //生成XML jason
	  public boolean getPrintXml(String strXml,String XmlUrl)
	  {
	  	ActiveXComponent objArchSend = new ActiveXComponent("StampPubCom.StampPubFuncCom");
		Dispatch myCom = (Dispatch) objArchSend.getObject();
	  	try
	  	{ 	
		    Dispatch.call(myCom, "BSTR2File",strXml,XmlUrl).getString();
		//    System.out.println(Dispatch.call(myCom, "GetErrorMessage").getString());
		    //objArchSend.release();
		    
		    return true;
	 	}
	 	catch(Exception e)
	 	{
	 		System.out.println(Dispatch.call(myCom, "GetErrorMessage").getString());
	 		e.printStackTrace();
	 		return false;
	 	}
	  }
	  //上传公文分数XML到公章服务器
	  public boolean uploadCountXml(String XmlUrl)
	  {
	  	ActiveXComponent objArchSend = new ActiveXComponent("LocalSealStamp.ConductFile");
		Dispatch myCom = (Dispatch) objArchSend.getObject();
	  	try
	  	{ 	
		    Dispatch.call(myCom, "HTTPUploadFile",XmlUrl, "http://"+Founder_Host+"/stampserver/extend/interfaces/printlic2db.aspx").getString();
		//    System.out.println(Dispatch.call(myCom, "GetErrorMessage").getString());
		    //objArchSend.release();
		    
		    return true;
	 	}
	 	catch(Exception e)
	 	{
	 		System.out.println(Dispatch.call(myCom, "GetErrorMessage").getString());
	 		e.printStackTrace();
	 		return false;
	 	}
	  }
	  //上传XML到OA服务器
	  public boolean uploadFileforXml(String XmlUrl,String serverURL)
	  {
	  	ActiveXComponent objArchSend = new ActiveXComponent("ASPCom.PostRecv");
		Dispatch myCom = (Dispatch) objArchSend.getObject();
	  	try
	  	{ 	
		    Dispatch.call(myCom, "HTTPUploadFile",XmlUrl, serverURL).getString();
		    return true;
	 	}
	 	catch(Exception e)
	 	{
	 		System.out.println(Dispatch.call(myCom, "GetErrorMessage").getString());
	 		e.printStackTrace();
	 		return false;
	 	}
	  }
	  //文件上传到服务器的方法
	  public boolean SendFileEx(String strURL,String strRetFileName)
	  {
	  	try
	  	{
	//	  System.runFinalizersOnExit(true);
	
		    ActiveXComponent objArchSend = new ActiveXComponent("ASPCom.PostRecv");
		    Dispatch myCom = (Dispatch) objArchSend.getObject();
		    Variant vResult = Dispatch.call(myCom, "SendFile",
		                                    new Variant(strURL),
		                                    new Variant(strRetFileName)
											);
	
		    Variant vResult1 = Dispatch.call(myCom, "GetErrorMessage");
	
			int nRes = vResult.getInt();
		    String strError = vResult1.getString();
		    
		    //objArchSend.release();
		    
		//    if(nRes == SysConfig.RET) return true;
		
			if(strError != null && strError.equals("S_OK")) return true;
		    
		    return false;
	 	}
	 	catch(Exception e)
	 	{
	 		
				e.printStackTrace();
		
	 		return false;
	 	}
	  } 
	  //JAVA实现的文化下载 Jason
	  public void downloadFile(String urls,String strRetFileName) throws IOException{
		  URL url=new URL(urls);
		  InputStream in= url.openConnection().getInputStream();
		  File file=new File(strRetFileName);
		  FileOutputStream out=new FileOutputStream(file,true);
		  //计数器
		  int counter=0;
		  //开始读
		  int ch;
		  byte[] buffer=new byte[1024];
		  while((ch=in.read(buffer))!=-1){
		  out.write(buffer,0,ch);
		  counter+=ch;
		  }
		  out.flush();
		  in.close();
		  out.close();

		  }
	
	  //文件下载到本地的方法
	  public boolean HTTPDownloadFile(String strURL,String strRetFileName)
	  {
		  	try
		  	{
		//	  System.runFinalizersOnExit(true);
		
			    ActiveXComponent objArchSend = new ActiveXComponent("ASPCom.PostRecv");
			    Dispatch myCom = (Dispatch) objArchSend.getObject();
			    Variant vResult = Dispatch.call(myCom, "HTTPDownloadFile",
			                                    new Variant(strURL),
			                                    new Variant(strRetFileName)
												);
		
			    Variant vResult1 = Dispatch.call(myCom, "GetErrorMessage");
		
				int nRes = vResult.getInt();
			    String strError = vResult1.getString();
			    
			    //objArchSend.release();
			    
			//    if(nRes == SysConfig.RET) return true;
			
				if(strError != null && strError.equals("S_OK")) return true;
			    
			    return false;
		 	}
		 	catch(Exception e)
		 	{
		 		
					e.printStackTrace();
			
		 		return false;
		 	}
	  }
	  //删除本机的各种临时文件 jason
	  public void deleteTempFile(String fileUrl)
	  {
	  	ActiveXComponent objArchSend = new ActiveXComponent("StampPubCom.StampPubFuncCom");
		Dispatch myCom = (Dispatch) objArchSend.getObject();
	  	try
	  	{ 	
		    Dispatch.call(myCom, "DeleteFile",fileUrl).getString();
	 	}
	 	catch(Exception e)
	 	{
	 		System.out.println(Dispatch.call(myCom, "GetErrorMessage").getString());
	 		e.printStackTrace();
	 	}
	  }
	  public boolean sendXmlToFounder(int docId,String serverPath,String serverProPath){
		  try{
			  COMBridge com=new COMBridge();
			  DocManage docManage=new DocManage();
			  //打印份数
			  String printNum = constants.get("PRINT_NUM").toString();
			  //文档后缀名
			  String sealGwExtend = constants.get("SEAL_GW_EXTEND").toString();
			  //默认下载路径
			  String download_dir = constants.get("DOWNLOAD_DIR").toString();
			  //获得主送和抄送单位
			  String receiveDept = docManage.getReceiveDepts(docId);
			  String printNums = "";
				for(int i=0;i<receiveDept.split(";").length;i++){
					printNums += printNum+";";
				}
				if(printNums.length()>0) printNums = printNums.substring(0,printNums.length()-1);
				
				//获得公文附件是ceb的附件
				String[] attachFile =  docManage.getAttachment(docId,sealGwExtend);
				for(int i=0;i<attachFile.length;i++){
					  String cebfileName=attachFile[i];
					  String fileurl=download_dir+cebfileName;
					  String remoteFileUrl=serverPath+"/files/upload/"+cebfileName;
					  //下载ceb文件到本地
					  com.downloadFile(remoteFileUrl,fileurl);
					  //获取CEBID
					  String cebId=com.getCEBID(fileurl);
					  //拼装方正公章接口的XML
					  String strXml = "<?xml version = \"1.0\" encoding=\"gb2312\"?>";
					  strXml +="<Doc>";
					  strXml +="<DocumentID>"+cebId+"</DocumentID>";              
					  strXml +="<Receivers>"+receiveDept+"</Receivers>";     
					  strXml +="<PrnNums>"+printNums+"</PrnNums>";                     
					  strXml +="<SendType>1</SendType>";                          
					  strXml +="<Count>"+receiveDept.split(";").length+"</Count>";                               
					  strXml +="</Doc>";
					  log.info("strXml==="+strXml);
					  String xmlFileName=docId+"["+i+"]"+".xml";
					  String xmlUrl=download_dir+xmlFileName;
					  //生成本地的XML
					  boolean flag1=com.getPrintXml(strXml,xmlUrl);
					  String strURL="http://"+Founder_Host+"/stampserver/extend/interfaces/PrintLic2DB.aspx";
					  //上传本地的XML到方正公章服务器
					  boolean flag2=com.SendFileEx(strURL, xmlUrl);
					  String strOAURL=serverProPath+"/apabi/upload.jsp?fileName="+xmlFileName;
					  //上传本地的XML到OA服务器
					  boolean flag3=com.uploadFileforXml(xmlUrl,strOAURL);
					  //删除本机的各种临时文件包括下载的CEB和生成的XML
					  deleteTempFile(fileurl);
					  deleteTempFile(xmlUrl);
				}
				return true;
			  }catch(Exception ex){
				  ex.printStackTrace();
				  return false;
			  }
	  } 
}












 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics