项目结构如下:

 下载代码如下:

    @GetMapping(value = "downTemplateFile")
    @ApiOperation(value = "下载名单模板文件", notes = "下载名单模板文件")
    public void downTemplateFile(){
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"file";
        System.out.println("template====>"+path);
        String fileName = "应急避险信息登记册.docx";
        FileUtils.downloadFile(path + File.separator + fileName,fileName);
    }

 本来环境启动下载能正常获取,但打包成docker镜像文件启动后,提示找不到文件,于是改成下面这种方式下载后正常下载

    @GetMapping(value = "downTemplateFile")
    @ApiOperation(value = "下载名单模板文件", notes = "下载名单模板文件")
    public void downTemplateFile() throws IOException {
        String path = "/file/";
        String fileName = "应急避险信息登记册.docx";
        File f = new File(path+fileName);//临时文件存在的位置
        if (!f.exists()) {
            InputStream in = this.getClass().getResourceAsStream(path+fileName);//图片在项目中的位置
            FileUtils.copyInputStreamToFile(in, f);
        }
        FileUtils.downloadFile(path + fileName,fileName);
    }

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐