Showing posts with label File Upload. Show all posts
Showing posts with label File Upload. Show all posts

Tuesday, 6 May 2014

File Upload using Rest Data Control in ADF


About:

In my previous post, I explained on how to create a Restful service using which File Upload can be performed without using Multipart Form data. In this blog, i will be explaining on how to consume such a service in RestDataControl in ADF.

Pre-requisites:

1. Lets create a jersey rest service as per this blog and deploy it to the server
2. Now create a XSD file for the service as shown below

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified">
  <xsd:element name="fileData">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="fileInfo" type="xsd:base64Binary"/>
        <xsd:element name="fileName" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>


Description:

Create a Fusion Web Application and using the url of the rest service deployed above create a Rest Data Control from New -> Gallery -> WebService DataControl and select Type as REST.






Now create a jspx page and backing bean in the view controller. Then create pagedef for the page by right clicking on the jspx page and selecting Go to PageDef .  In the Page Definition add the methodAction from the DC



 Add the following piece of code in the Backing bean associated with the page.

/**
      *  Method to Upload the file and post it to the rest service
      */
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("posting");
             try {
                 byte[] buffer = readFully(file.getInputStream());
                 String encodedStr = new sun.misc.BASE64Encoder().encode(buffer);
                 if(op!=null){
                    Map tempMap = new HashMap();
                    
                    tempMap.put("fileInfo",encodedStr);
                    tempMap.put("fileName",file.getFilename());
                    op.getParamsMap().put("fileData", tempMap);
                    op.execute();
                 }
             } catch (IOException e) {
                 System.out.println(e);
             }
             
        }
      }
    /**
      *  Method to convert the InputStream to ByteArray
      */

    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 the Page create a InputFileComponent and bind the ValuechangeListener to the fileUploaded method in the backing bean. Now add Command button for Submit.

That's it we are ready to transfer the file, run the jspx page and select the file to be transferred using the browse button and click on submit.

Thursday, 24 April 2014

File Upload in Jersey Restful service


About:

There are many blogs which discuss on how to do file transfer in JAX-RS /Restful service all this blogs discuss using the Multipartformdata i.e. FormDataParam as the input Stream.
In this Blog, I will  be discussing on one of the way about how to create a service which will accept the File Object as input for the method which it consumes in the form XML.


Code: 

Bean Class
package model;
import java.io.InputStream;
import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class FileData {
    public FileData() {
        super();
    }
    
    String fileName;
    byte[] fileInfo;


    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileName() {
        return fileName;
    }


    public void setFileInfo(byte[] fileInfo) {
        this.fileInfo = fileInfo;
    }

    public byte[] getFileInfo() {
        return fileInfo;
    }

}

Service Class

package model;


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("model")
public class FileService {
    public FileService() {
        super();
    }


    @POST
    @Consumes("application/xml")
    public void uploadFile(FileData fd) {
        try {
            OutputStream os = new FileOutputStream("" + fd.getFileName());
            InputStream is = new ByteArrayInputStream(fd.getFileInfo());
             
            byte[] buffer = new byte[1024];
            int bytesRead;
            //read from is to buffer
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            is.close();
            //flush OutputStream to write any buffered data to file
            os.flush();
            os.close();


        } catch (Exception e) {
            System.out.println("Inside the Service"+e);
        }

    }

}


For Client :

I will explain in another blog on how to consume this in REST DataControl / URL DataControl