Download file Using JAX-WS Web Service in Java
January 04, 2014
This page will provide a demo for downloading a file using JAX-WS web service in java. We are using here MTOM that is message transmission optimization mechanism. We will create two project, one for server and one for client. Downloading a file using JAX-WS is simple. We will describe step-by-step here. For the demo we consider two directories.
1. D:\DownloadTest
2. D:\DownloadTest\save
Our demo will pick a file from first directory and will save to second directory. You can download complete source code from link given at end of this blog. To describe the download using JAX-WS, we will go by three steps as below.
1. Software Requirement
2. Create JAX-WS Server for Download
3. Create JAX-WS Client for Download
Software Requirement
1. JDK 62. Eclipse
JAX-WS 2.1 is also required but it is available in JDK 6.
Create JAX-WS Server for Download
To create download server, we need to create an interface and its implementation. Find both the file.FileDownload.java
package com.concretepage.endpoint; import javax.activation.DataHandler; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public interface FileDownload{ @WebMethod public DataHandler downloadFile(String fileName); }
FileDownloadImpl.java
package com.concretepage.endpoint; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.jws.WebService; import javax.xml.ws.soap.MTOM; @MTOM @WebService(endpointInterface = "com.concretepage.endpoint.FileDownload") public class FileDownloadImpl implements FileDownload { @Override public DataHandler downloadFile(String fileName) { FileDataSource dataSource = new FileDataSource("D:/DownloadTest/"+fileName); DataHandler fileDataHandler = new DataHandler(dataSource); return fileDataHandler; } }
@WebService : It defines a class to be a web service. The interface and its implementation both should be annotated with @WebService.
@SOAPBinding : It is used for SOAP binding. It maps the web service to SOAP message protocol.
@WebMethod : It is annotated with a method that is exposed to web service. The method must be public.
@MTOM : It enables the use of message transmission optimization mechanism in our web service. It is used to send efficiently a binary data.
To publish the web service we are using a main method that will start a server with our given URL.
DownloadServer.java
package com.concretepage; import javax.xml.ws.Endpoint; import com.concretepage.endpoint.FileDownloadImpl; public class DownloadServer { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/WebServiceDemo/FileDownload", new FileDownloadImpl()); } }
Create JAX-WS Client for Download
Now we need to create the client that will access the web service to download a file. For this, first step is to create the stub of web service. To create the stub, run the below command in command prompt from the client project directory.wsimport -s src/main/java http://localhost:8080/WebServiceDemo/FileDownload?wsdl
FileClient.java
package com.concretepage.client; import java.io.FileOutputStream; import com.concretepage.endpoint.FileDownload; import com.concretepage.endpoint.FileDownloadImplService; public class FileClient{ public static void main(String[] args) throws Exception { FileDownloadImplService service = new FileDownloadImplService(); FileDownload server = service.getFileDownloadImplPort(); byte[] bytes = server.downloadFile("test.docx"); FileOutputStream fos = new FileOutputStream("D:/DownloadTest/save/downloadTest.docx"); fos.write(bytes); fos.flush(); fos.close(); System.out.println("Download Completed"); } }