FileUploadController.java 16 KB

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