zoukankan      html  css  js  c++  java
  • springboot下载文件

    1.后端:
    public void getExcelTemplate(HttpServletResponse response) throws UnsupportedEncodingException {

       //文件名最好不要用中文,会出现中文乱码
    InputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream("/template/template.xlsx"));

    response.reset();
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("模板.xlsx", "UTF-8"));

    // 循环取出流中的数据
    byte[] b = new byte[1024];
    int len;
    try {
    while ((len = inputStream.read(b)) > 0)
    response.getOutputStream().write(b, 0, len);
    inputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    2.前端
    可以直接使用window.location.href方式打开
    downloadTemplate() {
    window.location.href = "http://localhost:8080/getTemplate";
    }
  • 相关阅读:
    数据库基础-INDEX
    LINQ教程
    NPOI导出EXCEL
    WPF数据双向绑定
    WPF控件数据单项绑定
    HelloWorld IL代码
    Python基础教程(英文视频教学)
    ado.net的5个主要对象
    Linux学习-0627
    C#中Abstract和Virtual
  • 原文地址:https://www.cnblogs.com/sanshao-ghf/p/14808675.html
Copyright © 2011-2022 走看看