如何使用 Liferay 在 portlet 中下载文件或 InpuStream?

How download a file or InpuStream in portlet using Liferay?

我在 UI 中有一个按钮调用下载 file 的方法,它采用 InpuStream 类型。这是我的代码

<p:commandButton value="Download" actionListener="#{cc.attrs.noteAndFileBean.downloadFileAttached(noteAndFile)}" />

和豆子

public void downloadFileAttached(final GridFSDBFile noteAndFile)
{
    try
    {
        PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        HttpServletResponse response = PortalUtil.getHttpServletResponse(portletResponse);
        response.setContentType(noteAndFile.getContentType());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + noteAndFile.getFilename());
        response.setHeader("Content-Transfer-Encoding", "binary");
        response.getOutputStream().write(IOUtils.toByteArray(noteAndFile.getInputStream()));
        response.flushBuffer();
    }
    catch (IOException excpetion)
    {
        LOGGER.error("An error occurred downloading the file: " + excpetion.getMessage(), excpetion);
        showMessage(ERROR, "Problemas al descargar el archivo");
    }
}

GridFSDBFile 类型是一个将文件存储在 mongo 中的对象,然后为了获取 InputStream 我调用 getInputStream() 然后将其传递给 byte array .

当我调用这个方法时,我收到一个很大的异常(部分异常)

Caused by: java.lang.IllegalStateException
    at com.liferay.portal.servlet.filters.gzip.GZipResponse.getWriter(GZipResponse.java:150)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
    at com.liferay.portal.kernel.servlet.MetaInfoCacheServletResponse.getWriter(MetaInfoCacheServletResponse.java:315)
    at com.liferay.portlet.MimeResponseImpl.getWriter(MimeResponseImpl.java:102)
    at com.liferay.faces.bridge.context.BridgeContextImpl.getResponseOutputWriter(BridgeContextImpl.java:1197)
    at com.liferay.faces.bridge.context.BridgeContextWrapper.getResponseOutputWriter(BridgeContextWrapper.java:251)
    at com.liferay.faces.bridge.context.ExternalContextCompat_2_0_Impl.getResponseOutputWriter(ExternalContextCompat_2_0_Impl.java:629)
    at com.sun.faces.context.PartialViewContextImpl.createPartialResponseWriter(PartialViewContextImpl.java:429)
    at com.sun.faces.context.PartialViewContextImpl.access0(PartialViewContextImpl.java:72)
    at com.sun.faces.context.PartialViewContextImpl$DelayedInitPartialResponseWriter.getWrapped(PartialViewContextImpl.java:573)
    at com.liferay.faces.util.context.PartialResponseWriterWrapper.<init>(PartialResponseWriterWrapper.java:31)
    at com.liferay.faces.util.context.PartialViewContextCleanupImpl$PartialResponseWriterCleanupImpl.<init>(PartialViewContextCleanupImpl.java:198)
    at com.liferay.faces.util.context.PartialViewContextCleanupImpl.getPartialResponseWriter(PartialViewContextCleanupImpl.java:177)
    at org.primefaces.context.PrimePartialViewContext.getPartialResponseWriter(PrimePartialViewContext.java:71)
    at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:197)
    at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:124)
    at javax.faces.context.ExceptionHandlerWrapper.handle(ExceptionHandlerWrapper.java:97)
    at com.liferay.faces.bridge.context.ExceptionHandlerAjaxImpl.handle(ExceptionHandlerAjaxImpl.java:85)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at com.liferay.faces.bridge.BridgePhaseResourceImpl.execute(BridgePhaseResourceImpl.java:107)
    ... 136 more

如何下载文件?

我找到了解决方案,获取 Liferay 的 ServletResponse 并将字节数组中的 InputStream 传递给它很重要。

public StreamedContent getDownloadFileStreamContent(final GridFSDBFile noteAndFile)
{
    try
    {
        PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext()
                .getResponse();
        HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
        res.setHeader("Content-Disposition", "attachment; filename=\"" + noteAndFile.getFilename() + "\"");//
        res.setHeader("Content-Transfer-Encoding", "binary");
        res.setContentType(noteAndFile.getContentType());
        res.getOutputStream().write(IOUtils.toByteArray(noteAndFile.getInputStream()));
        res.flushBuffer();

        showMessage(SUCCESS, "Archivo descargado");

    }
    catch (IOException ioe)
    {
        showMessage(ERROR, "Problemas al descargar el archivo");
        LOGGER.error("An error occurred uploading the file: " + ioe.getMessage(), ioe);
    }

    return null;
}

另外我用的是primefaces的组件

<p:commandButton value="Nuevo Download" ajax="false" update="growl">
    <p:fileDownload value="#{cc.attrs.noteAndFileBean.getDownloadFileStreamContent(noteAndFile)}" />
</p:commandButton>