@RequestMapping(value = "/exportTemplet",method = RequestMethod.GET,produces="application/json")
public R<Object> downModel(HttpServletResponse response, HttpServletRequest request) {
R<Object> result=R.ok();
String fileName = request.getParameter("fileName");
String filePath ="";
ServletOutputStream out;
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
try{
// String filePath = getClass().getResource("/static/template/" + fileName).getPath();//文件在项目中的存放路径
// 这里可以设置本地文件夹路径 if(fileName.contains("专家")){ filePath = RuoYiConfig.getProfile()+"/model/导入专家模板.xlsx";}
// else {
// filePath = RuoYiConfig.getProfile()+"/model/导入导师模板.xlsx";}
filePath = this.getClass().getResource("/static/template/" + fileName).getPath();//文件在项目中的存放路径
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
// 非IE浏览器的处理:
fileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1");
}
filePath = URLDecoder.decode(filePath, "UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
FileInputStream inputStream = new FileInputStream(filePath);
out = response.getOutputStream();
int b = 0;
byte[] buffer = new byte[1024];
while ((b = inputStream.read(buffer)) != -1) {
// 4.写到输出流(out)中
out.write(buffer, 0, b);
}
inputStream.close();
if (out != null) {
out.flush();
out.close();
}
}catch (Exception e){
R.fail(ExceptionUtils.getMessage(e));
R.fail(ExceptionUtils.getStackTrace(e));
return R.fail(e.getMessage());
}
return null;
}
//前端(可以选择点击事件和<a>标签)
点击事件
handleExportTutor(){
var url=process.env.VUE_APP_BASE_API + "web/tutor/exportTemplet?fileName=导入专家模板.xlsx";
window.open(url);//跳转后台的路径
},
超链接
<p style="font-size: 14px;color: red"><a :href="baseApi + 'web/tutor/exportTemplet?fileName=导入专家模板.xlsx'" download>下载专家导入模板</a></p>
download 这个可以本页面下载
需要在项目resources对应文件夹上传需要下载的模板》