项目需要下载导入模板,springboot使用如下下载方式可解决问题:
/**
* 导入模板下载
* @param req
* @param resp
* @param context
* @return
*/
@RequestMapping(value = "wSku/downLoadFile", method = {RequestMethod.POST, RequestMethod.GET})
public @ResponseBody void downLoadFile(HttpServletRequest req,HttpServletResponse resp) {
String fileDownName ="价格表-模板.xlsx";
logger.debug("下载模板文件名称:"+fileDownName);
try {
InputStream fis = WSkuController.class.getResourceAsStream("/exceltemplet/priceListTemplate.xlsx");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setContentType("bin");
String fileNames = fileDownName;
String agent = request.getHeader("USER-AGENT");
String codedfilename = "";
if (null != agent && -1 != agent.indexOf("MSIE") || null != agent && -1 != agent.indexOf("Trident")) {// ie
String name = java.net.URLEncoder.encode(fileNames, "UTF8");
codedfilename = name;
} else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐,chrome等
codedfilename = new String(fileNames.getBytes("UTF-8"), "iso-8859-1");
} response.addHeader("Content-Disposition", "attachment; filename=\"" + codedfilename + "\"");
response.getOutputStream().write(buffer);
} catch (IOException e) {
e.printStackTrace();
logger.debug("下载价格表-模板文件报错"+e.getMessage(), e);
}
}
其中文件存放在如下路径下:/imfbp-istorewares-admin/src/main/resources/exceltemplet/priceListTemplate.xlsx