• <bdo id='g5Gy0'></bdo><ul id='g5Gy0'></ul>
  • <small id='g5Gy0'></small><noframes id='g5Gy0'>

    1. <tfoot id='g5Gy0'></tfoot>
      <legend id='g5Gy0'><style id='g5Gy0'><dir id='g5Gy0'><q id='g5Gy0'></q></dir></style></legend>
      1. <i id='g5Gy0'><tr id='g5Gy0'><dt id='g5Gy0'><q id='g5Gy0'><span id='g5Gy0'><b id='g5Gy0'><form id='g5Gy0'><ins id='g5Gy0'></ins><ul id='g5Gy0'></ul><sub id='g5Gy0'></sub></form><legend id='g5Gy0'></legend><bdo id='g5Gy0'><pre id='g5Gy0'><center id='g5Gy0'></center></pre></bdo></b><th id='g5Gy0'></th></span></q></dt></tr></i><div id='g5Gy0'><tfoot id='g5Gy0'></tfoot><dl id='g5Gy0'><fieldset id='g5Gy0'></fieldset></dl></div>
      2. 在 Struts-2 中使用 iText 生成 PDF:结果类型流不起作用

        时间:2023-09-25
          <tbody id='DuwWU'></tbody>
          • <legend id='DuwWU'><style id='DuwWU'><dir id='DuwWU'><q id='DuwWU'></q></dir></style></legend>

              <tfoot id='DuwWU'></tfoot>
            1. <i id='DuwWU'><tr id='DuwWU'><dt id='DuwWU'><q id='DuwWU'><span id='DuwWU'><b id='DuwWU'><form id='DuwWU'><ins id='DuwWU'></ins><ul id='DuwWU'></ul><sub id='DuwWU'></sub></form><legend id='DuwWU'></legend><bdo id='DuwWU'><pre id='DuwWU'><center id='DuwWU'></center></pre></bdo></b><th id='DuwWU'></th></span></q></dt></tr></i><div id='DuwWU'><tfoot id='DuwWU'></tfoot><dl id='DuwWU'><fieldset id='DuwWU'></fieldset></dl></div>
            2. <small id='DuwWU'></small><noframes id='DuwWU'>

                • <bdo id='DuwWU'></bdo><ul id='DuwWU'></ul>
                  本文介绍了在 Struts-2 中使用 iText 生成 PDF:结果类型流不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的要求是使用 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:结果类型流不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 struts.xml 中为所有 Action 类添加拦截器 下一篇:如何显示所有可用的 Struts2 动作?

                  相关文章

                  <legend id='WWGvn'><style id='WWGvn'><dir id='WWGvn'><q id='WWGvn'></q></dir></style></legend>

                  <small id='WWGvn'></small><noframes id='WWGvn'>

                  1. <i id='WWGvn'><tr id='WWGvn'><dt id='WWGvn'><q id='WWGvn'><span id='WWGvn'><b id='WWGvn'><form id='WWGvn'><ins id='WWGvn'></ins><ul id='WWGvn'></ul><sub id='WWGvn'></sub></form><legend id='WWGvn'></legend><bdo id='WWGvn'><pre id='WWGvn'><center id='WWGvn'></center></pre></bdo></b><th id='WWGvn'></th></span></q></dt></tr></i><div id='WWGvn'><tfoot id='WWGvn'></tfoot><dl id='WWGvn'><fieldset id='WWGvn'></fieldset></dl></div>
                    <tfoot id='WWGvn'></tfoot>
                        <bdo id='WWGvn'></bdo><ul id='WWGvn'></ul>