zoukankan      html  css  js  c++  java
  • java以流的形式输出文件

    原文:http://blog.csdn.net/liutt55/article/details/78126614

      public void downProcessFile(HttpServletRequest request,HttpServletResponse response,String path){
            try {
                File file = new File(path);
                String filename = file.getName();// 获取日志文件名称
                InputStream fis = new BufferedInputStream(new FileInputStream(path));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                response.reset();
                // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
                response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
                response.addHeader("Content-Length", "" + file.length());
                OutputStream os = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                os.write(buffer);// 输出文件
                os.flush();
                os.close();
            } catch (Exception e) {
            }
        }
  • 相关阅读:
    菜根谭#77
    菜根谭#76
    菜根谭#75
    菜根谭#74
    菜根谭#73
    python迭代器
    python爬取网页数据
    yii2验证规则
    python装饰器的理解
    php中多图上传采用数组差集处理(array_diff,array_map)
  • 原文地址:https://www.cnblogs.com/shihaiming/p/8464825.html
Copyright © 2011-2022 走看看