• 00006-java 下载一个excel模板(文件),前端layui按钮


    下载按钮:

    <button class="layui-btn layui-btn-sm" data-type="downTemplate">模板下载</button>
    

    对应方法:

    downTemplate:function () {
        window.open(ctx+"/download/template/customer");
    },
    

    java 控制层:

    
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import javax.servlet.http.HttpServletRequest;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    
    
    @Controller
    @RequestMapping("/download")
    public class DownloadController {
    
       private Logger logger = LoggerFactory.getLogger(getClass());
    
    
       @RequestMapping(value = "/template/customer")
       public ResponseEntity<byte[]> downloadTemp(HttpServletRequest request) throws IOException {
          String path = request.getSession().getServletContext().getRealPath("/");
          String newFileName = "客户信息模板" + ".xlsx";
          String fileName = "customer.xlsx";
          File file = FileUtils.getFile(path, "template", fileName);
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
          headers.setContentDispositionFormData("attachment", new String(
                newFileName.getBytes("gbk"), "iso-8859-1"));
          return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.OK);
       }
    
    }
    

    customer.xlsx 是放在webapp/template目录下。

  • 相关阅读:
    MySQL优化—工欲善其事,必先利其器(2)
    MySQL优化—工欲善其事,必先利其器之EXPLAIN
    Linux工具安装和常用配置
    .Net Core配置文件介绍
    Centos7上开发.Net Core项目
    VMWare的host-only/bridged/NAT连接图文介绍
    Quartz.net 3.x使用总结(二)——Db持久化和集群
    Vuex实现状态管理
    Quartz.net 3.x使用总结(一)——简单使用
    C#获取根目录的方法总结
  • 原文地址:https://www.cnblogs.com/jianquan100/p/12919997.html
Copyright © 2020-2023  润新知