zoukankan      html  css  js  c++  java
  • 【日常笔记】java文件下载返回数据流形式

        @RequestMapping("/downloadFile")
        @ResponseBody
        public void download(String uploadPathUrl, HttpServletRequest request,    HttpServletResponse resp) throws Exception {
            
            //获取服务器绝对路径  这里获取的是配置文件中所配置的地址
            String path =PropertiesUtil.getInstance().getSysPro("uploadPath")+"//"+uploadPathUrl;
            log.info("下载路径:"+path);
            // 输出响应正文的输出流
            OutputStream out;
            // 读取本地文件的输入流
            InputStream in;
            // 获得本地输入流
            File file = new File(path);
            in = new FileInputStream(file);
            // 设置响应正文的MIME类型
            resp.setContentType("Content-Disposition;charset=GB2312");
            resp.setHeader("Content-Disposition", "attachment;" + " filename="+ new String(path.getBytes(), "ISO-8859-1"));
            // 把本地文件发送给客户端
            out = resp.getOutputStream();
            int byteRead = 0;
            byte[] buffer = new byte[512];
            while ((byteRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteRead);
            }
            in.close();
            out.flush();
            out.close();
        }

    日常笔记 文件下载

  • 相关阅读:
    全文本搜索神器
    唯一索引和普通索引怎么选择
    程序员应不应该搞全栈
    c 的陷阱
    抽象能力
    电影电视剧推荐
    系统故障诊断
    一次web网站被入侵的处理记录
    Spark RDD 操作
    (转)Mysql哪些字段适合建立索引
  • 原文地址:https://www.cnblogs.com/miskis/p/5505225.html
Copyright © 2011-2022 走看看