在Struts-2中使用iText生成PDF:结果类型流不起作用 [英] PDF generation using iText in Struts-2 : result type stream not working

查看:188
本文介绍了在Struts-2中使用iText生成PDF:结果类型流不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是使用iText生成PDF文件,我使用下面的代码创建示例PDF

 文档document = new文献(); 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document,baos);
document.open();
document.add(新段落(成功PDF来自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参数(参见下面的配置)

 < action name =exportReportclass = com.export.ExportReportAction > 
< result name =pdftype =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中

  private InputStream inputStream; 
public InputStream getInputStream(){
return inputStream;
}

public void setInputStream(InputStream inputStream){
this.inputStream = inputStream;
}

但是因为iText并不真正需要输入流而是直接写入响应输出流,我得到异常,因为我没有为inputStream参数设置任何东西。



请告诉我如何在struts-2中使用iText代码,将resultType作为流



谢谢<找到解决方案



执行此PDF导出可能无效。我们直接写入响应的输出流时不需要结果类型配置



例如,以这种方式使用您的动作类

 类ExportReportAction扩展ActionSupport {
public void exportToPdf(){//无返回类型
try {
文档文档=新文档() ;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document,baos);
document.open();
document.add(新段落(成功PDF来自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(例外e){
// catch
}

}
}

并以这种方式进行struts配置

 < action name =exportReportclass =com.export.ExportReportAction> 
<! - 不需要具有结果类型的流配置 - >
< / action>

这很酷!!!



感谢所有试图回答这个问题的人


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();

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>

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

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.

Please let me know how to use iText code in struts-2 having the resultType as stream

Thanks

解决方案

Found solution to this.

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
    }

  } 
}

and have your struts-configuration this way

<action name="exportReport" class="com.export.ExportReportAction"> 
 <!-- NO NEED TO HAVE RESULT TYPE STREAM CONFIGURATION-->
</action>

this works cool !!!

Thanks for all who attempted to answer this question

这篇关于在Struts-2中使用iText生成PDF:结果类型流不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆