Java 下载模板文件-简便方式

307 阅读1分钟

Java-本地下载模板文件简便方式

@ApiOperation(value = "下载模板文件")  
@GetMapping(value = "/downloadTemp")  
public ResponseEntity<Resource> downloadExcel() {  
    // 加载 Excel 文件作为资源  
    // 文件存放于 resources目录下
    Resource fileResource = new ClassPathResource("temp/student.xlsx");  

    // 创建响应实体  
    HttpHeaders headers = new HttpHeaders();  
    headers.add("Content-Disposition", "attachment; filename=\"student.xlsx\"");  

    // 返回文件资源和响应头  
    return ResponseEntity.ok()  
        .headers(headers)  
        .contentType(MediaType.APPLICATION_OCTET_STREAM)  
        .body(fileResource);  
}

案例实现

image.png