我正在调用一个SOAP 网络服务,它返回一个图像作为 SOAP 附件,即
<Image>
<xop:Include href="cid:10ee9.." >
</Image>
我会在附件中得到这张图片,即
AttachmentPart attachment = (AttachmentPart)iterator.next();
- 需要知道如何将此附件传递给 JSP 以显示?
- 还需要将其存储在数据库中,所以 BLOB 类型就可以了吗?
附件是否需要转换或将按原样存储在数据库中?
我正在调用一个SOAP 网络服务,它返回一个图像作为 SOAP 附件,即
<Image>
<xop:Include href="cid:10ee9.." >
</Image>
我会在附件中得到这张图片,即
AttachmentPart attachment = (AttachmentPart)iterator.next();
附件是否需要转换或将按原样存储在数据库中?
服务响应可能包含图片作为 base64 或作为信封外的附件。示例解码 base64:
public static BufferedImage decodeToImage(String imageString) {
BufferedImage image = null;
byte[] imageByte;
try {
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
JAVA 已经有一个用于处理带有附件的 SOAP 的 API。
SOAPMessage response = connection.call(requestMessage, serviceURL);
Iterator attachmentsIterator = response.getAttachments();
while (attachmentsIterator.hasNext()) {
AttachmentPart attachment = (AttachmentPart) attachmentsIterator.next();
//do something with attachment
}
只要您的图像存储在您的服务器可以提供的文件夹中,您只需将元素添加到您的 JSP 页面并让它们的 'src' 属性保存图像的路径。
例如,假设您将图像存储在一个名为“images”的文件夹中,该文件夹可以由您的服务器提供服务。您必须在 JSP 页面中插入一个元素,例如:
<img src="http://localhost:8080/images/image_name.jpg" /img>
另一种方法来做到这一点
你可以这样做
try{
String fileName = request.getParameter("image");
FileInputStream fis = new FileInputStream(new File("d:\\"+fileName));
BufferedInputStream bis = new BufferedInputStream(fis);
response.setContentType(contentType);
BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
for (int data; (data = bis.read()) > -1;) {
output.write(data);
}
}
catch(IOException e){
}finally{
// close the streams
}
要传递图像路径,您可以像这样使用 src
<img src="<%=request.getParameter("image")%>">