FileUploadController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package com.yihu.wlyy.web.common;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.fasterxml.jackson.databind.node.ObjectNode;
  9. import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiParam;
  12. import org.apache.commons.io.FileUtils;
  13. import org.json.JSONObject;
  14. import org.springframework.http.MediaType;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.util.FileCopyUtils;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestMethod;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import org.springframework.web.multipart.MultipartHttpServletRequest;
  22. import com.yihu.wlyy.util.DateUtil;
  23. import com.yihu.wlyy.util.ImageCompress;
  24. import com.yihu.wlyy.util.SystemConf;
  25. import com.yihu.wlyy.web.BaseController;
  26. @Controller
  27. @RequestMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  28. @Api(description = "文件上传")
  29. public class FileUploadController extends BaseController {
  30. @ApiParam
  31. FastDFSUtil fastDFSUtil;
  32. /**
  33. * 患者头像上传
  34. *
  35. * @return
  36. * @throws IOException
  37. * @throws IllegalStateException
  38. */
  39. @RequestMapping(value = "patientPhoto", method = RequestMethod.POST/* , headers = "Accept=image/png" */)
  40. @ResponseBody
  41. public String patientPhoto(HttpServletRequest request, HttpServletResponse response, String photo) {
  42. try {
  43. String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
  44. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  45. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  46. String fileName = null;
  47. String firstPhoto = null;
  48. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  49. // 上传文件
  50. MultipartFile mf = entity.getValue();
  51. fileName = mf.getOriginalFilename();
  52. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  53. // 重命名文件
  54. firstPhoto = photo.substring(0, photo.lastIndexOf(".")) + "_small." + fileExt;
  55. File uploadFile = new File(SystemConf.getInstance().getTempPath() + File.separator + firstPhoto);
  56. // 拷贝文件流到指定文件路径
  57. FileCopyUtils.copy(mf.getBytes(), uploadFile);
  58. }
  59. JSONObject json = new JSONObject();
  60. json.put("status", 200);
  61. json.put("msg", "上传成功");
  62. // 图片标识对象的HTTP链接
  63. json.put("urls", String.join(",", firstPhoto));
  64. System.out.println("图片上传:" + json.toString());
  65. return json.toString();
  66. } catch (Exception e) {
  67. error(e);
  68. return error(-1, "上传失败");
  69. }
  70. }
  71. /**
  72. * 图片上传
  73. *
  74. * @return
  75. * @throws IOException
  76. * @throws IllegalStateException
  77. */
  78. @RequestMapping(value = "image", method = RequestMethod.POST/* , headers = "Accept=image/png" */)
  79. @ResponseBody
  80. public String image(HttpServletRequest request, HttpServletResponse response) {
  81. // 圖片列表
  82. List<File> images = new ArrayList<File>();
  83. List<String> tempPaths = new ArrayList<String>();
  84. // 文件保存的临时路径
  85. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  86. // 拼接年月日路径
  87. String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
  88. try {
  89. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  90. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  91. // 创建文件夹
  92. File file = new File(tempPath + datePath);
  93. if (!file.exists()) {
  94. file.mkdirs();
  95. }
  96. String fileName = null;
  97. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  98. // 上传文件
  99. MultipartFile mf = entity.getValue();
  100. fileName = mf.getOriginalFilename();
  101. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  102. // 重命名文件
  103. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  104. String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
  105. File uploadFile = new File(tempPath + datePath + newFileName);
  106. // 拷贝文件流到指定文件路径
  107. FileCopyUtils.copy(mf.getBytes(), uploadFile);
  108. // 生成缩略图
  109. ImageCompress.compress(uploadFile.getAbsolutePath(), uploadFile.getAbsolutePath() + "_small", 300, 300);
  110. // 添加到上传成功数组中
  111. images.add(uploadFile);
  112. tempPaths.add(datePath + newFileName);
  113. }
  114. String urls = "";
  115. for (String image : tempPaths) {
  116. if (urls.length() == 0) {
  117. urls = image;
  118. } else {
  119. urls += "," + image;
  120. }
  121. }
  122. JSONObject json = new JSONObject();
  123. json.put("status", 200);
  124. json.put("msg", "上传成功");
  125. // 图片标识对象的HTTP链接
  126. json.put("urls", urls);
  127. System.out.println("图片上传:" + json.toString());
  128. return json.toString();
  129. } catch (Exception e) {
  130. error(e);
  131. try {
  132. // 清除垃圾图片
  133. for (File file : images) {
  134. FileUtils.forceDelete(file);
  135. }
  136. } catch (Exception e2) {
  137. error(e2);
  138. }
  139. return error(-1, "上传失败");
  140. }
  141. }
  142. /**
  143. * 聊天附件上传
  144. *
  145. * @return
  146. * @throws IOException
  147. * @throws IllegalStateException
  148. */
  149. @RequestMapping(value = "chat", method = RequestMethod.POST)
  150. @ResponseBody
  151. public String chatFile(HttpServletRequest request, HttpServletResponse response) {
  152. List<String> tempPaths = new ArrayList<String>();
  153. try {
  154. String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
  155. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  156. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  157. String fileName = null;
  158. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  159. // 上传文件
  160. MultipartFile mf = entity.getValue();
  161. fileName = mf.getOriginalFilename();
  162. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  163. ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream(),fileExt,"");
  164. tempPaths.add(fastUrl + objectNode.get("groupName").toString().replaceAll("\"","")
  165. + "/" + objectNode.get("remoteFileName").toString().replaceAll("\"",""));
  166. }
  167. String urls = String.join(",", tempPaths);
  168. JSONObject json = new JSONObject();
  169. json.put("status", 200);
  170. json.put("msg", "上传成功");
  171. // 图片标识对象的HTTP链接
  172. json.put("urls", urls);
  173. System.out.println("附件上传:" + json.toString());
  174. return json.toString();
  175. } catch (Exception e) {
  176. error(e);
  177. return error(-1, "上传失败");
  178. }
  179. }
  180. /**
  181. * 语音上传
  182. *
  183. * @param request
  184. * @param response
  185. * @return
  186. */
  187. @RequestMapping(value = "voice", method = RequestMethod.POST)
  188. @ResponseBody
  189. public String voice(HttpServletRequest request, HttpServletResponse response) {
  190. // 圖片列表
  191. List<File> voices = new ArrayList<File>();
  192. List<String> tempPaths = new ArrayList<String>();
  193. // 文件保存的临时路径
  194. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  195. // 拼接年月日路径
  196. String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
  197. try {
  198. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  199. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  200. // 创建文件夹
  201. File file = new File(tempPath + datePath);
  202. if (!file.exists()) {
  203. file.mkdirs();
  204. }
  205. String fileName = null;
  206. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  207. // 上传文件
  208. MultipartFile mf = entity.getValue();
  209. fileName = mf.getOriginalFilename();
  210. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  211. // 重命名文件
  212. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  213. String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
  214. File uploadFile = new File(tempPath + datePath + newFileName);
  215. // 拷贝文件流到指定文件路径
  216. FileCopyUtils.copy(mf.getBytes(), uploadFile);
  217. // 添加到上传成功数组中
  218. voices.add(uploadFile);
  219. tempPaths.add(datePath + newFileName);
  220. }
  221. String urls = "";
  222. for (String voice : tempPaths) {
  223. if (urls.length() == 0) {
  224. urls = voice;
  225. } else {
  226. urls += "," + voice;
  227. }
  228. }
  229. JSONObject json = new JSONObject();
  230. json.put("status", 200);
  231. json.put("msg", "上传成功");
  232. // 图片标识对象的HTTP链接
  233. json.put("urls", urls);
  234. System.out.println("语音上传:" + json.toString());
  235. return json.toString();
  236. } catch (Exception e) {
  237. error(e);
  238. try {
  239. // 清除垃圾图片
  240. for (File file : voices) {
  241. FileUtils.forceDelete(file);
  242. }
  243. } catch (Exception e2) {
  244. error(e2);
  245. }
  246. return error(-1, "上传失败");
  247. }
  248. }
  249. }