附件、正文圖片隨Web服務打包傳輸的解決思路

目的:在與第三方系統之間的數據交換,一般數據的格式爲以XML的形式進行傳輸;但在實際應用中,一條信息中,可能包含附件、html正文,且html正文中包含圖片;那麼勢必也要把這些數據也要進行傳輸,這樣才能保障一條信息的完整性。

解決思路

1、所有的圖片、附件都以二進制進行傳輸;

2、要記錄html正文中圖片的對應位置;因爲在html正文中,圖片一般都是以img以上存在,並無實際數據;

3、建議以以下格式進行規劃;

<Data>
         <Item>
                   <Record>    //記錄信息,主要包括數據庫字段信息
                   </Record>
                   <Images>
                            <Image ImgPlaceholder=”Guid”>   // ImgPlaceholder:正文中的佔位符,表達方式{{guid}}
                                               //二進制數據
                            </Image>
                            <Image>
                                               //二進制數據
                            </Image>
                            …
                   <Images>
                   <Attachments>
                            <Attachment>
                                               //二進制數據
                            </Attachment>
                            <Attachment>
                                               //二進制數據
                            </Attachment>
                            ….
                   </Attachments>
         </Item>
         <Item>
         </Item>
</Data>


 

編程思路:

1、獲取正文內容;遍歷所有img標籤 url;

2、將圖片轉成二進制

3、遍歷圖片src字符串 string[]

 

 

參考:

1、通過http訪問圖片,並將圖片專程二進制;

 publicstatic byte[] getByte(String imgUrl) {

        try {
           URL url = new URL(imgUrl);
           HttpURLConnection connection = (HttpURLConnection) url
                  .openConnection();

           // connection.getResponseCode();
           connection.setConnectTimeout(10000);
           BufferedImage image = ImageIO.read(connection.getInputStream());

           ByteArrayOutputStream out = newByteArrayOutputStream();
           ImageIO.write(image, "jpg", out);

           return out.toByteArray();
        } catch(MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        } catch(IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }
        returnnull;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章