Servlet Output PDF Document Method

zhaozj2021-02-16  82

Overview Java Servlet programming can easily send HTML files to the client's web browser. However, many sites also allow access to non-HTML format documents, including Adobe PDF, Microsoft Word, and MICORSOFT EXCEL. In fact, these non-HTML formats can be sent using a servlet as long as they can be represented by MIME type. This article will take PDF as an example to show you how to transfer non-HTML format files using servlets and how this file is generated by Java. You can use servlet to open a file in your browser using servlets in the output stream of the Servlet. First start from the output stream of the servlet: servletOutputStream out = res. maxPutStream (); Internet Using MIME (MultiPOS Internet Mail Extension Multi-purpose Internet Mail Extension Protocol) to transfer mixed format, multimedia and binary data files. If you want to open a document in the Servlet's Response object, you must set the MIME type of the document. Send a PDF document to the web client (1) MIME Type Web Browser uses the MIME type to identify non-HTML documents and determine how data within the document. Use the plug-in to use the MIME type, and when the web browser downloads the document indicated by the MIME type, you can start the corresponding plugin to process this document. Some MIME types can also be used in conjunction with external programs, and the corresponding external program will be launched after the browser downloads the document. MIME type is very useful. They allow web browsers to handle documents in different formats, but do not need to embed relevant knowledge in advance. Java Servlets can use the MIME type to deliver non-HTML files to your browser, such as Adobe PDF and MICORSOFT WORD. Using the correct MIME type ensures that these non-HTML files are displayed correctly or external programs. The MIME type of the PDF file is "Application / PDF". To open a PDF document with a servlet, you need to set the contentTy of the HEADER in the Response object to "Application / PDF": // PDF file MIME Type Res.SetContentType ("Application / PDF"); // can also pass the following Way to set

Response.setHeader ("Content-Type", "Application / PDF"); (2) Content Disposition HTTP Response Header Content-Disposition Allows the servlet to specify the information represented by the document. With this header, you can specify a document separately (instead of opening in the browser), and can also be displayed according to the user's operation. If the user wants to save the document, you can also recommend a file name for the document. This suggestion name will appear in the File Name column of the Save AS dialog. If not specified, the name of the servlet will appear in the dialog. In servlet, you need to set the header to this: res.sethead, "attachment; filename =" "example.pdf"); // attachment - because you don't want to open directly in your browser It uses Adobe Acrobat. // You can determine the suggested name at the time of saving text by setting the default file name. //Response.setheader ("inline; filename = report.pdf"); (3) The package is not very simple after completing the above work, and remains very simple. You need to create a java.net.URL object based on the name of the file to be sent. The string to the URL constructor must be a valid URL address pointing to the file. Here, I want to open the local PDF document: String fileurl = "http://localhost/aboutadobe/careeropp/pdfs/tables.pdf; URL string can also be similar to http://www.gr.com/pub/ Somefile.doc or http://www.xls. But you must ensure that the file type to be transmitted is consistent with the MIME type previously set in the HTTP Response object. (4) Before you start reading the document is first obtained from the URL input stream InputStream objects, with the InputStream BufferedInputStream encapsulate BufferedInputStreambis = newBufferedInputStream (url.openStream ());. Once you have done, it is simply the InputStream the bytes written to the the output stream OutputStream servlet:! BufferedOutputStreambos = new BufferedOutputStream (out); byte [] buff = new byte [2048]; intbytesRead; // a simple read and write cycles while (-1 = (bytesRead = bis.read (buff , Buff.length)) {bos.write (buff, 0, bytesread);} In the final block, close these streams, such as: bos.close (); PDF documentation in the server-side generation utilization ITEXT040 tool The package can be very convenient to output a very beautiful PDF document.

转载请注明原文地址:https://www.9cbs.com/read-14173.html

New Post(0)