4

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?

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
brelian
  • 403
  • 2
  • 15
  • 31

2 Answers2

3

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>
Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
Babesh Florin
  • 226
  • 1
  • 11
  • I'm pretty sure that was the most confusing java formatting I have ever seen. Your variable naming is is very strange too, but I try not to touch that. – Joel Peltonen Jul 22 '15 at 06:18
1

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:

  • Make sure, userId is a valid id for a user.
  • Make sure that your value for groupId and repositoryId is valid as well - e.g. it needs to be the id for a site.
  • Same for 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.
  • You're giving 0 for the size (bytes.length)
  • I'm guessing that the file you hardcoded actually exists? Make extra extra sure that you don't have a typo in file- or foldername

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

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • exactly I want to upload images into a exact folder that I created in document library by a portlet. I think for some part that related to add image into that folder I can use "DLFileEntryLocalServiceUtil.addFileEntry()" in my portlet. Is it right? Or my idea is false????????/ – brelian Jun 10 '13 at 04:01