码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-05-20 09:21:18      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:out   commons   entity   adc   file   ram   var   for   gets   

下载按钮:

<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目录下。

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

标签:out   commons   entity   adc   file   ram   var   for   gets   

原文地址:https://blog.51cto.com/14816966/2496779

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!