zoukankan      html  css  js  c++  java
  • 流的方式预览文件

    流的方式预览文件 

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

        /**
         * 读取文件
         * 
         * @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(); } } }
  • 相关阅读:
    hash
    C#执行Sql事务处理
    数据库的锁表
    页面的刷新 和图片的替换
    单点登录 Webservice
    js 动态调用js文件
    .net生成EXCEL
    JS : 连续滚动
    引用指定类型的对象
    对象序列化为字符串
  • 原文地址:https://www.cnblogs.com/meitanzai/p/5825792.html
Copyright © 2011-2022 走看看