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

    复制代码
    //vue element-ui
    <el-button size="medium" type="primary" @click="download">导出</el-button>
      //js
      downLoad(){
         window.location.href="/api/downLoad";
      },
     
    复制代码
    复制代码
    //后台java

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
    import java.util.Map;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;


    @RestController
    public class DownLoadFile {

      private static Logger log = LoggerFactory.getLogger(DownLoadFile.class);

      @RequestMapping(value = "/downLoad", method = RequestMethod.GET)
      public static final String downLoad(HttpServletResponse res) throws UnsupportedEncodingException {
        Map<String, Object> reMap = new HashMap<>();
        //文件名 可以通过形参传进来
        String fileName = "t_label.txt";
        //要下载的文件地址 可以通过形参传进来
        String filepath = "f:/svs/" + fileName;

        OutputStream os = null;
        InputStream is = null;
        try {
          // 取得输出流
          os = res.getOutputStream();
          // 清空输出流
          res.reset();
          res.setContentType("application/x-download;charset=GBK");
          res.setHeader("Content-Disposition",
            "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso-8859-1"));
          // 读取流
          File f = new File(filepath);
          is = new FileInputStream(f);
          if (is == null) {
            reMap.put("msg", "下载附件失败");
          }
          // 复制
          IOUtils.copy(is, res.getOutputStream());
            res.getOutputStream().flush();
          } catch (IOException e) {
            reMap.put("msg", "下载附件失败,error:" + e.getMessage());
          }
          // 文件的关闭放在finally中
          finally {
            try {
              if (is != null) {
                is.close();
              }
            } catch (IOException e) {
              log.error(e.toString());
            }
            try {
              if (os != null) {
                os.close();
              }
            } catch (IOException e) {
              log.error(e.toString());
            }
          }
          String str = JsonUtil.map2Json(reMap);
          return str;
        }

    }

     转自:https://www.cnblogs.com/xuchao0506/p/12681480.html

  • 相关阅读:
    SQL Server 中,将多行转换为一行,用某个符号隔开的SQL 语句
    JQ 轻松学会$.get(),$.post(),$ajax()的作用和用法
    Dynamics 365 组织服务查询时,关于输入时间和输出时间的时区问题讲解
    在Dynamics 365的标准窗体,lookup字段变成了[Object Object],picklist直接展示数字
    C# 回顾正则表达式的常用语法
    观洛马琴科对决洛佩兹比赛有感
    读取硬盘序列号
    delphi 数组复制利用CopyMemory 最为完美
    MFC多线程通讯--自定义消息
    MFC 多线程及线程同步
  • 原文地址:https://www.cnblogs.com/javalinux/p/14807391.html
Copyright © 2011-2022 走看看