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 文件 两大技术

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

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

 

转载于:https://www.cnblogs.com/zhukaixin/p/9151171.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值