1

I'm new at Java and Life. I'm creating an UploadPortlet example with Primefaces. Here is my view.xhtml:

<h:form enctype="multipart/form-data">
    <p:fileUpload fileUploadListener="#{imagesView.upload}"
        mode="advanced" multiple="true" sizeLimit="10000000"
        allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
    <p:growl id="messages" showDetail="true" />
</h:form>

And the upload function:

public void upload(FileUploadEvent event) {
    UploadedFile file = event.getFile();
    String url = event.getFile().toString();

    RenderRequest renderRequest = (RenderRequest) (FacesContext.getCurrentInstance().getExternalContext()
            .getRequestMap().get("javax.portlet.request"));
    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long repositoryId = themeDisplay.getScopeGroupId();
    String mimeType = file.getContentType();
    String title = "test2222";
    String description = "This file is added via programatically";
    String changeLog = "hi";
    Long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
    try {
        Folder folder = DLAppServiceUtil.getFolder(themeDisplay.getScopeGroupId(), parentFolderId, "test");
        ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),
                renderRequest);
        InputStream is = file.getInputstream();
        DLAppServiceUtil.addFileEntry(repositoryId, folder.getFolderId(), file.getFileName(), mimeType, title,
                description, changeLog, is, file.getSize(), serviceContext);
    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }

then I got this:

Caused by: java.lang.ClassCastException: com.liferay.portlet.ResourceRequestImpl cannot be cast to javax.portlet.RenderRequest

If I use the local file, it's ok, for ex:

File file = new File("F:\\Lib\\Photos\\expample.png");

So, anyone can help me explain why and how to fix this problem? Thx you!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bing Farm
  • 75
  • 1
  • 8
  • 2
    You are casting a resource request to a render request, see here: [Can someone explain "ClassCastException" in Java?](http://stackoverflow.com/questions/907360/can-someone-explain-classcastexception-in-java). – Tobias Liefke Nov 01 '16 at 10:35
  • 4
    Try to use `PortletRequest` instead of `RenderRequest` - that should fix it. – Tobias Liefke Nov 01 '16 at 10:41
  • 1
    This question is NOT a duplicate of the other one. That explains what a CCE is, this one asks why it happens in a very specific situation. In this case, it happens because the OP is trying to get a RenderRequest in a method executed in the resource phase of the portlet. Tobias suggestion may help, but this question surely deserves a longer answer. – brandizzi Nov 04 '16 at 18:49
  • many thx to Tobias and brandizzi! The Tobias's solution fixed my problem. It's just using PortletRequest instead of RenderRequest. But actually I still not undertand about the problems and the different between PortletRequest, RenderRequest and ActionRequest. As my question, when I use a local file ( java.io.File), RenderRequest work... I'm sorry for my poor knowledge. I'll try to learn more – Bing Farm Nov 07 '16 at 02:26

0 Answers0