当前位置: 技术问答>java相关
iText servlet PDF文件输出问题,高分求救!
来源: 互联网 发布时间:2017-03-17
本文导语: 运行下面Serlet代码,怎么显示不出来?请专家们解答。 import java.io.PrintWriter; import java.io.IOException; import java.io.File; import java.util.Date; import javax.servlet.http.*; import javax.servlet.*; import com.lowagie.text.*; import com.lowagie.tex...
运行下面Serlet代码,怎么显示不出来?请专家们解答。
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
public class Chap0105_1 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Document document = new Document();
try {
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline;filename="Example.pdf"");
PdfWriter.getInstance(document, response.getOutputStream());
// step 3
document.open();
// step 4
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph("Hello World"));
}
catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: we close the document (the outputstream is also closed internally)
document.close();
}
}
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
public class Chap0105_1 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Document document = new Document();
try {
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline;filename="Example.pdf"");
PdfWriter.getInstance(document, response.getOutputStream());
// step 3
document.open();
// step 4
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph("Hello World"));
}
catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: we close the document (the outputstream is also closed internally)
document.close();
}
}
|
Document document = new Document();//创建一Document对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();//
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Phrase("你好世界"));
document.close();
response.setContentType("application/pdf");
response.setContentLength(baos.size());
ServletOutputStream pout = response.getOutputStream();
baos.writeTo(pout);
pout.flush();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Phrase("你好世界"));
document.close();
response.setContentType("application/pdf");
response.setContentLength(baos.size());
ServletOutputStream pout = response.getOutputStream();
baos.writeTo(pout);
pout.flush();
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。