我遇到了这个错误 - "应用程序/pdf"不是受支持的MIME类型。我正在尝试将 p:editor 的内容保存为 pdf 到我的本地机器。
进口
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;
功能
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;
}
}
在以下位置发表了一篇出色的帖子: https://forum.primefaces.org/viewtopic.php?f=3&t=21342&p=170835#p170835
请帮忙!
编辑------------------
如果我使用 mime 类型作为图像/jpg,那么我也会收到同样的错误。问题看起来像其他东西。谁能指出我.
javax.portlet.faces.BridgeException: java.lang.IllegalArgumentException: image/jpg is not a supported mime type
感谢 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;
}