00006-java excelテンプレート(ファイル)をダウンロードし、フロントエンドlayuiボタン

2021 ワード

ダウンロードボタン:

対応方法:
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 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(FileUtils.readFileToByteArray(file),
            headers, HttpStatus.OK);
   }

}

Customer.xlsxはwebapp/templateディレクトリの下に配置されます.