• java 实现在线阅读 .pdf


    1.资源的本地地址

    2.设置响应头

    3.代码实现

     1 @ResponseBody
     2     @RequestMapping(value = "/read")
     3     @ApiOperation(value="阅读", httpMethod = "GET", notes = "阅读")
     4     public String read(@ApiParam(required = true, name="id", value="")@RequestParam(value="id", required=true)String id,
     5                        HttpServletRequest request,HttpServletResponse response) throws IOException {
     6         PeriodicalResource periodicalResource = periodicalResourceService.get(id);
     7         String filePath = periodicalResource.getAttachment();
     8         File file = new File(periodicalBaseDir + filePath);//本地资源位置
     9         if (file.exists()) {
    10             response.setContentType("text/html; charset=UTF-8");//文本&编码
    11             response.setContentType("application/pdf");//pdf
    12             byte[] buffer = new byte[16*1024];
    13             FileInputStream fis = null;
    14             BufferedInputStream bis = null;
    15             try {
    16                 fis = new FileInputStream(file);
    17                 bis = new BufferedInputStream(fis);
    18                 OutputStream os = response.getOutputStream();
    19                 int i = bis.read(buffer);
    20                 while (i != -1) {
    21                     os.write(buffer, 0, i);
    22                     i = bis.read(buffer);
    23                 }
    24             } catch (Exception e) {
    25                 e.printStackTrace();
    26             } finally {
    27                 if (bis != null) {
    28                     try {
    29                         bis.close();
    30                     } catch (IOException e) {
    31                         e.printStackTrace();
    32                     }
    33                 }
    34                 if (fis != null) {
    35                     try {
    36                         fis.close();
    37                     } catch (IOException e) {
    38                         e.printStackTrace();
    39                     }
    40                 }
    41             }
    42         }
    43         return null;
    44     }

    4.总结一下经验 记录一下实现过程

    虽然实现但是自己还是很懵逼 ,主要涉及的技术有 IO流,File 文件 两大技术

      在学校时 ,总感觉自己萌萌哒 ,结果工作之后自己咻咻咻 ,还要学习的东西很多啦

    希望自己的建议可以帮助各位道友    加油     一直在路上

  • 相关阅读:
    Oracle分析函数
    oracle row_number的使用
    lru缓存测试类
    注解测试类
    lucene测试类
    SVN中检出(check out) 跟导出(export) 的区别
    Lucene原理与代码分析
    Lucene入门基础教程
    linux的less命令
    day4 大纲笔记
  • 原文地址:https://www.cnblogs.com/zhukaixin/p/9151171.html
Copyright © 2020-2023  润新知