In Liferay6.1 I want to add images programmatically into Document Library?
This is my main method that I want to add an image into document library by it :
public class ImgGallery {
public static void main(String[] args) throws SystemException, FileNotFoundException {
byte[] bytes = new byte[0];
File srcfile = new File("C:\\Users\\my-pc\\Pictures\\extra\\myPhoto.jpg");
InputStream in = new FileInputStream(srcfile);
long repositoryId = 10180;
long folderId = 10651;
Map<String, Fields> fieldsMap = new HashMap<String, Fields>();
DLFileEntryLocalServiceUtil.addFileEntry(
10196, repositoryId, repositoryId, folderId,
"filename", MimeTypesUtil.getContentType("behnaz.jpg"),
"title", "description", "changeLog", 0, fieldsMap,
srcfile, null, bytes.length, serviceContext
);
}
}
This doesn't work - I don't know what arguments to supply to DLFileEntryLocalServiceUtil.addFileEntry?
For adding a file to Liferay document library
You will need a folder, that you can create it in liferay control panel, my folder is TestFolder. You search for it with DLFolderLocalServiceUtil.getFolder. You need the dlfileentrytypes. I hope we will understand the rest from the. You need to add the fileentry and after that you need to update it to be approved.
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
File file = uploadRequest.getFile("uploadFile");
DLFileEntry fileEntry = null;
Long lessonId = ParamUtil.getLong(request, "lid");
Lesson lll = LessonLocalServiceUtil.getLesson(lessonId);
String lesName = URLEncoder.encode(lll.getName(themeDisplay.getLocale()));
Date da = new Date();
String ext = FileUtil.getExtension(file.getName());
String dat = new SimpleDateFormat("yyyy-MM-dd").format(da);
String title = lesName + "-" + dat + "." + ext;
long portraitId = themeDisplay.getUser().getPortraitId();
byte[] imageBytes = FileUtil.getBytes(file);
InputStream is = new ByteArrayInputStream(imageBytes);
PortletPreferences preferences = request.getPreferences();
String nameRo=uploadRequest.getParameter("nameRO");
String nameEn=uploadRequest.getParameter("name");
DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(themeDisplay.getScopeGroupId(), 0, "TestFolder");
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), request);
List<DLFileEntryType> tip = DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(DLUtil.getGroupIds(themeDisplay));
fileEntry = DLFileEntryLocalServiceUtil.addFileEntry(themeDisplay.getUserId(),
themeDisplay.getScopeGroupId(),
themeDisplay.getScopeGroupId(),
dlFolder.getFolderId(),
file.getName(),
MimeTypesUtil.getContentType(file),
title,
request.getParameter("name"),
"",
tip.get(0).getFileEntryTypeId(),
null,
file,
is,
file.getTotalSpace(),
serviceContext);
DLFileEntryLocalServiceUtil.updateFileEntry(
themeDisplay.getUserId(),
fileEntry.getFileEntryId(),
file.getName(),
MimeTypesUtil.getContentType(file),
title,
"",
"",
true,
tip.get(0).getPrimaryKey(),
null,
file,
is,
file.getTotalSpace(),
serviceContext);
EDIT : To acces the fileentry download url you can use
DLFileEntry dlf = DLFileEntryLocalServiceUtil.getDLFileEntry(f.get(i).getFileEntryId());
<a href='<%=themeDisplay.getPortalURL()+"/c/document_library/get_file?uuid="+DL.getUuid()+"&groupId="+themeDisplay.getScopeGroupId() %>' download>
Download </a>
Are you trying to call this API function from a commandline? (as the method main implies): You can't just startup a JVM and call Liferay API functions as no initialization is done yet.
You'll need to call API functions (esp. on Local services) from a webapplication - e.g. a portlet or a hook - this typically does not happen from a main method.
Edit: Take a look at the javadoc, granted, this does not give you many clues other than the argument names, but if you go through these and see the implementation as well, there are some things that might be worth trying/checking:
userId is a valid id for a user.groupId and repositoryId is valid as well - e.g. it needs to be the id for a site.folderId: Make sure that it's a valid id for a folder. For this example we don't know how you came up with the value.bytes.length)If this doesn't solve your problem, let us know what exact problem you have with your arguments: "I have a problem" is not really enough information for any meaningful help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With