美文网首页
JAVA url转二进制流(接收PDF)

JAVA url转二进制流(接收PDF)

作者: 各种萌新 | 来源:发表于2021-06-21 13:08 被阅读0次
    @RequestMapping("/PDF")
    @ResponseBody
    public synchronized void PDF(HttpServletRequest req,HttpServletResponse res,String bxCaseId) throws Exception {
        OutputStream out = null;
        try {
//这里是获取url的,业务需要就不改了,可以无视。
            String pdfPath = (String)approvalContractService.getPdf(bxCaseId).getMap().get("CONTRACT_FILE_URL");
            if(Strings.isEmpty(pdfPath)){
                throw new Exception();
            }
            URL url = new URL(pdfPath);
            URLConnection conn = url.openConnection();
            InputStream in = conn.getInputStream();
            out = res.getOutputStream();
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            int ch;
            while ((ch = in.read()) != -1) {   
                swapStream.write(ch);
            }
            res.setContentType("application/pdf;charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
             
            swapStream.writeTo(out);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

相关文章

网友评论

      本文标题:JAVA url转二进制流(接收PDF)

      本文链接:https://www.haomeiwen.com/subject/rcvnsltx.html