BaseApiController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.goafanti.common.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import org.springframework.web.multipart.MultipartRequest;
  16. import com.goafanti.common.bo.Result;
  17. import com.goafanti.common.utils.FileUtils;
  18. import com.goafanti.common.utils.LoggerUtils;
  19. import com.goafanti.core.shiro.token.TokenManager;
  20. @RestController
  21. @RequestMapping(value = "/api")
  22. public class BaseApiController extends BaseController {
  23. @Value(value = "${upload.path}")
  24. private String uploadPath = null;
  25. @Value(value = "${upload.private.path}")
  26. private String uploadPrivatePath = null;
  27. /**
  28. *
  29. * @param request
  30. * @return
  31. */
  32. protected List<MultipartFile> getFiles(HttpServletRequest request) {
  33. if (ServletFileUpload.isMultipartContent(request)) {
  34. Iterator<String> itr = ((MultipartRequest) request).getFileNames();
  35. List<MultipartFile> files = new ArrayList<MultipartFile>();
  36. if (itr.hasNext()) {
  37. files.add(((MultipartRequest) request).getFile(itr.next()));
  38. }
  39. return files;
  40. }
  41. return new ArrayList<MultipartFile>();
  42. }
  43. /**
  44. *
  45. * @param res
  46. * @param path
  47. * @param isPrivate
  48. * @param req
  49. * @param sign
  50. * @return
  51. */
  52. protected String handleFile(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign) {
  53. List<MultipartFile> files = getFiles(req);
  54. String fileName = "";
  55. if (!files.isEmpty()) {
  56. try {
  57. MultipartFile mf = files.get(0);
  58. fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path, TokenManager.getUserId());
  59. mf.transferTo(isPrivate ? toPrivateFile(fileName) : toFile(fileName));
  60. LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
  61. } catch (Exception e) {
  62. LoggerUtils.error(getClass(), "文件上传失败", e);
  63. res.getError().add(buildError("", "文件上传失败!"));
  64. }
  65. } else {
  66. res.getError().add(buildError("", "文件上传失败!"));
  67. }
  68. return fileName;
  69. }
  70. /**
  71. *
  72. * @param res
  73. * @param path
  74. * @param isPrivate
  75. * @param req
  76. * @param sign
  77. * @return
  78. */
  79. protected String handleFile(Result res, boolean isPrivate, HttpServletRequest req, String... fileName) {
  80. List<MultipartFile> files = getFiles(req);
  81. String realFilePath = "";
  82. if (!files.isEmpty()) {
  83. try {
  84. MultipartFile mf = files.get(0);
  85. realFilePath = FileUtils.buildFilePath(fileName) + FileUtils.getSuffix(mf.getOriginalFilename());
  86. File f = isPrivate ? toPrivateFile(realFilePath) : toFile(realFilePath);
  87. mf.transferTo(f);
  88. LoggerUtils.debug(getClass(), realFilePath + " 文件上传成功");
  89. } catch (Exception e) {
  90. LoggerUtils.error(getClass(), "文件上传失败", e);
  91. res.getError().add(buildError("", "文件上传失败!"));
  92. }
  93. } else {
  94. res.getError().add(buildError("", "文件上传失败!"));
  95. }
  96. return realFilePath;
  97. }
  98. /**
  99. *
  100. * @param res
  101. * @param fileName
  102. * @param filePath
  103. */
  104. protected void downloadFile(HttpServletResponse res, String fileName, String filePath) {
  105. FileUtils.downloadFile(res, fileName, uploadPrivatePath + filePath);
  106. }
  107. protected void downloadUnPrivateFile(HttpServletResponse res, String fileName, String filePath) {
  108. FileUtils.downloadFile(res, fileName, uploadPath + filePath);
  109. }
  110. private File buildFile(String filePath) {
  111. File toFile = new File(filePath);
  112. toFile.mkdirs();
  113. return toFile;
  114. }
  115. /**
  116. *
  117. * @param fileName
  118. * @return
  119. */
  120. protected File toFile(String fileName) {
  121. return buildFile(uploadPath + fileName);
  122. }
  123. /**
  124. *
  125. * @param fileName
  126. * @return
  127. */
  128. protected File toPrivateFile(String fileName) {
  129. return buildFile(uploadPrivatePath + fileName);
  130. }
  131. /**
  132. *
  133. * @param fileName
  134. * @return
  135. */
  136. protected File toAdminPrivateFile(String fileName) {
  137. return buildFile(uploadPrivatePath + FileUtils.buildFilePath("/admin/", fileName));
  138. }
  139. protected Integer getPageNo(String pageNo) {
  140. if (StringUtils.isNumeric(pageNo)) {
  141. Integer pn = Integer.parseInt(pageNo);
  142. return pn < 1 ? 1 : pn;
  143. }
  144. return 1;
  145. }
  146. protected Integer getPageSize(String pageSize) {
  147. if (StringUtils.isNumeric(pageSize)) {
  148. Integer ps = Integer.parseInt(pageSize);
  149. return ps > 50 ? 50 : (ps < 10 ? 10 : ps);
  150. }
  151. return 10;
  152. }
  153. protected String handleFiles(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign,
  154. String uid) {
  155. List<MultipartFile> files = getFiles(req);
  156. MultipartFile mf = files.get(0);
  157. String fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path,
  158. StringUtils.isBlank(uid) ? TokenManager.getUserId() : uid);
  159. if (!files.isEmpty()) {
  160. try {
  161. if (isPrivate) {
  162. mf.transferTo(toPrivateFile(fileName));
  163. } else {
  164. mf.transferTo(toFile(fileName));
  165. }
  166. LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
  167. } catch (IllegalStateException | IOException e) {
  168. LoggerUtils.error(getClass(), "文件上传失败", e);
  169. res.getError().add(buildError("", "文件上传失败!"));
  170. return "";
  171. }
  172. } else {
  173. res.getError().add(buildError("", "文件上传失败!"));
  174. return "";
  175. }
  176. return fileName;
  177. }
  178. protected Integer handlePageNo(String pageNo) {
  179. try {
  180. Integer n = Integer.valueOf(pageNo);
  181. return n < 1 ? 1 : n;
  182. } catch (Exception e) {
  183. return 1;
  184. }
  185. }
  186. protected Integer handlePageSize(String pageSize) {
  187. try {
  188. Integer s = Integer.valueOf(pageSize);
  189. return s < 5 ? 5 : s > 100 ? 100 : s;
  190. } catch (Exception e) {
  191. return 10;
  192. }
  193. }
  194. }