BaseApiController.java 8.2 KB

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