Java 模板下载

模板下载Java代码:

import org.springframework.core.io.ClassPathResource;
import java.io.InputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.core.io.InputStreamResource;


@RequestMapping(value = "/downloadExcelTemplate", method = RequestMethod.POST)
    public ResponseEntity<InputStreamResource> downloadExcelTemplate(HttpServletRequest request, @RequestBody Map<String,Object> params) throws Exception {
        // 模板文件路径
        //   String filetype = request.getParameter("type");
        //String filetype = params.get("type").toString();
        String templateFilePath = "";
        if(jzCode.equals(params.get("projectType"))){
            templateFilePath = "/templates/"+"yn_zgxh_jz"+".xlsx";//经责
        }else if(nkCode.equals(params.get("projectType"))){
            templateFilePath = "templates/"+"yn_zgxh_nk"+".xlsx";//内控
        }else if(zxCode.equals(params.get("projectType"))){
            templateFilePath = "templates/"+"yn_zgxh_zx"+".xlsx";//专项
        }

        // 获取资源文件输入流
        ClassPathResource resource = new ClassPathResource(templateFilePath);
        InputStream inputStream = resource.getInputStream();

        // 设置响应头,包括文件名和MIME类型
        String filename = "";
        if(jzCode.equals(params.get("projectType"))){
            filename = "经责离任(任中审计)整改情况跟踪表.xlsx";//经责
        }else if(nkCode.equals(params.get("projectType"))){
            filename = "内控发现问题整改情况跟踪表.xlsx";//内控
        }else if(zxCode.equals(params.get("projectType"))){
            filename = "专项审计调查整改问题跟踪表.xlsx";//专项
        }

        String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", encodedFilename);

        // 返回包含文件流的ResponseEntity对象
        return ResponseEntity.ok()
                .headers(headers)
                .body(new InputStreamResource(inputStream));
    }

模板放置路径

 

apipost调用接口:

 

posted @ 2025-07-09 16:08  krt-wanyi  阅读(20)  评论(0)    收藏  举报