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 文件 两大技术
在学校时 ,总感觉自己萌萌哒 ,结果工作之后自己咻咻咻 ,还要学习的东西很多啦
希望自己的建议可以帮助各位道友 加油 一直在路上