使用SOAP消息发送图像

时间:2016-04-29 12:17:20

标签: java xml soap encoding

我想将我的JPG添加到我的SOAP请求中,我想要转换它。

当我使用SoapUI发送相同的soapMessage(它可以工作)时,我可以看到我的图像的编码是这样的:

[0xff的] [0xd8] [0xff的] [0xe0的] [为0x0] [0×10] JFIF [为0x0] [为0x1] [为0x1] [为0x1] [为0x0] H [为0x0] H [为0x0] [为0x0] [0xff的] [0xe1] [0x19] tExif [为0x0] [为0x0] MM [为0x0] * [为0x0] [为0x0] [为0x0] [0x8中] [为0x0] [位于0xC] [为0x1] [为0x0] [为0x0] [为0x4] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x0] [位于0xC] [将0xC0] [为0x1] [为0x1] [为0x0] [为0x4] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x0] [0x7的],[为0x1] [0xf] [为0x0] [0X2] [为0x0] [为0x0] [为0x0] [0x8中] [为0x0] [为0x0] [为0x0] [0x9e] [为0x1] [0×10] [为0x0] [0X2] [为0x0] [为0x0] [为0x0] [0x8中] [为0x0] [为0x0] [为0x0] [0xa6] [为0x1] [0×12] [为0x0] [0x3] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x1] [为0x0] [为0x0] [为0x1] [0X1A] [为0x0] [0x5的] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x0] [为0x0] [0xae ] [为0x1] [0x1b] [为0x0] [0x5的] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x0] [为0x0] [0xb6] [为0x1]([为0x0] [0x3] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [0X2] [为0x0] [为0x0] [为0x1] 1 [为0x0] [0X2] [为0x0] [为0x0] [为0x0] [位于0xC] [为0x0] [为0x0] [为0x0] [0xbe] [为0x1] 2 [为0x0] [0X2] [为0x0] [为0x0] [为0x0] [0×14] [为0x0] [为0x0] [为0x0] [0xca] [0X2] [0×13] [为0x0] [0x3 ] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x1] [为0x0] [为0x0] [0×87] i的[为0x0] [为0x4] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x0] [为0x0] [写0xDE] [为0x0] [为0x0] [0x3] Bsamsung [为0x0] SM-P600 [为0x0] [为0x0] [为0x0] [为0x0] H [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x0] [为0x0] H [为0x0] [为0x0] [为0x0] [为0x1] P600XXUDOJ3 [为0x0] 20 16:04:08 14:32:04 [0x0] [0x0] [0x1c] [0x82] [0x9a] [0x0] [0x5] [0x0] [0x0] [0x0] [0x1] [0x0] [0x0] [ 0X2] 4 [在0x82] [0x9d] [为0x0] [0x5的] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] [为0x0] [0X2]的百分比抑制率均为0x88]“[为0x0] [0x3] [为0x0 ] [为0x0] [为0x0] [为0x1] [为0x0] [0X2] [为0x0] [为0x0] [均为0x88]'[为0x0] [0x3] [为0x0] [为0x0] [为0x0] [为0x1] [为0x0] P [为0x0 ] [为0x0] [0×90] [为0x0] [为0x0] [0x7的] [为0x0] [为0x0] [为0x0] [为0x4] 0220 [0×90] [0x3] [为0x0] [0X2] [为0x0] [为0x0] [为0x0] [0×14] [为0x0] [为0x0] [0X2] d [0×90] [为0x4] [为0x0] [0X2] [为0x0] [为0x0] [为0x0] [0×14] [为0x0] [为0x0] [0X2] X [0x92] [为0x1] [为0x0] [\ n]的“

所以我认为在将我的图像(JPG)添加到我的消息之前,我应该完全按照我的图像进行编码。

我怎么能像这样编码我的图像呢?

1 个答案:

答案 0 :(得分:0)

要使用SOAP发送图像,您必须使用以下格式之一对其进行编码:

  • base64Binary的
  • SwA - 带附件的SOAP
  • MTOM

然后在您的SOAP请求中,您可以使用xsi:type声明您的编码类型:

 <submitClaim>
  <accountNumber>5XJ45-3B2</accountNumber>
  <eventType>accident</eventType>
  <image imageType="jpg" xsi:type="base64binary">
   4f3e9b0...(rest of encoded image)
  </image>
 </submitClaim>

在您的java代码中,您必须阅读BufferedImage并将其转换为字符串,然后将其添加到您的SOAP请求中:

public static String encodeToString(BufferedImage image, String type) {  
        String imageString = null;  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  

        try {  
            ImageIO.write(image, type, bos);  
            byte[] imageBytes = bos.toByteArray();  

            BASE64Encoder encoder = new BASE64Encoder();  
            imageString = encoder.encode(imageBytes);  

            bos.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return imageString;  
    }