Download file, filename doesn't work in portlet

后端 未结 2 751
你的背包
你的背包 2021-01-27 13:12

I set filename in the HttpServletResponse header but when I download it has not this filename

FileInputStream fis = new FileInputStream(fileDocument         


        
2条回答
  •  面向向阳花
    2021-01-27 14:13

    If you are in the action or render phase of your portlet (what I would guess for a JSF portlet): there is a ResponseWrapper which will suppress any header. I'm using this method to find the response for sending binary data during the JSF lifecycle:

    public static HttpServletResponse getHttpServletResponse() {
        final FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext == null) {
            throw new IllegalStateException("Not inside a JSF request");
        }
        final Object responseObject = facesContext.getExternalContext().getResponse();
        if (responseObject instanceof HttpServletResponse) {
            return (HttpServletResponse) responseObject;
        }
        if (responseObject instanceof PortletResponse) {
            // Use Liferays util to find the real response
            HttpServletResponse response = PortalUtil.getHttpServletResponse((PortletResponse) responseObject);
            // Find the outer most response (setting the headers would have no effect, as we are included)
            while (response instanceof ServletResponseWrapper) {
                final ServletResponse servletResponse = ((ServletResponseWrapper) response).getResponse();
                if (!(servletResponse instanceof HttpServletResponse)) {
                    break;
                }
                response = (HttpServletResponse) servletResponse;
            }
            return response;
        }
        throw new IllegalStateException("Unknown type of response object: " + responseObject);
    }
    

提交回复
热议问题