温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

跨Web服务器推送文件数据包以及直接推送数据库数据

发布时间:2020-07-08 08:22:24 来源:网络 阅读:803 作者:Red_Ant_hoyl 栏目:开发技术

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();
        }
    运行上面的推送代码之后,你会在被调用端的相应磁盘位置,看到:

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

    public class AboutMailContent implements Serializable {
    private String uuid;// 内容id
    private 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);
    //接口的实现
    @Override
    public 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();
        }

    在被调用端的数据库中查找一下:
    跨Web服务器推送文件数据包以及直接推送数据库数据

插入成功!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI