欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring 文件下载

程序员文章站 2022-04-09 09:24:12
...
    /**
     * 文件下载
     */
    public static ResponseEntity<byte[]> downloadApp(String driName,String name) throws IOException {
        //获取文件路径
        String disk=AppDownload.class.getResource("").getPath().substring(1,3)+"\\update\\"+driName+"\\"+name;
        //创建文件
        File file=new File(disk);
        byte[] body;
        InputStream is = new FileInputStream(file);
        body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=" + file.getName());
        HttpStatus statusCode = HttpStatus.OK;
        return new ResponseEntity<byte[]>(body, headers, statusCode);
    }

 

相关标签: 下载