Example usage for com.liferay.portal.kernel.util FileUtil getBytes

List of usage examples for com.liferay.portal.kernel.util FileUtil getBytes

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil getBytes.

Prototype

public static byte[] getBytes(InputStream is) throws IOException 

Source Link

Usage

From source file:com.celamanzi.liferay.portlets.rails286.Rails286Portlet.java

License:Open Source License

private void retrieveFiles(ActionRequest request) throws IOException {
    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
    Map<String, Object[]> files = new HashMap<String, Object[]>();

    // Try to discover the file parameters
    for (Object key : uploadRequest.getParameterMap().keySet()) {
        File file = uploadRequest.getFile(key.toString());
        if (file != null && file.length() > 0) {
            // I need to store the bytes because the file will be deleted
            // after the method execution, so when OnlineClient request the
            // file it no longer exists. I need to store the file object
            // to store the path and the attributes of this file.
            byte[] bytes = FileUtil.getBytes(file);
            files.put(key.toString(), new Object[] { file, bytes });
        }/*from w ww. j av a2s  . co  m*/
    }

    if (files.size() > 0) {
        request.setAttribute("files", files);
    }
}

From source file:com.fmdp.webform.portlet.WebFormPortlet.java

License:Open Source License

public String uploadFile(File file, String sourceFileName, String folder) throws PortletException, IOException {

    //String realPath = getPortletContext().getRealPath("/");
    String fsep = File.separator;

    if (_log.isDebugEnabled()) {
        _log.debug("Folder to upload: " + folder);
    }//from  ww w.j av a2s  .c  o m

    try {

        if (_log.isDebugEnabled()) {
            _log.debug("Source file name to upload: " + sourceFileName);
        }

        File newFolder = null;
        newFolder = new File(folder);
        if (!newFolder.exists()) {
            newFolder.mkdir();
        }
        File newfile = null;
        String filename = newFolder.getAbsoluteFile() + fsep + sourceFileName;
        String originalFileName = sourceFileName;
        newfile = new File(filename);
        int j = 0;

        while (newfile.exists()) {
            j++;
            String ext = FileUtil.getExtension(originalFileName);
            filename = newFolder.getAbsoluteFile() + fsep + stripExtension(originalFileName) + "("
                    + Integer.toString(j) + ")." + ext;
            newfile = new File(filename);
        }
        if (_log.isDebugEnabled()) {
            _log.debug("New file name: " + newfile.getName());
            _log.debug("New file path: " + newfile.getPath());
        }

        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(newfile);

        byte[] bytes_ = FileUtil.getBytes(file);
        int i = fis.read(bytes_);

        while (i != -1) {
            fos.write(bytes_, 0, i);
            i = fis.read(bytes_);
        }
        fis.close();
        fos.close();
        Float size = (float) newfile.length();
        if (_log.isDebugEnabled()) {
            _log.debug("file size bytes: " + size);
            _log.debug("file size Mb: " + size / 1048576);
            _log.debug("File created: " + newfile.getName());
        }

        return newfile.getName();

    } catch (FileNotFoundException e) {
        _log.error("File Not Found.");
        e.printStackTrace();
        return "File Upload Error";
    } catch (NullPointerException e) {
        _log.error("File Not Found.");
        e.printStackTrace();
        return "File Upload Error";
    }

    catch (IOException e1) {
        _log.error("Error Reading The File.");
        e1.printStackTrace();
        return "File Upload Error.";
    }

}

From source file:com.liferay.blogs.attachments.test.BaseBlogsEntryImageTestCase.java

License:Open Source License

@Test
public void testAddImage() throws Exception {
    BlogsEntry blogsEntry = addBlogsEntry((ImageSelector) null);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(),
            user.getUserId());//  w w w.j  a  v  a 2 s. co m

    FileEntry fileEntry = getTempFileEntry(user.getUserId(), "image1.jpg", serviceContext);

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(fileEntry.getContentStream()),
            fileEntry.getTitle(), fileEntry.getMimeType(), IMAGE_CROP_REGION);

    addImage(blogsEntry.getEntryId(), imageSelector);

    blogsEntry = BlogsEntryLocalServiceUtil.getEntry(blogsEntry.getEntryId());

    FileEntry imageFileEntry = PortletFileRepositoryUtil.getPortletFileEntry(getImageFileEntryId(blogsEntry));

    Assert.assertEquals("image1.jpg", imageFileEntry.getTitle());
}

From source file:com.liferay.blogs.attachments.test.BlogsEntryCoverImageTest.java

License:Open Source License

@Override
protected BlogsEntry addBlogsEntry(String imageTitle) throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(),
            user.getUserId());/*from  ww w  . ja v  a 2  s .  com*/

    FileEntry fileEntry = getTempFileEntry(user.getUserId(), imageTitle, serviceContext);

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(fileEntry.getContentStream()),
            fileEntry.getTitle(), fileEntry.getMimeType(), IMAGE_CROP_REGION);

    return addBlogsEntry(imageSelector);
}

From source file:com.liferay.blogs.attachments.test.BlogsEntryCoverImageTest.java

License:Open Source License

@Override
protected BlogsEntry updateBlogsEntry(long blogsEntryId, String imageTitle) throws Exception {

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(),
            user.getUserId());//from  w  w  w .  j a va2 s  . co  m

    FileEntry fileEntry = getTempFileEntry(user.getUserId(), imageTitle, serviceContext);

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(fileEntry.getContentStream()),
            fileEntry.getTitle(), fileEntry.getMimeType(), IMAGE_CROP_REGION);

    return updateBlogsEntry(blogsEntryId, imageSelector);
}

From source file:com.liferay.blogs.attachments.test.BlogsEntryImageSelectorHelperTest.java

License:Open Source License

@Test
public void testGetImageSelectorWithDLImageFileEntry() throws Exception {
    InputStream inputStream = null;

    try {/*from www  .  j a v  a2  s .  co m*/
        inputStream = getInputStream();

        byte[] bytes = FileUtil.getBytes(inputStream);

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

        FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(TestPropsValues.getUserId(),
                _group.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, _IMAGE_TITLE,
                MimeTypesUtil.getContentType(_IMAGE_TITLE), "image", StringPool.BLANK, StringPool.BLANK, bytes,
                serviceContext);

        BlogsEntryImageSelectorHelper blogsEntryImageSelectorHelper = new BlogsEntryImageSelectorHelper(
                fileEntry.getFileEntryId(), fileEntry.getFileEntryId() + 1, _IMAGE_CROP_REGION,
                StringPool.BLANK, StringPool.BLANK);

        ImageSelector imageSelector = blogsEntryImageSelectorHelper.getImageSelector();

        Assert.assertArrayEquals(bytes, imageSelector.getImageBytes());
        Assert.assertEquals(_IMAGE_TITLE, imageSelector.getImageTitle());
        Assert.assertEquals(MimeTypesUtil.getContentType(_IMAGE_TITLE), imageSelector.getImageMimeType());
        Assert.assertEquals(_IMAGE_CROP_REGION, imageSelector.getImageCropRegion());
        Assert.assertEquals(StringPool.BLANK, imageSelector.getImageURL());
        Assert.assertFalse(blogsEntryImageSelectorHelper.isFileEntryTempFile());
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}

From source file:com.liferay.blogs.attachments.test.BlogsEntryImageSelectorHelperTest.java

License:Open Source License

@Test
public void testGetImageSelectorWithSameDLImageFileEntry() throws Exception {

    InputStream inputStream = null;

    try {//w  ww . j  a  v a2  s.c  o  m
        inputStream = getInputStream();

        byte[] bytes = FileUtil.getBytes(inputStream);

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

        FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(TestPropsValues.getUserId(),
                _group.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, _IMAGE_TITLE,
                MimeTypesUtil.getContentType(_IMAGE_TITLE), "image", StringPool.BLANK, StringPool.BLANK, bytes,
                serviceContext);

        BlogsEntryImageSelectorHelper blogsEntryImageSelectorHelper = new BlogsEntryImageSelectorHelper(
                fileEntry.getFileEntryId(), fileEntry.getFileEntryId(), _IMAGE_CROP_REGION, StringPool.BLANK,
                StringPool.BLANK);

        Assert.assertNull(blogsEntryImageSelectorHelper.getImageSelector());
        Assert.assertFalse(blogsEntryImageSelectorHelper.isFileEntryTempFile());
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}

From source file:com.liferay.blogs.attachments.test.BlogsEntryImageSelectorHelperTest.java

License:Open Source License

@Test
public void testGetImageSelectorWithTempImageFileEntry() throws Exception {
    InputStream inputStream = null;

    try {//from  www . j  a v  a 2  s  . c  o m
        inputStream = getInputStream();

        byte[] bytes = FileUtil.getBytes(inputStream);

        FileEntry tempFileEntry = TempFileEntryUtil.addTempFileEntry(_group.getGroupId(),
                TestPropsValues.getUserId(), _TEMP_FOLDER_NAME, _IMAGE_TITLE, getInputStream(),
                ContentTypes.IMAGE_JPEG);

        BlogsEntryImageSelectorHelper blogsEntryImageSelectorHelper = new BlogsEntryImageSelectorHelper(
                tempFileEntry.getFileEntryId(), tempFileEntry.getFileEntryId() + 1, _IMAGE_CROP_REGION,
                StringPool.BLANK, StringPool.BLANK);

        ImageSelector imageSelector = blogsEntryImageSelectorHelper.getImageSelector();

        Assert.assertArrayEquals(bytes, imageSelector.getImageBytes());
        Assert.assertEquals(_IMAGE_TITLE, imageSelector.getImageTitle());
        Assert.assertEquals(MimeTypesUtil.getContentType(_IMAGE_TITLE), imageSelector.getImageMimeType());
        Assert.assertEquals(_IMAGE_CROP_REGION, imageSelector.getImageCropRegion());
        Assert.assertEquals(StringPool.BLANK, imageSelector.getImageURL());
        Assert.assertTrue(blogsEntryImageSelectorHelper.isFileEntryTempFile());
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}

From source file:com.liferay.blogs.attachments.test.BlogsEntrySmallImageTest.java

License:Open Source License

@Override
protected BlogsEntry addBlogsEntry(String imageTitle) throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(),
            user.getUserId());/*from   w  ww. jav  a  2 s  .  c  om*/

    FileEntry fileEntry = getTempFileEntry(user.getUserId(), imageTitle, serviceContext);

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(fileEntry.getContentStream()),
            fileEntry.getTitle(), fileEntry.getMimeType(), StringPool.BLANK);

    return addBlogsEntry(imageSelector);
}

From source file:com.liferay.blogs.attachments.test.BlogsEntrySmallImageTest.java

License:Open Source License

@Override
protected BlogsEntry updateBlogsEntry(long blogsEntryId, String coverImageTitle) throws Exception {

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(),
            user.getUserId());//from   w w  w . j ava 2  s  . c  o m

    FileEntry fileEntry = getTempFileEntry(user.getUserId(), coverImageTitle, serviceContext);

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(fileEntry.getContentStream()),
            fileEntry.getTitle(), fileEntry.getMimeType(), StringPool.BLANK);

    return updateBlogsEntry(blogsEntryId, imageSelector);
}