Today is the Qixi Festival, it seems to have nothing to do with the author. The author also has a favorite person, but the author has not said broken.I want to give her a princess-like life, but the author is not good enough, and I am afraid that time is not allowed.However, the author's emotions and sorrows seem to be related to her smile.好了,每天闲扯一下很开心。
今天,我们继续讲。利用Hessian跨Web服务器推送文件数据包和跨Web服务器推送数据库数据。
Hessian跨Web服务推送文件数据包
推送一个文件数据包xxx.zip给另一个Web服务。
1、我们在被调用端定义一个接口
/**

远端推送文件数据@param bs
*/
public void acceptMailAnaexe(byte[] bs);
2、被调用端接口的实现类
/**这里我们接收推送的文件然后,将文件写在电脑的C盘

当然,你也可以替换为你的服务器路径
*/
public void acceptMailAnaexe(byte[] bs) {
System.err.println(bs);
try {
FileOutputStream output = new FileOutputStream("C:\oadocMD5.zip");
output.write(bs);
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3、同样,我们在调用端定义相同的接口和接口的实现
4、下面我们开始推送数据包
为了方便起见,我直接从本地磁盘中获取的zip文件包,然后将文件包转为二进制数据流,将数据流发给被调用端。

/*推送文件*/ byte[] data = null; FileInputStream input = null; try { //这里可以放置任何的文件,也可以是代码生成的文件等等,这里方便起见,直接从磁盘中拿一个文件 input = new FileInputStream(new File("C:\\oadocMD5.zip")); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int numbufRead = 0; while ((numbufRead = input.read(buf)) != -1) { output.write(buf, 0, numbufRead); } data = output.toByteArray(); acceptMail = (AcceptMailService)factory.create(AcceptMailService.class, url.toString()); acceptMail.acceptMailAnaexe(data); output.close(); input.close(); System.err.println(data); } catch (IOException e) { e.printStackTrace(); }

运行上面的推送代码之后,你会在被调用端的相应磁盘位置,看到:


恭喜你,文件数据包推送成功!
Hessian推送数据库数据
接下来,我们将技术升级,直接操作调用端的数据库。推送的内容直接写入调用端的数据库。
1、首先我们操作的实体类,在调用端和被调用端,都应该存在且相同,实体类的xml配置什么的自不必说。
2、数据库对应的实体类,必须序列化
我们定义一个简单的数据库表,里面包含两个字段Id(主键)与content(内容)
我们创建该表的实体类对象:

public class AboutMailContent implements Serializable {private String uuid;// 内容idprivate String content;// 内容

public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
3、接下来我们定义调用端和被调用端的接口和接口的实现

//接口public void sendChiefBur(String text);

//接口的实现@Overridepublic void sendChiefBur(String text) { TransactionCache tx = null; tx = new TransactionCache(); String uuid = UuidUtil.getUuid(); mailContent.setUuid(uuid); mailContent.setContent(text); tx.save(mailContent); tx.commit();}

4、配置完成,现在开始操作被调用端的数据库,往数据库里插入一条数据。

String url = “http://xx.xx.xxx.xx:1546/20180817/HelloHessian”; HessianProxyFactory factory = new HessianProxyFactory(); try { 接口名about = (接口名)factory.create(接口名.class, url.toString()); about .sendChiefBur("这里是塞入数据库的内容"); } catch (MalformedURLException e) { e.printStackTrace(); }

在被调用端的数据库中查找一下:

插入成功!