サーバーからクライアントへバイナリ・アタッチメント(イメージ)を送信するための
Message Transmission Optimization Mechanism(MTOM)
および
XML-Binary Optimized Packaging(XOP)
テクニックの使用方法を示す完全なJAX-WS SOAPベースの例。
サーバ上でMTOMを有効にする
サーバーがMTOM経由で添付ファイルを送信できるようにするには、非常に簡単です。単にjavax.xml.ws.soap.MTOMを使用してWebサービス実装クラスに注釈を付けます。
1. WebServiceエンドポイント
ここではRPCスタイルのWebサービスがあり、ユーザーが画像ファイルをアップロードまたはダウンロードできるようにするために、 `downloadImage(String name)`と `uploadImage(image data)`という2つのメソッドが公開されています。
File:ImageServer.java
package com.mkyong.ws; import java.awt.Image; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface ImageServer{ //download a image from server @WebMethod Image downloadImage(String name); //update image to server @WebMethod String uploadImage(Image data); }
__File:Image Server Impl.java
package com.mkyong.ws; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.jws.WebService; import javax.xml.ws.WebServiceException; import javax.xml.ws.soap.MTOM; //Service Implementation Bean @MTOM @WebService(endpointInterface = "com.mkyong.ws.ImageServer") public class ImageServerImpl implements ImageServer{ @Override public Image downloadImage(String name) { try { File image = new File("c:\\images\\" + name); return ImageIO.read(image); } catch (IOException e) { e.printStackTrace(); return null; } } @Override public String uploadImage(Image data) { if(data!=null){ //store somewhere return "Upload Successful"; } throw new WebServiceException("Upload Failed!"); } }
ファイル:ImagePublisher.java
package com.mkyong.endpoint; import javax.xml.ws.Endpoint; import com.mkyong.ws.ImageServerImpl; //Endpoint publisher public class ImagePublisher{ public static void main(String[]args) { Endpoint.publish("http://localhost:9999/ws/image", new ImageServerImpl()); System.out.println("Server is published!"); } }
2. WebServiceクライアント
ここでは、公開されたWebサービスにURL「
http://localhost:9999/ws/image
」でアクセスするためのWebサービスクライアントがあります。
File:ImageClient.java
package com.mkyong.client; import java.awt.Image; import java.io.File; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import javax.xml.ws.Service; import javax.xml.ws.soap.MTOMFeature; import javax.xml.ws.soap.SOAPBinding; import com.mkyong.ws.ImageServer; public class ImageClient{ public static void main(String[]args) throws Exception { URL url = new URL("https://localhost:9999/ws/image?wsdl"); QName qname = new QName("http://ws.mkyong.com/", "ImageServerImplService"); Service service = Service.create(url, qname); ImageServer imageServer = service.getPort(ImageServer.class); /** ** ** ** ** ** ** ** ** ** ** ** test download ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** / Image image = imageServer.downloadImage("rss.png"); //display it in frame JFrame frame = new JFrame(); frame.setSize(300, 300); JLabel label = new JLabel(new ImageIcon(image)); frame.add(label); frame.setVisible(true); System.out.println("imageServer.downloadImage() : Download Successful!"); } }
3. HTTPとSOAPのトラフィック
トラフィック監視ツールで取得された、クライアントによって生成されたHTTPおよびSOAPトラフィックです。
P.Sスペースを節約するために最初のwsdl要求は省略されました。
クライアント送信要求:
POST/ws/image HTTP/1.1 SOAPAction: "" Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, ** ; q=.2, ** /** ; q=.2 Content-Type: text/xml; charset=utf-8 User-Agent: Java/1.6.0__13 Host: localhost:9999 Connection: keep-alive Content-Length: 209 <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:downloadImage xmlns:ns2="http://ws.mkyong.com/"> <arg0>rss.png</arg0> </ns2:downloadImage> </S:Body> </S:Envelope>
-
サーバーは応答を送信します:**
HTTP/1.1 200 OK Transfer-encoding: chunked Content-type: multipart/related; start="<rootpart** c73c9ce8-6e02-40ce-9f68-064e18843428@example.jaxws.sun.com>"; type="application/xop+xml"; boundary="uuid:c73c9ce8-6e02-40ce-9f68-064e18843428"; start-info="text/xml" --uuid:c73c9ce8-6e02-40ce-9f68-064e18843428 Content-Id:<rootpart ** c73c9ce8-6e02-40ce-9f68-064e18843428@example.jaxws.sun.com> Content-Type:application/xop xml; charset = utf-8; type = "text/xml"コンテンツ転送 - エンコーディング:バイナリ <?xml version = "1.0"?> <S:封筒xmlns:S = "http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:downloadImageResponse xmlns:ns2 = "http://ws.mkyong.com/"> <戻る> <xop:Include xmlns:xop = "http://www.w3.org/2004/08/xop/include" href = "cid:012eb00e-9460-407c-b622-1be987fdb2cf@example.jaxws.sun.com"> </xop:インクルード> </return> </ns2:downloadImageResponse> </S:Body> </S:封筒> --uuid:c73c9ce8-6e02-40ce-9f68-064e18843428 コンテンツID:<012eb00e-9460-407c-b622-1be987fdb2cf@example.jaxws.sun.com> コンテンツタイプ:image/png コンテンツ転送エンコーディング:バイナリ ここのバイナリデータ.............
クライアント上でMTOMを有効にする
クライアントがMTOM経由で添付ファイルをサーバーに送信できるようにするには、いくつかの余計な作業が必要です。次の例を参照してください。
….//codes enable MTOM in client
BindingProvider bp = (BindingProvider) imageServer;
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);
=== 1. WebServiceクライアント MTOMを介して上記の公開されたエンドポイント(http://localhost:8888/ws/image)に画像ファイルを送信するWebサービスクライアントがあります。 __File:ImageClient.java__
package com.mkyong.client;
import java.awt.Image;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.soap.MTOMFeature;
import javax.xml.ws.soap.SOAPBinding;
import com.mkyong.ws.ImageServer;
public class ImageClient{
public static void main(String[]args) throws Exception {
URL url = new URL("https://localhost:8888/ws/image?wsdl"); QName qname = new QName("http://ws.mkyong.com/", "ImageServerImplService");
Service service = Service.create(url, qname); ImageServer imageServer = service.getPort(ImageServer.class);
/** ** ** ** ** ** ** ** ** ** ** ** test upload ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** / Image imgUpload = ImageIO.read(new File("c:\\images\\rss.png"));
//enable MTOM in client BindingProvider bp = (BindingProvider) imageServer; SOAPBinding binding = (SOAPBinding) bp.getBinding(); binding.setMTOMEnabled(true);
String status = imageServer.uploadImage(imgUpload); System.out.println("imageServer.uploadImage() : " + status);
}
}
=== 2. HTTPとSOAPのトラフィック トラフィック監視ツールで取得された、クライアントによって生成されたHTTPおよびSOAPトラフィックです。 __P.Sスペースを節約するために最初のwsdl要求は省略されました。__ ** クライアント送信要求:**
POST/ws/image HTTP/1.1
Content-type: multipart/related;
start=”<rootpart**
751f2e5d-47f8-47d8-baf0-f793c29bd931@example.jaxws.sun.com
>”;
type=”application/xop+xml”;
boundary=”uuid:751f2e5d-47f8-47d8-baf0-f793c29bd931″;
start-info=”text/xml”
Soapaction: “”
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg,
; q=.2,
/** ; q=.2
User-Agent: JAX-WS RI 2.1.6 in JDK 6
Host: localhost:9999
Connection: keep-alive
Content-Length: 6016
–uuid:751f2e5d-47f8-47d8-baf0-f793c29bd931
Content-Id:<rootpart**
751f2e5d-47f8-47d8-baf0-f793c29bd931@example.jaxws.sun.com
> Content-Type:application/xop xml; charset = utf-8; type = “text/xml”コンテンツ転送 – エンコーディング:バイナリ
<s:Body> <ns2:uploadImage xmlns:ns2 = “http </tt> </tt> </xsl:xmlns:S =”
http://schemas.xmlsoap.org/soap/envelope/”>
; ://ws.mkyong.com/”> <arg0> <xop:xmlns:xop =”
http://www.w3.org/2004/08/xop/include
“href =” cid:2806f201-e15e-
4ee0-8347-b7b4dffad5cb@example.jaxws.sun.com
“> </xop:Include> </arg0> </ns2:uploadImage> </S:Body> </S:Envelope>
–uuid:751f2e5d-47f8-47d8-baf0-f793c29bd931
Content-Id: <
2806f201-e15e-4ee0-8347-b7b4dffad5cb@example.jaxws.sun.com
>
Content-Type: image/png
Content-Transfer-Encoding: binary
Binary data here………….
** サーバーは応答を送信します:**
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: multipart/related;
start=”<rootpart**
188a5835-198b-4c28-9b36-bf030578f2bd@example.jaxws.sun.com
>”;
type=”application/xop+xml”;
boundary=”uuid:188a5835-198b-4c28-9b36-bf030578f2bd”;
start-info=”text/xml”
–uuid:188a5835-198b-4c28-9b36-bf030578f2bd
Content-Type:application/xop xml; charset = utf-8; type = “text/xml” Content-Type:<rootpart**
188a5835-198b-4c28-9b36-bf030578f2bd@example.jaxws.sun.com
> – エンコーディング:バイナリ
<?xml version = “1.0”?> <S:Envelope xmlns:S = “http://schemas.xmlsoap.org/soap/envelope/”> <S:Body> <ns2:uploadImageResponse xmlns:ns2 = “http ://ws.mkyong.com/”> <return> Upload Successful </return> </ns2:uploadImageResponse> </S:Body> </S:Envelope>
–uuid:188a5835-198b-4c28-9b36-bf030578f2bd–
=== 完全なWSDL文書 WSDLファイルの調査に興味がある人は、URL:__http://localhost:9999/ws/image?wsdl__を使用してwsdlファイルを入手できます。 ImageServer WSDLファイルのサンプル
<?xml version=”1.0″ encoding=”UTF-8″?>
<definitions
xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/”
xmlns:tns=”http://ws.mkyong.com/”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns=”http://schemas.xmlsoap.org/wsdl/”
targetNamespace=”http://ws.mkyong.com/”
name=”ImageServerImplService”>
<types></types>
<message name=”downloadImage”>
<part name=”arg0″ type=”xsd:string”></part>
</message>
<message name=”downloadImageResponse”>
<part name=”return” type=”xsd:base64Binary”></part>
</message>
<message name=”uploadImage”>
<part name=”arg0″ type=”xsd:base64Binary”></part>
</message>
<message name=”uploadImageResponse”>
<part name=”return” type=”xsd:string”></part>
</message>
<portType name=”ImageServer”>
<operation name="downloadImage"> <input message="tns:downloadImage"></input> <output message="tns:downloadImageResponse"></output> </operation> <operation name="uploadImage"> <input message="tns:uploadImage"></input> <output message="tns:uploadImageResponse"></output> </operation>
</portType>
<binding name=”ImageServerImplPortBinding” type=”tns:ImageServer”>
<soap:binding transport=”http://schemas.xmlsoap.org/soap/http” style=”rpc”>
</soap:binding>
<operation name="downloadImage"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal" namespace="http://ws.mkyong.com/"></soap:body> </input> <output> <soap:body use="literal" namespace="http://ws.mkyong.com/"></soap:body> </output> </operation>
<operation name="uploadImage"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal" namespace="http://ws.mkyong.com/"> </soap:body> </input> <output> <soap:body use="literal" namespace="http://ws.mkyong.com/"> </soap:body> </output> </operation>
</binding>
<service name=”ImageServerImplService”>
<port name=”ImageServerImplPort” binding=”tns:ImageServerImplPortBinding”>
<soap:address location=”http://localhost:9999/ws/image”></soap:address>
</port>
</service>
</definitions>
=== ソースコードをダウンロードする ダウンロード - リンク://wp-content/uploads/2010/11/JAX-WS-Attachment-MTOM-Example.zip[JAX-WS-Attachment-MTOM-Example.zip](20KB) [#reference]## === リファレンス . http://www.devx.com/xml/Article/34797/1763/page/1 . http://download.oracle.com/docs/cd/E17802__01/webservices/webservices/docs/2.0/jaxws/mtom-swaref.html . http://www.crosschecknet.com/intro__to__mtom.php . http://download.oracle.com/docs/cd/E12840__01/wls/docs103/webserv__adv/mtom.html . http://www.theserverside.com/news/1363957/Sending-Attachments-withSOAP . http://metro.java.net/guide/Binary__Attachments____MTOM__.html リンク://タグ/添付ファイル/[添付ファイル]リンク://タグ/jax-ws/[jax-ws]link://tag/mtom/[mtom]link://タグ/web-services/[Webサービス]