Controller层:
@RequestMapping("/admin/model") public ResponseEntity<byte[]> download2() throws IOException { File file = excelModelService.buildXlsById(); return ResponseUtils.buildResponseEntity(file); }
Service层:
import org.springframework.stereotype.Service; import org.springframework.util.ResourceUtils; import java.io.File; import java.io.FileNotFoundException; /** * @author 赵鑫 * @Time: 2018/7/22 * @Email:[email protected] */ @Service public class ExcelModelServiceImpl { public File buildXlsById(){ //do something to find this file File file=null; try { file = ResourceUtils.getFile("classpath:static/model.xlsx"); } catch (FileNotFoundException e) { e.printStackTrace(); } return file; } }
页面代码
<a href="${request.contextPath}/admin/model">下载模板</a>
文件所放位置