📄 saajclient.java
字号:
package com.j2ee14.ch19;
import javax.xml.soap.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import com.sun.image.codec.jpeg.*;
import javax.swing.JFrame;
/**
*接收SOAP附件的客户端
*/
public class SAAJClient extends JFrame
{
String endPointURLString = "http://localhost:8080/saaj/AttachmentServlet";
public static void main(String[] args)throws Exception
{
new SAAJClient();
}
public SAAJClient()
{
super("test image transport");
setSize(800, 600);
setVisible(true);
}
/**
*从Web服务获得图像
*/
public Image getImageFromWebservice()
{
try
{
SOAPConnectionFactory soapConnectionFactory =
javax.xml.soap.SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection =
soapConnectionFactory.createConnection();
MessageFactory messageFactory =
MessageFactory.newInstance();
SOAPMessage soapMessage =
messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope requestEnvelope =
soapPart.getEnvelope();
SOAPBody body = requestEnvelope.getBody();
SOAPBodyElement operation = body.addBodyElement
(requestEnvelope.createName("getImage"));
javax.xml.soap.SOAPElement element =
operation.addChildElement(requestEnvelope.createName("test"));
operation.addChildElement("testgetImage").addTextNode("getImagebyAttachment_Test");
javax.xml.soap.SOAPMessage returnedSOAPMessage =
soapConnection.call(soapMessage, endPointURLString);
System.out.println("========");
java.util.Iterator it = returnedSOAPMessage.getAttachments();
BufferedImage image=null ;
while (it.hasNext()) {
AttachmentPart attachment = (AttachmentPart)it.next();
printAttachmentInfo(attachment);
image= decodeImage(attachment.getDataHandler().getInputStream());
}
return image;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
//把附件解码成Image
public BufferedImage decodeImage(java.io.InputStream in)throws java.io.IOException
{
BufferedImage image=null ;
image = new BufferedImage(600,800, BufferedImage.TYPE_INT_RGB);
com.sun.image.codec.jpeg.JPEGImageDecoder dencoder =JPEGCodec.createJPEGDecoder(in);
image=dencoder.decodeAsBufferedImage();
return image;
}
//打印附件的一些信息
private void printAttachmentInfo( AttachmentPart attachment)throws Exception
{
Object content = attachment.getContent();
/**
*打印附件的一些信息
*/
System.out.println("ContentLocation="+attachment.getContentLocation());
System.out.println("Contentsize="+attachment.getSize());
System.out.println("ContentType="+attachment.getContentType());
String id = attachment.getContentId();
System.out.print("Attachment " + id + " contains: " +content);
}
public void paint(Graphics g)
{
super.paint(g);
Image img = getImageFromWebservice();
g.drawImage(img,0,0,null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -