1. in
service.xml
Open
service.xml:
write
the following code.
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE service-builder
PUBLIC "-//Liferay//DTD
Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.upload.slayer">
<author>meera</author>
<namespace>U</namespace>
<entity name="FileUploader"
table="fileuploader" local-service="true">
<column name="fid"
type="long" primary="true"/>
<column name="content"
type="String"/>
</entity>
</service-builder>
2. Now
do Ant-build service for the portlet you created.
3. portlet-model-hints.xml (ctrl+shift+r)
a) Open portlet-model-hints.xml and you will see a code like this
<model-hints>
<model name="com.sample.model.FileUploader">
<field name="fid" type="long" />
<field name="content" type="String" />
</model>
<model-hints>
a) Open portlet-model-hints.xml and you will see a code like this
<model-hints>
<model name="com.sample.model.FileUploader">
<field name="fid" type="long" />
<field name="content" type="String" />
</model>
<model-hints>
b) Now update the above code with this below code:
<?xml version="1.0"
encoding="UTF-8"?>
<model-hints>
<model name="com.sample.model.FileUploader">
<field name="fid"
type="long" />
<field name="content"
type="String">
<hint-collection name="CLOBTYPE"
/>
</field>
</model>
<hint-collection name="CLOBTYPE">
<hint name="max-length">2000000</hint>
</hint-collection>
<model name="com.upload.slayer.model.FileUploader">
<field name="fid"
type="long" />
<field name="content"
type="String" />
</model>
</model-hints>
View.jsp
4. Now open view.jsp an add following code to
insert file
<portlet:actionURL name="addStack"
var="actionFoo"></portlet:actionURL>
<form
action="<%=actionFoo.toString()%>" method="post"
enctype="multipart/form-data">
<input type="file"
name="file"/>
<input type="submit"
value="Submit" />
</form>
5. Now
deploy it and you will see the file and the submit button in your page.
FileUploaderLocalServiceImpl.java
6. Open FileUploaderLocalServiceImpl.java and
add following code
package com.upload.slayer.service.impl;
import java.io.File;
import java.io.IOException;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.FileUtil;
import com.upload.slayer.model.FileUploader;
import com.upload.slayer.service.base.FileUploaderLocalServiceBaseImpl;
public class FileUploaderLocalServiceImpl
extends
FileUploaderLocalServiceBaseImpl {
public void add(File file)
{
byte[] bt;
long fid=0L;
try {
fid=CounterLocalServiceUtil.increment();
FileUploader f =
fileUploaderLocalService.createFileUploader(fid);
bt = FileUtil.getBytes(file);
System.out.println(bt);
f.setContent(Base64.objectToString(bt));
fileUploaderPersistence.update(f, false);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
In contoller.java
7. Open controller.java action class and write down logic as shown below:
package com.upload;
import java.io.File;
import
javax.portlet.ActionRequest;
import
javax.portlet.ActionResponse;
import
com.liferay.portal.kernel.upload.UploadPortletRequest;
import
com.liferay.portal.util.PortalUtil;
import
com.liferay.util.bridges.mvc.MVCPortlet;
import
com.upload.slayer.service.FileUploaderLocalServiceUtil;
public class UploadPortlet extends MVCPortlet {
public void addStack(ActionRequest
arq,ActionResponse ars)
{
UploadPortletRequest ureq=PortalUtil.getUploadPortletRequest(arq);
File file=(File) ureq.getFile("file");
System.out.println("sddgvwsg"+((java.io.File)
file).getName());
FileUploaderLocalServiceUtil.add((java.io.File)
file);
}
}