我在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?
答案 0 :(得分:0)
您无法将java.io.File
投射到com.liferay.portal.kernel.util.File
,因为java.io.File
未实现com.liferay.portal.kernel.util.File
界面。
请记住,当A 是-a B时,您只能将A转换为B.
答案 1 :(得分:0)
直接的答案是你不能......'cos java.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。