0

我在liferay工作我有这个功能:

public class NewPortlet extends MVCPortlet {

public void abc(ActionRequest arq,ActionResponse ars) throws Exception {
    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(arq);
    FileUploader fileUp = new FileUploaderImpl();
    fileUp.setFid(CounterLocalServiceUtil.increment("FileUploader.class"));
    File file = (File) uploadRequest.getFile("file");   
    byte[] bytes = FileUtil.getBytes(file);
    fileUp.setContent(Base64.objectToString(bytes));
    FileUploaderLocalServiceUtil.addFileUploader(fileUp);
}
}

其中 File 属于 com.liferay.portal.kernel.util.File;我正在尝试使用上述功能上传图片。我让 UI 上传图片,当我点击提交时,我收到以下错误:

ERROR [http-bio-8080-exec-37][render_portlet_jsp:154] java.lang.ClassCastException: java.io.File cannot be cast to com.liferay.portal.kernel.util.File
at com.test.image.test.NewPortlet.abc(NewPortlet.java:25)
at com.liferay.portal.kernel.portlet.LiferayPortlet.callActionMethod(LiferayPortlet.java:153)
at com.liferay.util.bridges.mvc.MVCPortlet.callActionMethod(MVCPortlet.java:250)
at com.liferay.portal.kernel.portlet.LiferayPortlet.processAction(LiferayPortlet.java:80)
at com.liferay.util.bridges.mvc.MVCPortlet.processAction(MVCPortlet.java:220)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:70)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:534)
at com.liferay.portlet.InvokerPortletImpl.invokeAction(InvokerPortletImpl.java:579)

我应该怎么做才能将 java.io.File 转换为 com.liferay.portal.kernel.util.File?

4

2 回答 2

0

您不能强制java.io.File转换为com.liferay.portal.kernel.util.File,因为java.io.File没有实现com.liferay.portal.kernel.util.File接口。

请记住,只有当 A is-a B 时,您才能将 A 转换为B。

于 2013-03-19T06:44:10.347 回答
0

直接的答案是你不能......'cosjava.io.File不是com.liferay.portal.kernel.util.File接口的子类。


但真正的答案是您可能根本不应该尝试com.liferay.portal.kernel.util.File界面。据我所知,它是某种 utils 类的接口……它没有任何明显的实现类。(坦率地说,我根本想不通为什么这个接口存在!)

事实上,我认为你应该这样做:

java.io.File file = ...
byte[] bytes = com.liferay.portal.kernel.util.FileUtil.getBytes(file);

或者:

java.io.File file = ...
com.liferay.portal.kernel.util.FileUtil fu = 
            new com.liferay.portal.kernel.util.FileUtil();
byte[] bytes = fu.getBytes(file);

(我使用完全限定名称仅用于说明目的......)

这是基于我在这里找到的 Liferay 5.1 javadocs 和源代码。YMMV。

于 2013-03-19T07:23:48.683 回答