Liferay from fileentry to java file object

Sabrina picture Sabrina · May 29, 2012 · Viewed 7.3k times · Source

Anyone know how to retrieve a contentStream from a DLFileEntry (http://docs.liferay.com/portal/6.0/javadocs/com/liferay/portlet/documentlibrary/model/DLFileEntry.html) using an httpservlet?

I tried using DLFileEntry.getContentStream() but it fails giving me the error

com.liferay.portal.security.auth.PrincipalException: PermissionChecker not initialized

I solved in part that problem setting hardcode my userId:

long userId=2

            PrincipalThreadLocal.setName(userId);

            User user = UserLocalServiceUtil.getUserById(userId);

            PermissionChecker permissionChecker;

                permissionChecker = PermissionCheckerFactoryUtil.create(user, false);

            PermissionThreadLocal.setPermissionChecker(permissionChecker);

The problem of this solution is how to get the real userId, and how happen if the user is a guest?

I tried Long.parseLong(req.getRemoteUser()); but fail also.

Answer

Ravi Kumar Gupta picture Ravi Kumar Gupta · May 30, 2012
DLFileEntryLocalServiceUtil.getFile(userId, fileEntryId, version, incrementCounter) 

gives you the File. Did you try using this?