Monday 10 December 2012

UploadPortletRequest getFiles() cannot upload files with size less than 1kb

Introduction: While uploading a file in liferay we use the processAction() method of our MVC portlet class, wherein we use the following kind of code UploadPortletRequest uploadrequest = PortalUtil.getUploadPortletRequest(actionRequest);
File file =  uploadrequest.getFile("fileUpload");

where "fileUpload" is ofcourse in view.jsp

Problem: On upload if the file size was less than 1kb, there is no temporary file created(Whenever an upload operation is done a temporary file in the temp folder of the tomcat is created which is been referred for any operation performed to use the file.

Solution: use input stream instead of getFile(), as follows:-

UploadPortletRequest uploadrequest = PortalUtil.getUploadPortletRequest(actionRequest);
            InputStream[] inputStream = uploadrequest.getFilesAsStream("fileupload");
            for(InputStream fileObj:inputStream){
            File file = FileUtil.createTempFile(fileObj);
            } 

No comments:

Post a Comment