DownloadController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * 版权所有:厦门市巨龙信息科技有限公司
  3. * Copyright 2011 Xiamen Dragon Info. Tech. Co. Ltd.
  4. * All right reserved.
  5. * ====================================================
  6. * 文件名称: DownloadController.java
  7. * 修订记录:
  8. * No 日期 作者(操作:具体内容)
  9. * 1. 2018-11-16 黄华源(创建:创建文件并实现模版下载)
  10. * ====================================================
  11. * 类描述:
  12. */
  13. package com.dragoninfo.dcuc.authweb.restcontroller.download;
  14. import com.dragonsoft.duceap.commons.util.string.StringUtils;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiImplicitParam;
  17. import io.swagger.annotations.ApiOperation;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.boot.system.ApplicationHome;
  21. import org.springframework.core.io.ClassPathResource;
  22. import org.springframework.core.io.Resource;
  23. import org.springframework.web.bind.annotation.GetMapping;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.*;
  29. import java.net.URLEncoder;
  30. /**
  31. * <pre><b><font color="blue">DownloadController</font></b></pre>
  32. * <p/>
  33. * <pre><b>&nbsp;--描述说明--</b></pre>
  34. * <pre>
  35. * 提供警员模版、辅警模版、施工人员模版下载功能,采用io流实现。
  36. * </pre>
  37. * <pre>
  38. * <b>--样例--</b>
  39. * 前端发起请求:
  40. * http://localhost:port/context/downloadTemplate/downPolice/{type}.action
  41. * </pre>
  42. * JDK版本:JDK1.6
  43. *
  44. * @author <b>黄华源</b>
  45. */
  46. @Api(tags = {"附件下载接口"})
  47. @RestController
  48. @RequestMapping(value = "/downloadTemplate/")
  49. public class DownloadController {
  50. public final String POLICE_TEMPLATE_RLPATH = "/template/PoliceTemplate.xls";
  51. public final String AUXILIARY_TEMPLATE_RLPATH = "/template/auxiliarypoliceTemplate.xls";
  52. public final String EXTERNALPERSONNEL_TEMPLATE_RLPATH = "/template/externalpersonnelTemplate.xls";
  53. public final String MANAGER_TEMPLATE_RLPATH = "/template/managerTemplate.xls";
  54. public final String GOVERNMENTPERSON_TEMPLATE_RLPATH = "/template/governmentpersonnelTemplate.xls";
  55. public final String ORG_TEMPLATE_RLPATH = "/template/OrgTemplate.xls";
  56. public final String USER_TEMPLATE_RLPATH = "/template/UserTemplate.xls";
  57. /**
  58. * 客体_服务资源模板
  59. */
  60. public final String SERVICE_RESOURCE_TEMPLATE_RLPATH = "/template/serviceResourceTemplate.xls";
  61. /**
  62. * 客体_功能资源模板
  63. */
  64. public final String APP_FUN_TEMPLATE_RLPATH = "/template/appFunTemplate.xls";
  65. /**
  66. * 主客体_应用资源模板
  67. */
  68. public final String APPLY_TEMPLATE_RLPATH = "/template/applyTemplate.xls";
  69. /**
  70. * 主客体_数据分级模板
  71. */
  72. public final String DATA_LEVEL_TEMPLATE_RLPATH = "/template/dataLevelTemplate.xls";
  73. /**
  74. * 主客体_数据安全级别模板
  75. */
  76. public final String DATA_SEC_TEMPLATE_RLPATH = "/template/dataSecTemplate.xls";
  77. private Logger logger = LoggerFactory.getLogger(DownloadController.class);
  78. @ApiOperation(value = "获取xls附件")
  79. @ApiImplicitParam(name = "type", value = "附件类型:police 警员,auxiliaryPolice 辅警, " +
  80. "externalPersonnel 外部人员, manager 管理员, govUser 政府人员, serviceResource 服务资源,appFun 功能资源, apply 应用资源," +
  81. "org 机构, user 人员 dataLevel 数据分级 dataSec 数据安全级别",
  82. required = true)
  83. @GetMapping(value = "/downTemplate")
  84. public HttpServletResponse downTemplate(@RequestParam("type") String type, HttpServletResponse response) {
  85. ApplicationHome home = new ApplicationHome(getClass());
  86. File sysfile = home.getSource();
  87. String jarPath = sysfile.getPath();//classes路径
  88. if (StringUtils.equals(type, "police")) {
  89. response = down(response, POLICE_TEMPLATE_RLPATH);
  90. } else if (StringUtils.equals(type, "auxiliaryPolice")) {
  91. response = down(response, AUXILIARY_TEMPLATE_RLPATH);
  92. } else if (StringUtils.equals(type, "externalPersonnel")) {
  93. response = down(response, EXTERNALPERSONNEL_TEMPLATE_RLPATH);
  94. } else if (StringUtils.equals(type, "manager")) {
  95. response = down(response,MANAGER_TEMPLATE_RLPATH);
  96. } else if (StringUtils.equals(type, "govUser")) {
  97. response = down(response, GOVERNMENTPERSON_TEMPLATE_RLPATH);
  98. } else if (StringUtils.equals(type, "org")) {
  99. response = down(response, ORG_TEMPLATE_RLPATH);
  100. } else if (StringUtils.equals(type, "user")) {
  101. response = down(response, USER_TEMPLATE_RLPATH);
  102. }else if (StringUtils.equals(type, "serviceResource")){
  103. //授权主客体管理_服务资源模板
  104. response = down(response, SERVICE_RESOURCE_TEMPLATE_RLPATH);
  105. }else if (StringUtils.equals(type, "appFun")){
  106. //授权主客体管理_功能资源模板
  107. response = down(response, APP_FUN_TEMPLATE_RLPATH);
  108. }else if (StringUtils.equals(type, "apply")){
  109. //授权主客体管理_应用资源模板
  110. response = down(response, APPLY_TEMPLATE_RLPATH);
  111. }else if (StringUtils.equals(type, "dataLevel")){
  112. //授权主客体管理_应用资源模板
  113. response = down(response, DATA_LEVEL_TEMPLATE_RLPATH);
  114. }else if (StringUtils.equals(type, "dataSec")){
  115. //授权主客体管理_应用资源模板
  116. response = down(response, DATA_SEC_TEMPLATE_RLPATH);
  117. }
  118. return response;
  119. }
  120. private HttpServletResponse down(HttpServletResponse response, String path) {
  121. try {
  122. // 以流的形式下载文件。
  123. Resource resource = new ClassPathResource(path);
  124. resource.getInputStream();
  125. InputStream fis = resource.getInputStream();
  126. byte[] buffer = new byte[fis.available()];
  127. fis.read(buffer);
  128. fis.close();
  129. // 清空response
  130. response.reset();
  131. // 设置response的Header
  132. String fileName = "";
  133. if ("PoliceTemplate.xls".equals( resource.getFilename())) {
  134. fileName = "警员模版.xls";
  135. } else if ("auxiliarypoliceTemplate.xls".equals( resource.getFilename())) {
  136. fileName = "辅警模版.xls";
  137. } else if ("externalpersonnelTemplate.xls".equals( resource.getFilename())) {
  138. fileName = "施工人员模版.xls";
  139. } else if ("managerTemplate.xls".equals( resource.getFilename())) {
  140. fileName = "管理员模板.xls";
  141. } else if ("governmentpersonnelTemplate.xls".equals( resource.getFilename())) {
  142. fileName = "政府人员模板.xls";
  143. } else if ("OrgTemplate.xls".equals( resource.getFilename())) {
  144. fileName = "机构信息模板.xls";
  145. } else if ("UserTemplate.xls".equals( resource.getFilename())) {
  146. fileName = "人员信息模板.xls";
  147. }else if ("serviceResourceTemplate.xls".equals( resource.getFilename())) {
  148. fileName = "客体_服务资源导入.xls";
  149. }else if ("appFunTemplate.xls".equals( resource.getFilename())) {
  150. fileName = "客体_功能资源导入.xls";
  151. }else if ("applyTemplate.xls".equals( resource.getFilename())) {
  152. fileName = "主客体_应用资源导入.xls";
  153. }else if ("dataLevelTemplate.xls".equals( resource.getFilename())) {
  154. fileName = "主客体_数据分级导入.xls";
  155. }else if ("dataSecTemplate.xls".equals( resource.getFilename())) {
  156. fileName = "主客体_数据安全级别导入.xls";
  157. }
  158. response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  159. response.addHeader("Content-Length", "" + resource.contentLength());
  160. OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
  161. response.setContentType("application/octet-stream");
  162. outputStream.write(buffer);
  163. outputStream.flush();
  164. outputStream.close();
  165. } catch (IOException ex) {
  166. ex.printStackTrace();
  167. }
  168. return response;
  169. }
  170. }