File Upload Using WSDC for an MTOM WebService

Use Case:

In this usecase I will explain about how to upload file in WSDC to the target WebService from the Backing Bean 

What is MTOM ?

MTOM or  Message Transmission Optimization Mechanism is the W3C Message Transmission Optimization Mechanism, a method of efficiently sending binary data to and from Web services.


Lets start by creating a Java WebService with MTOM Enabled to allow transfer of files as shown below.

In the service provide a location where the files have to be saved else create a folder called mytemp in C drive if planning to use the code below. 

package project1;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.MTOM;
@WebService(serviceName = "UploadService")
public class UploadService {
public UploadService() {
public void fileUpload(@WebParam(name = "arg0") String fileName, @XmlMimeType("application/octet-stream") @WebParam(name = "arg1") DataHandler data) {
try {
OutputStream os = new FileOutputStream("C:\\mytemp\\"+fileName);
InputStream is = data.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead;
//read from is to buffer
while((bytesRead = is.read(buffer)) !=-1){
os.write(buffer, 0, bytesRead);
//flush OutputStream to write any buffered data to file
catch (Exception e)
throw new WebServiceException(e);

Now deploy the Service to Integrated WLS.

Create an ADF Fusion Application and create WebService Data Control from the New Gallery using the WSDL URL of the service deployed. After creating the DC , expand the DC palette where we can observe that the FileUpload method takes 2 arguments , the first argument is the file Name which String type and the Second Argument is Object type 


Now in the ViewController, create a jspx page with Backing Bean or we can create the backing bean separately and bind to the page.

In the Backing Bean add the following Two Methods

// This Method is used to read the file provided by the user and pass it to the DC
public void fileUploaded(ValueChangeEvent event)
UploadedFile file = (UploadedFile) event.getNewValue();
if (file != null)
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage("Successfully uploaded file " + file.getFilename()+" (" + file.getLength()+" bytes)");
context.addMessage(event.getComponent().getClientId(context), message);
DCBindingContainer bc = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
OperationBinding op = bc.getOperationBinding("fileUpload");
try {
//Convert the file Data into Byte Array and then pass this to the DC
byte[] buffer = readFully(file.getInputStream());
Map param = op.getParamsMap();
} catch (IOException e) {
public byte[] readFully(InputStream input) throws IOException
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = input.read(buffer)) != -1)
output.write(buffer, 0, bytesRead);
return output.toByteArray();

In jspx page, Drag and drop the ADF Input File Component  and bind the Value Change Listener component to the fileUploaded() Method given above


Go to the page definitions of the page, in  the bindings add the Method Action as shown below

Once after adding the method action, drag &drop a button onto the page for submit.Now when the jspx page is rendered , browse and select a file then click on Submit button 


Now we can observe that file gets transferred to the location mentioned in the Service

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha