根据已有pdf下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36675996/article/details/80606190
        // 下载PDF
        @RequestMapping(value = "")//映射路径
        public void downloadPDF(HttpServletResponse response) { 
            //pdf所在路径
            String pdffilePath = xxx;
            //pdf文件名
            String fileName = xxx;
            // 读到流中
                InputStream inStream = null;
                try {
                    File file =new File(pdffilePath);
                     inStream = new FileInputStream(file);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                // 设置输出的格式
                response.reset();
                response.setContentType("bin");
                try {
                    response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859_1") + "\"");
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }
                // 循环取出流中的数据
                byte[] b = new byte[10240];
                int len;
                try {
                    while ((len = inStream.read(b)) > 0)
                        response.getOutputStream().write(b, 0, len);
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

猜你喜欢

转载自blog.csdn.net/qq_36675996/article/details/80606190