BaseApiController.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.goafanti.common.controller;
  2. import com.goafanti.common.bo.Result;
  3. import com.goafanti.common.utils.FileUtils;
  4. import com.goafanti.common.utils.LoggerUtils;
  5. import com.goafanti.core.shiro.token.TokenManager;
  6. import com.goafanti.order.enums.OrderImgEnums;
  7. import net.coobird.thumbnailator.Thumbnails;
  8. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import org.springframework.web.multipart.MultipartRequest;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  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.splicingFileName(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. protected String uploadFile(Result res, String path, HttpServletRequest req,String id, String sign) {
  73. List<MultipartFile> files = getFiles(req);
  74. String fileName = "";
  75. if (!files.isEmpty()) {
  76. try {
  77. MultipartFile mf = files.get(0);
  78. fileName = FileUtils.orderFileName(mf,id,path,sign);
  79. File file=toFile(fileName);
  80. mf.transferTo(file);
  81. // fileName=compressFile(file,fileName);
  82. LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
  83. } catch (Exception e) {
  84. LoggerUtils.error(getClass(), "文件上传失败", e);
  85. res.getError().add(buildError("", "文件上传失败!"));
  86. }
  87. } else {
  88. res.getError().add(buildError("", "文件为空,上传失败!"));
  89. }
  90. return fileName;
  91. }
  92. /**
  93. * 图片压缩
  94. * @param file
  95. * @param fileName
  96. * @return
  97. * @throws IOException
  98. */
  99. private String compressFile(File file, String fileName) throws IOException {
  100. if (fileName.contains("jpg")||fileName.contains("jpeg")||fileName.contains("bmp")
  101. ||fileName.contains("png")){
  102. String thumbnailFilePathName = fileName.substring(0, fileName.lastIndexOf(".")) + "_min"+fileName.substring(fileName.lastIndexOf("."));
  103. double scale = 0.4d ;
  104. Thumbnails.of(file).scale(1f).outputQuality(scale)
  105. .outputFormat("jpg")
  106. .toFile(uploadPath+thumbnailFilePathName);
  107. FileUtils.deleteFile(uploadPath+fileName);
  108. return thumbnailFilePathName;
  109. }
  110. return fileName;
  111. }
  112. protected Result deleteFile (Result res,String fileName){
  113. if (StringUtils.isBlank(fileName)) {
  114. res.getError().add(buildError("文件路径错误"));
  115. return res;
  116. }
  117. try {
  118. if (!com.goafanti.common.utils.excel.FileUtils.checkAllowDownload(fileName)) {
  119. throw new Exception("文件名称非法,不允许删除。 ");
  120. }
  121. String filePath = uploadPath + "/" + fileName;
  122. com.goafanti.common.utils.excel.FileUtils.deleteFile(filePath);
  123. if (fileName.contains(OrderImgEnums.CONTRACT.getCode())){
  124. }
  125. res.setData(1);
  126. } catch (Exception e) {
  127. LoggerUtils.error(PublicController.class, "删除文件失败", e);
  128. }
  129. return res;
  130. }
  131. /**
  132. *
  133. * @param res
  134. * @param isPrivate
  135. * @param req
  136. * @return
  137. */
  138. protected String handleFile(Result res, boolean isPrivate, HttpServletRequest req, String... fileName) {
  139. List<MultipartFile> files = getFiles(req);
  140. String realFilePath = "";
  141. if (!files.isEmpty()) {
  142. try {
  143. MultipartFile mf = files.get(0);
  144. realFilePath = FileUtils.buildFilePath(fileName) + FileUtils.getSuffix(mf.getOriginalFilename());
  145. File f = isPrivate ? toPrivateFile(realFilePath) : toFile(realFilePath);
  146. mf.transferTo(f);
  147. LoggerUtils.debug(getClass(), realFilePath + " 文件上传成功");
  148. } catch (Exception e) {
  149. LoggerUtils.error(getClass(), "文件上传失败", e);
  150. res.getError().add(buildError("", "文件上传失败!"));
  151. }
  152. } else {
  153. res.getError().add(buildError("", "文件上传失败!"));
  154. }
  155. return realFilePath;
  156. }
  157. /**
  158. *
  159. * @param res
  160. * @param fileName
  161. * @param filePath
  162. */
  163. protected void downloadFile(HttpServletResponse res, String fileName, String filePath) {
  164. FileUtils.downloadFile(res, fileName, uploadPath+ filePath);
  165. }
  166. protected void downloadUnPrivateFile(HttpServletResponse res, String fileName, String filePath) {
  167. FileUtils.downloadFile(res, fileName, uploadPrivatePath + filePath);
  168. }
  169. private File buildFile(String filePath) {
  170. File toFile = new File(filePath);
  171. toFile.mkdirs();
  172. return toFile;
  173. }
  174. /**
  175. *
  176. * @param fileName
  177. * @return
  178. */
  179. protected File toFile(String fileName) {
  180. return buildFile(uploadPath + fileName);
  181. }
  182. /**
  183. *
  184. * @param fileName
  185. * @return
  186. */
  187. protected File toPrivateFile(String fileName) {
  188. return buildFile(uploadPrivatePath + fileName);
  189. }
  190. /**
  191. *
  192. * @param fileName
  193. * @return
  194. */
  195. protected File toAdminPrivateFile(String fileName) {
  196. return buildFile(uploadPrivatePath + FileUtils.buildFilePath("/admin/", fileName));
  197. }
  198. protected Integer getPageNo(String pageNo) {
  199. if (StringUtils.isNumeric(pageNo)) {
  200. Integer pn = Integer.parseInt(pageNo);
  201. return pn < 1 ? 1 : pn;
  202. }
  203. return 1;
  204. }
  205. protected Integer getPageSize(String pageSize) {
  206. if (StringUtils.isNumeric(pageSize)) {
  207. Integer ps = Integer.parseInt(pageSize);
  208. return ps > 50 ? 50 : (ps < 10 ? 10 : ps);
  209. }
  210. return 10;
  211. }
  212. protected String handleFiles(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign,
  213. String uid) {
  214. List<MultipartFile> files = getFiles(req);
  215. MultipartFile mf = files.get(0);
  216. String fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path,
  217. StringUtils.isBlank(uid) ? TokenManager.getUserId() : uid);
  218. if (!files.isEmpty()) {
  219. try {
  220. if (isPrivate) {
  221. mf.transferTo(toPrivateFile(fileName));
  222. } else {
  223. mf.transferTo(toFile(fileName));
  224. }
  225. LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
  226. } catch (IllegalStateException | IOException e) {
  227. LoggerUtils.error(getClass(), "文件上传失败", e);
  228. res.getError().add(buildError("", "文件上传失败!"));
  229. return "";
  230. }
  231. } else {
  232. res.getError().add(buildError("", "文件上传失败!"));
  233. return "";
  234. }
  235. return fileName;
  236. }
  237. protected Integer handlePageNo(String pageNo) {
  238. try {
  239. Integer n = Integer.valueOf(pageNo);
  240. return n < 1 ? 1 : n;
  241. } catch (Exception e) {
  242. return 1;
  243. }
  244. }
  245. protected Integer handlePageSize(String pageSize) {
  246. try {
  247. Integer s = Integer.valueOf(pageSize);
  248. return s < 5 ? 5 : s > 100 ? 100 : s;
  249. } catch (Exception e) {
  250. return 10;
  251. }
  252. }
  253. protected Result res() {
  254. return new Result();
  255. }
  256. }