溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

跨Web服務(wù)器推送文件數(shù)據(jù)包以及直接推送數(shù)據(jù)庫數(shù)據(jù)

發(fā)布時(shí)間:2020-07-08 08:22:24 來源:網(wǎng)絡(luò) 閱讀:812 作者:Red_Ant_hoyl 欄目:開發(fā)技術(shù)

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.好了,每天閑扯一下很開心。
今天,我們繼續(xù)講。利用Hessian跨Web服務(wù)器推送文件數(shù)據(jù)包和跨Web服務(wù)器推送數(shù)據(jù)庫數(shù)據(jù)。
Hessian跨Web服務(wù)推送文件數(shù)據(jù)包
推送一個(gè)文件數(shù)據(jù)包xxx.zip給另一個(gè)Web服務(wù)。
1、我們?cè)诒徽{(diào)用端定義一個(gè)接口
/**

  • 遠(yuǎn)端推送文件數(shù)據(jù)
  • @param bs
    */
    public void acceptMailAnaexe(byte[] bs);
    2、被調(diào)用端接口的實(shí)現(xiàn)類
    /**這里我們接收推送的文件
  • 然后,將文件寫在電腦的C盤
  • 當(dāng)然,你也可以替換為你的服務(wù)器路徑
    */
    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、同樣,我們?cè)谡{(diào)用端定義相同的接口和接口的實(shí)現(xiàn)
    4、下面我們開始推送數(shù)據(jù)包
    為了方便起見,我直接從本地磁盤中獲取的zip文件包,然后將文件包轉(zhuǎn)為二進(jìn)制數(shù)據(jù)流,將數(shù)據(jù)流發(fā)給被調(diào)用端。

    /*推送文件*/
        byte[] data = null;
        FileInputStream input = null;
        try {
            //這里可以放置任何的文件,也可以是代碼生成的文件等等,這里方便起見,直接從磁盤中拿一個(gè)文件
            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();
        }
    運(yùn)行上面的推送代碼之后,你會(huì)在被調(diào)用端的相應(yīng)磁盤位置,看到:

    跨Web服務(wù)器推送文件數(shù)據(jù)包以及直接推送數(shù)據(jù)庫數(shù)據(jù)
    恭喜你,文件數(shù)據(jù)包推送成功!
    Hessian推送數(shù)據(jù)庫數(shù)據(jù)
    接下來,我們將技術(shù)升級(jí),直接操作調(diào)用端的數(shù)據(jù)庫。推送的內(nèi)容直接寫入調(diào)用端的數(shù)據(jù)庫。
    1、首先我們操作的實(shí)體類,在調(diào)用端和被調(diào)用端,都應(yīng)該存在且相同,實(shí)體類的xml配置什么的自不必說。
    2、數(shù)據(jù)庫對(duì)應(yīng)的實(shí)體類,必須序列化
    我們定義一個(gè)簡(jiǎn)單的數(shù)據(jù)庫表,里面包含兩個(gè)字段Id(主鍵)與content(內(nèi)容)
    我們創(chuàng)建該表的實(shí)體類對(duì)象:

    public class AboutMailContent implements Serializable {
    private String uuid;// 內(nèi)容id
    private String content;// 內(nèi)容

    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、接下來我們定義調(diào)用端和被調(diào)用端的接口和接口的實(shí)現(xiàn)

    //接口
    public void sendChiefBur(String text);
    //接口的實(shí)現(xiàn)
    @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、配置完成,現(xiàn)在開始操作被調(diào)用端的數(shù)據(jù)庫,往數(shù)據(jù)庫里插入一條數(shù)據(jù)。

    String url = “http://xx.xx.xxx.xx:1546/20180817/HelloHessian”;
        HessianProxyFactory factory = new HessianProxyFactory();
        try {
            接口名about = (接口名)factory.create(接口名.class, url.toString());
            about .sendChiefBur("這里是塞入數(shù)據(jù)庫的內(nèi)容");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    在被調(diào)用端的數(shù)據(jù)庫中查找一下:
    跨Web服務(wù)器推送文件數(shù)據(jù)包以及直接推送數(shù)據(jù)庫數(shù)據(jù)

插入成功!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI