Use the output stream to solve the problem of displaying documents (PDF, etc.) in web pages (JSP)

Source: Internet
Author: User

I saw a post today and asked questions about application/pdf. I tried it by myself.

The details are given belowCode

Add mime information to the Web. xml of the project (you can manually change it to another format, such as word)

<Mime-mapping> <extension> pdf </extension> <mime-type> application/pdf </mime-type> </mime-mapping>

Read the PDF file in the servlet and output it to the browser for display. However, this method should be very memory-consuming. If all the large files have been read for display

Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("application/pdf; charset = UTF-8"); outputstream out = response. getoutputstream (); // response output stream fileinputstream inputstream = new fileinputstream ("D:/a.pdf"); // File Read stream filechannel channel = inputstream. getchannel (); // file channel writablebytechannel channel_out = channels. newchannel (out); // response output channel. transferto (0, channel. size (), channel_out); // transfers the file channel to the response channel for output out. flush (); out. close ();}

Last display

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.