您的位置:首页 > 编程语言 > Java开发

java 方法的远程调用rmi

2011-05-24 15:10 531 查看
项目需要,crystal reports export to pdf,总算完成了,但新的问题又来了,因为export function用的是全新的crystal report的包,把 包导到项目里,和旧版本的包冲突,一时无奈,只能写成独立的方法,然后在项目工程里调用外部的JAR里的方法,先来个远程调用吧~~

个人觉得蛮有意思,写出来和大家分享一下:

首先,建个java工程,然后,写个接口文件

***************************接口java(注意红字,所有的都要)**********************

package iExpOption;

import java.io.ByteArrayInputStream;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Map;

public interface IExpOptionFunction extends Remote {

public void dbLogin(String username,String password) throws RemoteException;
public ByteArrayInputStream returnPDFStream() throws RemoteException;
public ByteArrayInputStream returnWordStream() throws RemoteException;
public byte[] returnPDFStream(String rptPath, Map param) throws RemoteException;
public ByteArrayInputStream returnPDFStream(String rptPath, Map param,String dbusername,String dbpassword) throws RemoteException;
public String test() throws RemoteException;

}

*********************方法的实现************************************
package expOption;

import iExpOption.IExpOptionFunction;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Iterator;
import java.util.Map;

import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;

public class ExpOptionFunction extends UnicastRemoteObject implements
IExpOptionFunction,Serializable{
private String dbusername;
private String dbpassword;
transient private ByteArrayInputStream bais;

public ExpOptionFunction() throws RemoteException {
}

public void dbLogin(String username, String password)
throws RemoteException{
this.dbpassword=password;
this.dbusername=username;

}

public ByteArrayInputStream returnPDFStream() throws RemoteException{
return null;
}

public byte[] returnPDFStream(String rptPath, Map param)
throws RemoteException{
ByteArrayInputStream input=null;
byte[] buffer = new byte[1444];

try {
ReportClientDocument repClientDoc=new ReportClientDocument();
repClientDoc.setReportAppServer(ReportClientDocument.inprocConnectionString);
repClientDoc.open(rptPath, ReportExportFormat._PDF);
repClientDoc.getDatabaseController().logon(this.dbusername, this.dbpassword);
if(param!=null)
{
Iterator it=param.keySet().iterator();
while(it.hasNext())
{
String key=(String)it.next();
Object value=param.get(key);
repClientDoc.getDataDefController().getParameterFieldController().setCurrentValue("", key,value);
}
}
input = (ByteArrayInputStream) repClientDoc.getPrintOutputController().export(ReportExportFormat.PDF);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = input.read(b, 0, b.length)) != -1) {
baos.write(b, 0, len);
}
buffer = baos.toByteArray();/*远程调用时,要传递inputstream或outputstream时,因为不能序列化,会报错,所以如果想传递,就再传递之前,把它们转成byte[]再传递。*/
repClientDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
return buffer;
}

public ByteArrayInputStream returnPDFStream(String rptPath, Map param,
String username, String password) throws RemoteException{

try {
ReportClientDocument repClientDoc=new ReportClientDocument();
repClientDoc.setReportAppServer(ReportClientDocument.inprocConnectionString);
repClientDoc.open(rptPath, ReportExportFormat._PDF);
repClientDoc.getDatabaseController().logon(username, password);
if(param!=null)
{
Iterator it=param.keySet().iterator();
while(it.hasNext())
{
String key=(String)it.next();
Object value=param.get(key);
repClientDoc.getDataDefController().getParameterFieldController().setCurrentValue("", key,value);
}
}
ByteArrayInputStream input = (ByteArrayInputStream) repClientDoc.getPrintOutputController().export(ReportExportFormat.PDF);
repClientDoc.close();
} catch (ReportSDKException e) {
e.printStackTrace();
}
return bais;

}

public ByteArrayInputStream returnWordStream() throws RemoteException{
return null;
}

public String test() throws RemoteException{
return "this is test function!!!";
}

}
*********************************service和端口的绑定********************************************

package crExpPdfService;

import iExpOption.IExpOptionFunction;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import expOption.ExpOptionFunction;

public class CRexppdfSer {

public static void main(String[] args) {

try {
LocateRegistry.createRegistry(2011); //绑定端口
IExpOptionFunction ipf=new ExpOptionFunction();
Naming.rebind("//localhost:2011/CRPDFex",ipf); //绑定实例
} catch (Exception e) {

e.printStackTrace();
}

}

}

******************************************************************************************

把这个程序运行起来

然后,开始重要的远程调用了哦

首先,把接口文件 IExpOptionFunction.java拷贝到调用方的项目里

然后在调用方建立一个新的类,注意,远程调用时,要传递inputstream或outputstream时,因为不能序列化,会报错,所以如果想传递,就再传递之前,把它们转成byte[]再传递。

*********************************远程调用************************************

package comdom;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

import iExpOption.IExpOptionFunction;

public class CrexpPDFClient {

static IExpOptionFunction ieof=null;
static
{
try {
ieof=(IExpOptionFunction) Naming.lookup("//IP:2011/CRPDFex");
} catch (MalformedURLException e) {

e.printStackTrace();
} catch (RemoteException e) {

e.printStackTrace();
} catch (NotBoundException e) {

e.printStackTrace();
}
}
public static void expFuntion(String dbusername,String dbpassword,Map param) {

try {
ieof.dbLogin(dbusername, dbpassword);

byte[] bytes=ieof.
returnPDFStream("C://XXXXX//XXXX.rpt", param);
InputStream input=new ByteArrayInputStream(bytes);

int bytesum = 0;
int byteread = 0;
FileOutputStream fs = new FileOutputStream("C:/test.pdf");
byte[] buffer = new byte[1444];
while ( (byteread = input.read(buffer)) != -1)
{
bytesum += byteread;

fs.write(buffer, 0, byteread);
}
input.close();
fs.close();

} catch (RemoteException e) {

e.printStackTrace();
} catch (Exception e) {

e.printStackTrace();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: