Portlet应用程序/ pdf不是受支持的mime类型错误

时间:2018-10-11 08:42:02

标签: pdf jsf primefaces portlet portletbridge

我遇到此错误-“ application / pdf”不是受支持的mime类型。我正在尝试将p:editor的内容另存为pdf到本地计算机。

进口

if (this.currentQuerySubscription != null) {
   this.currentQuerySubscription.unsubscribe();
}

功能

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;

以下精彩文章来自https://forum.primefaces.org/viewtopic.php?f=3&t=21342&p=170835#p170835

请帮助!

编辑------------------

如果我将mime类型用作图片/ jpg,那么我也会收到相同的错误。问题看起来像其他东西。谁能指出我的意思。

        public StreamedContent getFile() {
                try {
                     ByteArrayOutputStream os = new ByteArrayOutputStream();

                     Document document = new Document(PageSize.LETTER);
                     PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
                     document.open();
                     document.addCreationDate();
                    HTMLWorker htmlWorker = new HTMLWorker(document);
                     String str = "<html><head></head><body>"+ this.getMessage() +"</body></html>";
                     htmlWorker.parse(new StringReader(str));
                     document.close();

                     InputStream is = new ByteArrayInputStream(os.toByteArray());

                     file = new DefaultStreamedContent(is, "application/pdf", "ohyeah.pdf");
                     return file;
                  }
                  catch (Exception e) {
            return null;
                  }
            }

1 个答案:

答案 0 :(得分:-1)

感谢Melloware指出该错误与Portlet相关。

如果任何人都面临相同的问题,这是对我有用的解决方案。根据您的需要进行调整。

public StreamedContent getFile() throws IOException, DocumentException {
        final PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        final HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
        res.setContentType("application/pdf");
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        //res.setHeader("Content-Disposition", "attachment; filename=\".pdf\"");
        res.setHeader("Content-Disposition", "attachment; filename="+subject+".pdf");
        res.setHeader("Refresh", "1");
        res.flushBuffer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out=res.getOutputStream();
        Document document = new Document(PageSize.LETTER);
        PdfWriter.getInstance(document, baos);
        document.open();
        document.addCreationDate();
        HTMLWorker htmlWorker = new HTMLWorker(document);
        String str =this.getMessage();
        htmlWorker.parse(new StringReader(str));
        document.close();
        baos.writeTo(out);
        out.flush();
        out.close();
        return null;
    }