Monday 1 August 2016

liferay web service file upload

Hi
In this post I will first create web service and with the help of post man I will upload a file.
Here I will also increase the size of column using "portlet-model-hints.xml" file.
First I say thanks to Sudheer Kumar liferayway helps me to get the concept of increase of size of column.

We use liferay build service to create table
I am using two tables one is a just gateway to access other services.
My service.xml is

<entity name="EDServiceGateway" local-service="true" remote-service="true">
</entity>

<entity name="FileUploader" local-service="true">
  <column name="fid" type="long" primary="true"/>
    <column name="content" type="String"/>
</entity>

Then build service and also build WSDD.

After build service open /WEB-INF/sql/tables.sql

You can see
create table fileuploader (
fid LONG not null primary key,
content VARCHAR(75) null
);
Here content is name of our column and the default size is VARCHAR(75) if we use String data type in our service.xml file.
To increase its size we have to follow these steps

Open portlet-model-hints.xml

<model name="com.slayer.model.FileUploader">
<field name="fid" type="long" />
<field name="content" type="String" />
</model>

Above is default generated sql by default after service build.
We want to increase the size of content column.
To do this we have to modify file as below code.

<model name="com.slayer.model.FileUploader">
<field name="fid" type="long" />
<field name="content" type="String">
<hint-collection name="CLOB" />
</field>
</model>

This is our final code.
If you have source code of liferay portal then you can search a file.
portal-model-hints.xml.
In this file hint-collection="CLOB" already declared with very big size.
After above changes then build service again.

Now this time check your tables.sql file

create table fileuploader (
fid LONG not null primary key,
content TEXT null
);
Size of content column changes to TEXT type.

Please don't forget to build WSDD.

Number of services will create.

Here I am using FileUploaderLocalServiceImpl to upload file to db.

import java.io.File;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.FileUtil;

public class FileUploaderLocalServiceImpl
extends FileUploaderLocalServiceBaseImpl {

Log log = LogFactoryUtil.getLog(FileUploaderLocalServiceImpl.class);

public boolean add(File file){
   log.info("add()");
byte[] bt;
    long fid=0L;
    try {
     fid=CounterLocalServiceUtil.increment();
     FileUploader f = fileUploaderLocalService.createFileUploader(fid);
     bt = FileUtil.getBytes(file);
     f.setContent(Base64.objectToString(bt));
     fileUploaderPersistence.update(f, false);
    } catch (Exception e) {
    log.info("Err: "+e.getMessage());
    return false;
    }
    return true;
}

}


Class that expose web services
public class EDServiceGatewayServiceImpl extends
EDServiceGatewayServiceBaseImpl {

Log log = LogFactoryUtil.getLog(EDServiceGatewayServiceImpl.class);

  @AccessControlled(guestAccessEnabled=true)
public JSONArray fileUpload(File file) throws SystemException, PortalException{

  System.out.println("---------------->"+file);
  System.out.println("---------------->"+file.getAbsolutePath());
  log.info("file : "+file);
 File f = file;
 boolean bool = fileUploaderLocalService.add(file);
 log.info("success: "+bool);
  log.info("file name : "+f.getName());
 JSONArray ar = JSONFactoryUtil.createJSONArray();

 return ar;
}

}


if you try this one from
your ip:8080/api/jsonws/EDModule-portlet.edgateway/file-upload
Then it gives one error



This is I don't know why but you can access it through post man

First copy web service url



From here you get the url of generated web service.
Use this web url from post man.


Hit send and hope so it will work.
Thanks


No comments:

Post a Comment