思路:把要下载的文件放到exportExcel目录下,利用url查找是否有改文件,有的话下载,没有的话报错。
![](https://i-blog.csdnimg.cn/blog_migrate/906c4e1ce10debad429d45543f6c1802.png)
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
@Controller
@RequestMapping("templatedownload")
public class TemplateDownload {
@GetMapping("{filename}")
@ApiOperation(value = "获取Excel模板",notes = "获取Excel模板")
public ResultVO downExcel(@PathVariable("filename") String filename, HttpServletResponse response)throws Exception{
InputStream in = ClassUtils.class.getClassLoader().getResourceAsStream("exportexcel/"+filename+".xls");
if (in == null) {
throw new AppcationException(ExceptionEnum.NOTFOUNTVALUE);
}
response.setHeader("content-type", "application/vnd.ms-excel");
response.addHeader("Content-Disposition","attachment;filename=" +filename+".xls");
OutputStream fileOut= response.getOutputStream();
IOUtils.copy(in, fileOut);
in.close();
fileOut.close();
return ResultVOUtil.success();
}
}