zoukankan      html  css  js  c++  java
  • 浏览器下载java项目中的文件

    有一些文件保存在项目中需要在浏览器打开的页面中下载,需要找准文件的存储路径

    1. 工程格式

      

             

     2.代码

    public String execute() throws Exception {

    HttpServletResponse response = ServletActionContext.getResponse();
    // 下载本地文件
    String fileName = new String("例行检查报告模板.docx"); // 文件的默认保存名
    InputStream inStream =ServletActionContext.getServletContext().getResourceAsStream("WEB-INF/例行检查报告模板.doc");

    // 读到流中
    // InputStream inStream =this.getClass().getResourceAsStream("/WEB-INF/例行检查报告模板.docx"); // 文件的存放路径
    // 设置输出的格式
    response.setContentType("application/octet-stream");
    response.setCharacterEncoding("utf-8");
    response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8"));
    // 循环取出流中的数据
    byte[] b = new byte[100];
    int len;
    try {
    while ((len = inStream.read(b)) > 0) {
    response.getOutputStream().write(b, 0, len);
    }
    inStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }


    return null;

    }

  • 相关阅读:
    JS的type类型为 text/template
    Vue之x-template(2)
    Vue之x-template(1)
    vue之$mount
    console.log()与console.dir()
    Less用法注意事项
    一次 Linux 系统被攻击的分析过程
    WebAR 如何改变增强现实的未来
    开发中的测试名词解释
    Flutter 同步系统的 HTTP 代理设置
  • 原文地址:https://www.cnblogs.com/cuiguangpeng/p/14207757.html
Copyright © 2011-2022 走看看