我的要求是使用 iText 生成 PDF 文件,我使用以下代码创建示例 PDF
My requirement is to generate PDF file using iText, I use below code to create a sample PDF
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("success PDF FROM STRUTS"));
document.close();
ServletOutputStream outputStream = response.getOutputStream() ;
baos.writeTo(outputStream);
response.setHeader("Content-Disposition", "attachment; filename="stuReport.pdf"");
response.setContentType("application/pdf");
outputStream.flush();
outputStream.close();
如果您在上面的代码中看到,iText 没有使用任何 inputStream 参数,而是直接写入响应的输出流.而 struts-2 要求我们使用 InputStream 参数(见下面的配置)
If you see in the above code, iText is not using any inputStream parameter, rather it is writing directly to response's outputstream. Whereas struts-2 is mandating us to use InputStream parameter (see the configuration below)
<action name="exportReport" class="com.export.ExportReportAction">
<result name="pdf" type="stream">
<param name="inputName">inputStream</param>
<param name="contentType">application/pdf</param>
<param name="contentDisposition">attachment;filename="sample.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
我知道我的班级应该有用于 inputStream 的 getter 和 setter,而且我在 struts-configuration 中提到的班级中也有这个
I know that my class should have getters and setters for inputStream and i have that too in the class mentioned in struts-configuration
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
但由于 iText 并不真正需要输入流,而是直接写入响应的输出流,因此我得到了异常,因为我没有为 inputStream 参数设置任何内容.
But since iText doesn't really need inputstream rather it is writing directly to response's outputstream, i get exceptions since am not setting anything for the inputStream parameter.
请告诉我如何在 struts-2 中使用 iText 代码并将 resultType 作为流
Please let me know how to use iText code in struts-2 having the resultType as stream
谢谢
找到解决方案.
执行此 PDF 导出的操作中的方法可以是无效的.当我们直接写入响应的输出流时,不需要结果类型配置
The method in the action which performs this PDF export can be void. The result type configuration is not needed while we are writing directly to response's outputstream
例如,以这种方式设置您的操作类
for example, have your action class this way
Class ExportReportAction extends ActionSupport {
public void exportToPdf() { // no return type
try {
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("success PDF FROM STRUTS"));
document.close();
ServletOutputStream outputStream = response.getOutputStream() ;
baos.writeTo(outputStream);
response.setHeader("Content-Disposition", "attachment; filename="stuReport.pdf"");
response.setContentType("application/pdf");
outputStream.flush();
outputStream.close();
}catch (Exception e) {
//catch
}
}
}
并以这种方式进行 struts 配置
and have your struts-configuration this way
<action name="exportReport" class="com.export.ExportReportAction">
<!-- NO NEED TO HAVE RESULT TYPE STREAM CONFIGURATION-->
</action>
这很酷!!!
感谢所有试图回答这个问题的人
Thanks for all who attempted to answer this question
这篇关于在 Struts-2 中使用 iText 生成 PDF:结果类型流不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!