简体   繁体   English

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

[英]How download a file or InpuStream in portlet using Liferay?

I have a button in the UI that calls method for downloading a file which takes an InpuStream type. 我在UI中有一个按钮,它调用下载采用InpuStream类型的file方法。 This is my code 这是我的代码

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

and the bean 和豆

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");
    }
}

The GridFSDBFile type is an object that stores files in mongo then for get the InputStream I call getInputStream() then I pass it to byte array . GridFSDBFile类型是一个对象,它在mongo中存储文件然后为了获取InputStream我调用getInputStream()然后我将它传递给byte array

When I call this method I receive a big exception (Part of it) 当我调用这个方法时,我收到一个很大的例外(部分)

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.access$300(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

How can I download the file? 我该如何下载文件?

I found the solution, It's important get Liferay's ServletResponse and pass it the InputStream in byte array. 我找到了解决方案,重要的是获取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;
}

Adicionaly I use the primefaces's component Adicionaly我使用primefaces的组件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用nodeJS下载保存在gridFS中的文件 - how to download a file saved in gridFS using nodeJS 从Liferay Portlet中的纯osgi软件包获取osgi服务参考 - Get osgi service reference from pure osgi bundle in Liferay portlet 如何使用 Golang 从 Mongo GridFS 下载文件? - How to download a file from Mongo GridFS using Golang? 如何使用浏览器下载管理器从 express + mongoDB 服务器下载/流式传输文件 - How to download/stream a file from a express + mongoDB server, using the browser download manager 使用流星上传和下载文件 - File upload and download using meteor 如何在 React/Javascript 客户端中下载使用 GridFS 存储并使用 GraphQL 返回的文件? - How to download a file stored using GridFS and returned using GraphQL in the React/Javascript Client? 在Golang中使用griddfs从Mongodb下载文件 - Download file from Mongodb using griddfs in golang 如何使用delphi mongo-delphi-driver.GridFS.pas从MongoDB下载文件? - How can I download a file from MongoDB using delphi mongo-delphi-driver.GridFS.pas? 如何在Spring MVC和MongoDB中下载文件 - How to download file in Spring mvc and mongodb 如何使用iron-router(和ground-db)下载mongo集合作为文件? - How to download mongo collections as file using iron-router (and ground-db)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM