流的方式预览文件

流的方式预览文件 

 页面预览文件的时候,避免暴露服务器目标文件的所在地址,而是通过一个请求,把文件流直接输出展示。

    /**
     * 读取文件
     * 
     * @param request
     * @param response
     * @param path
     */
    @RequestMapping(value = "/readFile", method = {RequestMethod.POST,RequestMethod.GET})
    public void readFile(HttpServletRequest request, HttpServletResponse response, Long id) {

     //调用ServletOutputStream或者Writer之前有效。
     //当使用的缓存已满时,容器必须立刻刷新把缓存的内容发送到客户端,如果这是第一个被发送到客户端的数据,那么response也会被认为是已提交
response.reset(); InputStream in
= null; OutputStream output = null;      try { File file = new File(ctxPath + File.separator + fileName); //输出文件流 if (file.exists()) { output = response.getOutputStream(); in = new FileInputStream(file); byte tmp[] = new byte[256]; int i = 0; while ((i = in.read(tmp)) != -1) { output.write(tmp, 0, i); } output.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != in) { in.close(); } if (null != output) { output.close(); } } catch (Exception e) { e.printStackTrace(); } } }

 

转载于:https://www.cnblogs.com/meitanzai/p/5825792.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值