FileController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.uas.report.controller;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.util.StringUtils;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import com.uas.report.core.exception.ReportException;
  14. import com.uas.report.service.FileService;
  15. import com.uas.report.support.SysConf;
  16. /**
  17. * 文件操作请求
  18. *
  19. * @author sunyj
  20. * @since 2016年11月2日 下午2:05:04
  21. */
  22. @Controller
  23. @RequestMapping("/file")
  24. public class FileController {
  25. @Autowired
  26. private FileService fileService;
  27. @Autowired
  28. private SysConf sysConf;
  29. @RequestMapping("/autoDeploy")
  30. @ResponseBody
  31. public String autoDeploy(String userNames) {
  32. return fileService.autoDeploy(userNames);
  33. }
  34. @RequestMapping("/upload")
  35. @ResponseBody
  36. public String upload(String userName, String fileType, MultipartFile file) {
  37. return fileService.upload(userName, fileType, file);
  38. }
  39. /**
  40. * 下载文件
  41. *
  42. * @param filePath
  43. * 文件路径
  44. * @param isAbsolutePath
  45. * 文件路径是否为绝对路径
  46. * @param response
  47. */
  48. @RequestMapping("/download")
  49. public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response) {
  50. fileService.download(filePath, isAbsolutePath, response);
  51. }
  52. /**
  53. * 下载账套下的所有资源
  54. *
  55. * @param userName
  56. * 账套名
  57. * @param response
  58. */
  59. @RequestMapping("/download/zip")
  60. public void downloadZip(String userName, HttpServletResponse response) {
  61. if (StringUtils.isEmpty(userName)) {
  62. throw new ReportException("未传入当前账套用户名!");
  63. }
  64. String zipFilePath = fileService.getZip(userName);
  65. if (zipFilePath.isEmpty()) {
  66. throw new ReportException("压缩失败");
  67. }
  68. fileService.download(zipFilePath, true, response);
  69. }
  70. /**
  71. * 下载模板
  72. *
  73. * @param userName
  74. * 账套名称
  75. * @param reportName
  76. * 模板名称
  77. * @param response
  78. */
  79. @RequestMapping("/download/jrxml")
  80. public void downloadJrxml(String userName, String reportName, HttpServletResponse response) {
  81. fileService.download(fileService.getJrxmlFilePath(userName, reportName), true, response);
  82. }
  83. /**
  84. * 下载标准模板
  85. *
  86. * @param onlyData
  87. * 是否只获取数据
  88. */
  89. @RequestMapping("/standardJrxmls")
  90. @ResponseBody
  91. public Map<String, Object> standardJrxmls(String onlyData, HttpServletResponse response) {
  92. // 只返回字节数据,否则下载标准模板zip
  93. if (!StringUtils.isEmpty(onlyData) && (onlyData.equals("1") || onlyData.equals("true"))) {
  94. Map<String, Object> result = new HashMap<>();
  95. // 不直接返回byte[],是因为客户端请求数据时,该请求可能会抛出异常,
  96. // 之后客户端获取byte[]数据,进行处理时难以判断数据是文件数据还是异常信息
  97. result.put("data", fileService.getStandardJrxmls());
  98. return result;
  99. }
  100. try {
  101. response.sendRedirect("download/zip?userName=" + sysConf.getStandardMaster());
  102. return null;
  103. } catch (IOException e) {
  104. throw new ReportException(e).setDetailedMessage(e);
  105. }
  106. }
  107. @RequestMapping("/delete")
  108. @ResponseBody
  109. public String delete(String userName, String filePath) {
  110. // 账套为空,filePath为绝对路径,否则为账套下的相对路径
  111. if (StringUtils.isEmpty(userName)) {
  112. return "Deleted... " + fileService.delete(filePath);
  113. } else {
  114. return "Deleted... " + fileService.delete(userName, filePath);
  115. }
  116. }
  117. @RequestMapping("/listFiles")
  118. @ResponseBody
  119. public List<Map<String, Object>> listFiles(String fileRelativePath) {
  120. return fileService.listFiles(fileRelativePath);
  121. }
  122. }