Upload File to Server using WebService

Sometimes in our applications we need to save information contained in files png, pdf, xml, etc. maybe to consult later or store for this reason we need one solution to resolve this.

In this article i’m going to upload a file (pdf, image, or anithing type) to my server and save it in a remote path for example (/home/iger/Dropbox/files), later im going to add this in swing application.

I decided to use a web service because it’s a easy solution to connect to a remote computer and send the information.

1. Background

In the approach employed by this article, the binary data is embedded directly in the SOAP envelop using base64 text encoding. In other words, the raw binary data is converted to an encoded String which is value of an XML element in the SOAP message. Upon receiving the SOAP message, the receiver decodes the encoded String in order to re-construct the original binary data.

The following picture depicts this process:

SOAP with binary data process

This approach is the simplest way and is only suitable for transferring a small amount of binary data. It becomes very inefficient when transferring a large amount of binary data because the base64 text encoding technique bloats the data by a factor of 1.33x (UTF-8 text encoding) or 2.66x (UTF-16 text encoding) of the original size. Also, the encoding/decoding process slows down the application performance.

This approach is also often referred as “by value” or “inline attachment” method. Now, let’s go through an example application that is used to transfer small binary files. NOTE: To optimize the binary data transfer, see: Using MTOM to optimize binary data transfer with JAX-WS web services.

(www.codejava.net, 2017)

 Ok, lets go:

I used NetBeans to code the web service implementation:

first you need to add new web operation in a Web Application project:

addWebService

I use this attributes to create my WS

AddOperationForm

The wizard create a basic structure:

basic structure WS

Now i need to add a operation “upload”, we can use the pop-up menu to add a new operation with the right click on our web service,

  • name of operation: upload
  • parameters:
    • fileName (String).- is the name of the file to upload
    • fileBytes ([]byte).- is the file stored in array of bytes

AddOperation

Captura de pantalla 2017-12-26 23.43.06

That wizard create a following code:

code upload method

The following code write “fileBytes” in a remote path (filePath):

 String filePath = "/home/iger/Dropbox/files/" + fileName;
 
 try {
 FileOutputStream fos = new FileOutputStream(filePath);
 BufferedOutputStream outputStream = new BufferedOutputStream(fos);
 outputStream.write(fileBytes);
 outputStream.close();
 
 System.out.println("Received file: " + filePath);
 
 } catch (IOException ex) {
 System.err.println(ex);
 throw new WebServiceException(ex);
 }

In this point we have a simple web service to receive the file and store in remote path, we need to publish our web application server for example glassfish:

1.- we need make a “.war” project and publish it, for this we need to execute cleand and build our web project. If it work correcly the result is a “.war” file:

Created dir: C:\Users\igero\…\NetBeansProjects\LimitedEdition_WS\dist
Building jar: C:\Users\igero\..\NetBeansProjects\LimitedEdition_WS\dist\LimitedEdition_WS.war
do-dist:
dist:
BUILD SUCCESSFUL (total time: 4 seconds)

web deploy

We can see the web service deployed writing the url

web service up

We can test our web service with some tool like SoapUI (soapui.org)  the open source version workly excelent for basic executions.

soap uno

Open new Soap Project (File->New Soap Project [Ctrl + N]) and add WSDL url

soap dos

Click Ok Button and the tool create basic implementation for web methods, if you remember we have 2 web methods “hello” and “upload”.

uno (1)

For the second method you can test too

for example:

dos

After execution of webservice we can find the “GerardoTest.txt” inside path (/home/iger/Dropbox/files/) in our server:

terminal tres

The result will look like the following:

terminal cuatro

 

Autor: iger001

I'm Computer Systems Engineering graduates from National Polytechnic Institute, I have a Master degree in Computer Sciences, I like the video games and technology. I am interested in computer vision, IA, and robots.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s