Home  >  Web Services

Download file Using JAX-WS Web Service in Java

By Arvind Rai, 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 6
2. 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.
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;
@SOAPBinding(style = Style.RPC)
public interface FileDownload{
	public DataHandler downloadFile(String fileName);
There is one method downloadFile in the interface that will take filename as an argument. Now it will be implemented to return DataHandler.
package com.concretepage.endpoint;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
@WebService(endpointInterface = "com.concretepage.endpoint.FileDownload")
public class FileDownloadImpl implements FileDownload {
public DataHandler downloadFile(String fileName) {
	FileDataSource dataSource = new FileDataSource("D:/DownloadTest/"+fileName);
	DataHandler fileDataHandler = new DataHandler(dataSource);
	   return fileDataHandler;
Find the description of annotations which has been used in the JAX-WS server creation.

@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.
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());	
Find server project structure in eclipse.
Download file Using JAX-WS Web Service in Java

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
Find the file which is fetching the data from server and saving it in our defined location.
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");
		System.out.println("Download Completed");
Find client project structure in eclipse.
Download file Using JAX-WS Web Service in Java

Download Source Code


©2022 concretepage.com | Privacy Policy | Contact Us