项目开发过程中,总会有批量导入的操作,但是批量导入需要指定一下导入文件的格式;一般都会使用Excel文件操作批量导入,模板文件规定好上传哪些字段,用户需要按照模板对应字段填写;后台开发者就会提供一个模板供下载进行编写操作。
在开发工程中,可以提供一个模板文件存在,例如:
@ApiOperation("模板下载")
@RequestMapping(value = "/template", method = RequestMethod.GET)
@ApiImplicitParams({
@ApiImplicitParam(name = "ukey", value = "ukey", dataType = "string", paramType = "query", required = true) })
private RestResponse getTemplate(@Ukey @RequestParam(name = "ukey", required = true) String ukey,
HttpServletResponse res, HttpServletRequest req) throws IOException {
ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
RestResponse response = new RestResponse();
if (user == null || user.getUserBelong() == null) {
response.setCode(ResultCode.NO_LOGINED);
response.setMessage("登录信息失效,请重新登录");
return response;
}
String fileName = "slot_template.xlsx";
String path = getClass().getResource("templates/" + fileName).getPath();
OutputStream os = res.getOutputStream();
try {
String agent = (String) req.getHeader("USER-AGENT");
if (agent != null && agent.indexOf("MSIE") == -1 && agent.indexOf("like Gecko") == -1) {// FF
String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8"))))
+ "?=";
res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
} else { // IE
String enableFileName = URLEncoder.encode(fileName, "UTF-8");
res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
}
os.write(FileUtils.readFileToByteArray(new File(path)));
os.flush();
} finally {