java 在 Struts-2 中使用 iText 生成 PDF:结果类型流不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12265702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDF generation using iText in Struts-2 : result type stream not working
提问by Arun
My requirement is to generate PDF file using iText, I use below code to create a sample PDF
我的要求是使用 iText 生成 PDF 文件,我使用以下代码创建示例 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)
如果您在上面的代码中看到,iText 没有使用任何 inputStream 参数,而是直接写入响应的输出流。而 struts-2 要求我们使用 InputStream 参数(参见下面的配置)
<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
我知道我的班级应该有用于 inputStream 的 getter 和 setter,而且我在 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.
但是由于 iText 并不真正需要输入流,而是直接写入响应的输出流,因此我得到了异常,因为我没有为 inputStream 参数设置任何内容。
Please let me know how to use iText code in struts-2 having the resultType as stream
请让我知道如何在 struts-2 中使用结果类型作为流的 iText 代码
Thanks
谢谢
采纳答案by Arun
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
执行此 PDF 导出的操作中的方法可以是无效的。当我们直接写入响应的输出流时,不需要结果类型配置
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
感谢所有试图回答这个问题的人
回答by silver
Main Answer:
主要回答:
You can also return NONE
or return null
as explained in the Apache docs:
您也可以return NONE
或return null
如 Apache 文档中所述:
Returning ActionSupport.NONE (or null)from an action class method causes the results processing to be skipped. This is useful if the action fully handles the result processing such as writing directly to the HttpServletResponse OutputStream.
Source:http://struts.apache.org/release/2.2.x/docs/result-configuration.html
从操作类方法返回ActionSupport.NONE(或 null)会导致跳过结果处理。如果操作完全处理结果处理(例如直接写入 HttpServletResponse OutputStream),这将非常有用。
来源:http ://struts.apache.org/release/2.2.x/docs/result-configuration.html
Example:
例子:
O'Reilly offers a tutorial on Dynamically Creating PDFs in a Web Applicationusing Servlets (S.C. Sullivan, 2003). It can be converted to a Struts2 action class as shown below.
O'Reilly 提供了有关使用 Servlet在 Web 应用程序中动态创建 PDF的教程(SC Sullivan,2003 年)。它可以转换为 Struts2 动作类,如下所示。
It is good to have a helper class like PDFGenerator
to create the PDF for you and return it as a ByteArrayOutputStream
.
有一个帮助类PDFGenerator
可以为您创建 PDF 并将其作为ByteArrayOutputStream
.
PDFGenerator
class:
PDFGenerator
班级:
import java.io.ByteArrayOutputStream;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class PDFGenerator {
public ByteArrayOutputStream generatePDF() throws DocumentException {
Document doc = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(doc, baosPDF);
try {
doc.open();
// create pdf here
doc.add(new Paragraph("Hello World"));
} catch(DocumentException de) {
baosPDF.reset();
throw de;
} finally {
if(doc != null) {
doc.close();
}
if(pdfWriter != null) {
pdfWriter.close();
}
}
return baosPDF;
}
}
You can now call it in your action class.
您现在可以在您的操作类中调用它。
ViewPDFAction
class:
ViewPDFAction
班级:
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.yoursite.helper.PDFGenerator;
import com.opensymphony.xwork2.ActionSupport;
public class ViewPDFAction extends ActionSupport
implements ServletResponseAware {
private static final long serialVersionUID = 1L;
private HttpServletResponse response;
@Override
public String execute() throws Exception {
ByteArrayOutputStream baosPDF = new PDFGenerator().generatePDF();
String filename = "Your_Filename.pdf";
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"inline; filename=" + filename); // open in new tab or window
response.setContentLength(baosPDF.size());
OutputStream os = response.getOutputStream();
os.write(baosPDF.toByteArray());
os.flush();
os.close();
baosPDF.reset();
return NONE; // or return null
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
web.xml
:
web.xml
:
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
struts.xml
:
struts.xml
:
<action name="viewpdf" class="com.yoursite.action.ViewPDFAction">
<!-- NO CONFIGURATION FOR RESULT NONE -->
</action>