Send Any File using SOAP Webservice

This tutorial shows how we can send file using SOAP based webservice. The type of the file can be anything, i.e., text, image, pdf, MS doc etc. For this type of application we need to create two applications – one will work as a client application(which sends a file) and other one will work as a server application(which receives a file). So the server application will deploy the service whereas client application will consume the service. In server application we will have logic for what will be done with the file content after receiving the file. Using the client application we will upload the file and send through the SOAP service.

In this example both client and server applications are web based application. In client application we will upload the file using JSP, Servlet and send it to the server application which then saves the received file to the specific location on disk.

Please feel free to contact me for any kind of help.

Final Output

Home page

SOAP File Attachment

When no file selected for upload

SOAP File Attachment

When a file is selected and sent

SOAP File Attachment

Prerequisites

Tomcat v7
Eclipse
JDK 1.6

jars for server application

SOAP File Attachment

jar for client application

activation.jar

We will now look at the server and client applications how they get created.

First and foremost step is to create Dynamic Web Project in Eclipse for both server(project name : soap-send-file) and client(project name : upload-file) application.

Server Application

Step 1. After creating the project in Eclipse, put all jars under WEB-INF/lib directory.

Step 2. Create endpoint interface with the below source code

 

Step 3. Create endpoint implementation for endpoint service.

 

Step 4. Create Constants.java

 

Step 5. Create FileUtils.java

 

Step 6. Create sun-jaxws.xml file with the below content and put it under WEB-INF directory

 

Step 7. Modify web.xml file as below

 

Step 8. We need to generate request and response structure. So navigate to the root directory of the project and execute the below command from command prompt.

 

Step 9. Now run the application. Hit the URL http://<host>:<port>/<context path>/<URL pattern>?wsdl, i.e., http://localhost:8080/soap-send-file/SoapFileSend?wsdl. You will get the below wsdl file in the browser

 

Now we will see how we can consume the service and send file to the above application.

Client Application

Step 1. After creating a project in Eclipse, put the jar activation.jar under WEB-INF/lib directory

Step 2. Modify the deployment descriptor file – web.xml as follows. We won’t put any entry for servlet in this file because we are using servlet 3 so using annotation is enough.

 

Step 3. Now generate stub or client from wsdl file. So do right-click on the client project(upload-file) and choose Web Services -> Web Service Client. Click Next. Put the URL http://localhost:8080/soap-send-file/SoapFileSend?wsdl for “Service Definition:” on the next window. Make sure “Deploy Client” is selected for client generation as shown below picture.

SOAP File Attachment

You can also select local wsdl file for “Service Definition:” using “Broswe” button. Click Next. Click Finish. The required client files are generated under in.webtuts.service package.

Step 4. Now create a view – index.jsp which will give user an opportunity to upload a file for sending using SOAP webservice.

 

Step 5. Create UploadUtils.java file which holds some utility methods.

 

Step 6. Create a servlet which will upload the file and send to the server application using SOAP service.

 

Step 7. Now run the application.

Note:  Please run the server application first in order to receive and save the file onto disk.

Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.