Stream pdf file through servlet to browser

I have a typical servlet that passes PDF to a browser. PDF files are stored on the internal server from which my servlet is extracted. If I hit the servlet directly from the browser, pdf is displayed. If I try the same url in the tag <IMG>on the webpage, ... the broken feed.

Any insight into why this should be?

As an experiment, I can pass gifs streams without problems.

Here is the code that I pretty much removed from the inner layers:

public class PdfServlet extends HttpServlet {

    private static final Logger log = Logger.getLogger(PdfServlet.class.getName());

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) {

        String url = (String) req.getParameter("url");

        log.info("The URL is " + url);
        String format = "application/pdf";
//           String format = "image/gif";

        streamBinaryData(url, format, res);

    }

    /*
     * This Method Handles streaming Binary data
     * <p>
     * @param String urlstr ex: http;//localhost/test.pdf etc.
     * @param String format ex: pdf or audio_wav or msdocuments etc.
     * @param ServletOutputStream outstr
     * @param HttpServletResponse resp
     */
    private void streamBinaryData(
            String urlstr,
            String format,
            HttpServletResponse resp) {

        ServletOutputStream outstr = null;
        String ErrorStr = null;

        try {
            outstr = resp.getOutputStream();

            //find the right MIME type and set it as contenttype
            resp.setContentType(format);
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                URL url = new URL(urlstr);
                URLConnection urlc = url.openConnection();
                int length = urlc.getContentLength();

                resp.setContentLength(length);
//                resp.setHeader("Content-Length", String.valueOf(+length));
//                resp.setHeader("Content-Disposition", "inline");

                // Use Buffered Stream for reading/writing.
                InputStream in = urlc.getInputStream();
                bis = new BufferedInputStream(in);
                bos = new BufferedOutputStream(outstr);
                byte[] buff = new byte[length];
                int bytesRead;
                // Simple read/write loop.
                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                    log.info("Got a chunk of " + bytesRead);
                    bos.write(buff, 0, bytesRead);
                }
            } catch (Exception e) {
                e.printStackTrace();
                ErrorStr = "Error Streaming the Data";
                outstr.print(ErrorStr);
            } finally {
                log.info("finally!!!");
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (outstr != null) {
                    outstr.flush();
                    outstr.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

... and an HTML file. PDF does not work with a broken pipe, and the gif image is displayed even if the content type is returned as "application / pdf".

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Cheesy Servlet Experiment</title>
    </head>
    <body>
        <h1>Cheesy Servlet Experiment</h1>

        <P>
            <img src="http://10.0.0.9/ServletExperiment/pdf?url=http%3a%2f%2fwww.samplepdf.com%2fsample.pdf" alt="yah mon">
        <P>
            <img src="http://10.0.0.9/ServletExperiment/pdf?url=http%3a%2f%2fbbs.homeshopmachinist.net%2fimages%2fstatusicon%2fforum_new.gif" alt="yah mon">
    </body>
</html>

Edit - the following works in FF. I do not know how standard this is.

<object data="http://www.samplepdf.com/sample.pdf" type="application/pdf" width="600" height="600">
    alt : <a href="http://www.samplepdf.com/sample.pdf">test.pdf</a>
</object>

. .

+3
4

PDF img? . , , , .

. , . , GIF.

+4

, PDF img. img , JPEG, GIF PNG.

PDF- . PDF , PDF.

, , HTML- PDF. , target="_blank", .

+3

( img, IMG IMG src, ). Href servelt, PDF . make suer .

+1

: length . , .

byte [] read_buffer = new byte[1024 * 10] , 10kb.

And write those 10kb on bos.

Save this loop until you get it -1using the function read.

And yes. You do not use the img tag for pdf

0
source

Source: https://habr.com/ru/post/1768177/


All Articles