/** * 版权所有:厦门市巨龙信息科技有限公司 * Copyright 2011 Xiamen Dragon Info. Tech. Co. Ltd. * All right reserved. * ==================================================== * 文件名称: DownloadController.java * 修订记录: * No 日期 作者(操作:具体内容) * 1. 2018-11-16 黄华源(创建:创建文件并实现模版下载) * ==================================================== * 类描述: */ package com.dragoninfo.dcuc.authweb.restcontroller.download; import com.dragonsoft.duceap.commons.util.string.StringUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.system.ApplicationHome; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; /** *
DownloadController* *
--描述说明--*
* 提供警员模版、辅警模版、施工人员模版下载功能,采用io流实现。 **
* --样例-- * 前端发起请求: * http://localhost:port/context/downloadTemplate/downPolice/{type}.action ** JDK版本:JDK1.6 * * @author 黄华源 */ @Api(tags = {"附件下载接口"}) @RestController @RequestMapping(value = "/downloadTemplate/") public class DownloadController { public final String POLICE_TEMPLATE_RLPATH = "/template/PoliceTemplate.xls"; public final String AUXILIARY_TEMPLATE_RLPATH = "/template/auxiliarypoliceTemplate.xls"; public final String EXTERNALPERSONNEL_TEMPLATE_RLPATH = "/template/externalpersonnelTemplate.xls"; public final String MANAGER_TEMPLATE_RLPATH = "/template/managerTemplate.xls"; public final String GOVERNMENTPERSON_TEMPLATE_RLPATH = "/template/governmentpersonnelTemplate.xls"; public final String ORG_TEMPLATE_RLPATH = "/template/OrgTemplate.xls"; public final String USER_TEMPLATE_RLPATH = "/template/UserTemplate.xls"; /** * 客体_服务资源模板 */ public final String SERVICE_RESOURCE_TEMPLATE_RLPATH = "/template/serviceResourceTemplate.xls"; /** * 客体_功能资源模板 */ public final String APP_FUN_TEMPLATE_RLPATH = "/template/appFunTemplate.xls"; /** * 主客体_应用资源模板 */ public final String APPLY_TEMPLATE_RLPATH = "/template/applyTemplate.xls"; /** * 主客体_数据分级模板 */ public final String DATA_LEVEL_TEMPLATE_RLPATH = "/template/dataLevelTemplate.xls"; /** * 主客体_数据安全级别模板 */ public final String DATA_SEC_TEMPLATE_RLPATH = "/template/dataSecTemplate.xls"; private Logger logger = LoggerFactory.getLogger(DownloadController.class); @ApiOperation(value = "获取xls附件") @ApiImplicitParam(name = "type", value = "附件类型:police 警员,auxiliaryPolice 辅警, " + "externalPersonnel 外部人员, manager 管理员, govUser 政府人员, serviceResource 服务资源,appFun 功能资源, apply 应用资源," + "org 机构, user 人员 dataLevel 数据分级 dataSec 数据安全级别", required = true) @GetMapping(value = "/downTemplate", produces = "application/octet-stream") public void downTemplate(@RequestParam("type") String type, HttpServletResponse response) { ApplicationHome home = new ApplicationHome(getClass()); File sysfile = home.getSource(); String jarPath = sysfile.getPath();//classes路径 if (StringUtils.equals(type, "police")) { response = down(response, POLICE_TEMPLATE_RLPATH); } else if (StringUtils.equals(type, "auxiliaryPolice")) { response = down(response, AUXILIARY_TEMPLATE_RLPATH); } else if (StringUtils.equals(type, "externalPersonnel")) { response = down(response, EXTERNALPERSONNEL_TEMPLATE_RLPATH); } else if (StringUtils.equals(type, "manager")) { response = down(response,MANAGER_TEMPLATE_RLPATH); } else if (StringUtils.equals(type, "govUser")) { response = down(response, GOVERNMENTPERSON_TEMPLATE_RLPATH); } else if (StringUtils.equals(type, "org")) { response = down(response, ORG_TEMPLATE_RLPATH); } else if (StringUtils.equals(type, "user")) { response = down(response, USER_TEMPLATE_RLPATH); }else if (StringUtils.equals(type, "serviceResource")){ //授权主客体管理_服务资源模板 response = down(response, SERVICE_RESOURCE_TEMPLATE_RLPATH); }else if (StringUtils.equals(type, "appFun")){ //授权主客体管理_功能资源模板 response = down(response, APP_FUN_TEMPLATE_RLPATH); }else if (StringUtils.equals(type, "apply")){ //授权主客体管理_应用资源模板 response = down(response, APPLY_TEMPLATE_RLPATH); }else if (StringUtils.equals(type, "dataLevel")){ //授权主客体管理_应用资源模板 response = down(response, DATA_LEVEL_TEMPLATE_RLPATH); }else if (StringUtils.equals(type, "dataSec")){ //授权主客体管理_应用资源模板 response = down(response, DATA_SEC_TEMPLATE_RLPATH); } } private HttpServletResponse down(HttpServletResponse response, String path) { InputStream fis = null; OutputStream out = null; try { // 以流的形式下载文件。 Resource resource = new ClassPathResource(path); fis = resource.getInputStream(); // 清空response response.reset(); // 设置response的Header String fileName = ""; if ("PoliceTemplate.xls".equals( resource.getFilename())) { fileName = "警员模版.xls"; } else if ("auxiliarypoliceTemplate.xls".equals( resource.getFilename())) { fileName = "辅警模版.xls"; } else if ("externalpersonnelTemplate.xls".equals( resource.getFilename())) { fileName = "施工人员模版.xls"; } else if ("managerTemplate.xls".equals( resource.getFilename())) { fileName = "管理员模板.xls"; } else if ("governmentpersonnelTemplate.xls".equals( resource.getFilename())) { fileName = "政府人员模板.xls"; } else if ("OrgTemplate.xls".equals( resource.getFilename())) { fileName = "机构信息模板.xls"; } else if ("UserTemplate.xls".equals( resource.getFilename())) { fileName = "人员信息模板.xls"; }else if ("serviceResourceTemplate.xls".equals( resource.getFilename())) { fileName = "客体_服务资源导入.xls"; }else if ("appFunTemplate.xls".equals( resource.getFilename())) { fileName = "客体_功能资源导入.xls"; }else if ("applyTemplate.xls".equals( resource.getFilename())) { fileName = "主客体_应用资源导入.xls"; }else if ("dataLevelTemplate.xls".equals( resource.getFilename())) { fileName = "主客体_数据分级导入.xls"; }else if ("dataSecTemplate.xls".equals( resource.getFilename())) { fileName = "主客体_数据安全级别导入.xls"; } response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); response.addHeader("Content-Length", "" + resource.contentLength()); out = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); IOUtils.copy(fis,out); } catch (IOException ex) { ex.printStackTrace(); } finally { IOUtils.closeQuietly(fis, out); } return response; } }