FileUploadController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. long times = CommonUtil.getVideoTimeAndImg(tempPath,pngPath);
  190. if(times<1000){
  191. JSONObject json = new JSONObject();
  192. json.put("status", -1);
  193. json.put("success",true);
  194. json.put("msg", "视频录制时间太短!");
  195. json.put("times", times);
  196. return json.toString();
  197. }
  198. ObjectNode imgNode = fastDFSUtil.upload(new FileInputStream(pngPath),"png","");
  199. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  200. ObjectNode videoNode = fastDFSUtil.upload(new FileInputStream(tempPath),fileExt,"");
  201. tempPaths.add(fastUrl + imgNode.get("groupName").toString().replaceAll("\"","")
  202. + "/" + imgNode.get("remoteFileName").toString().replaceAll("\"",""));
  203. tempPaths.add(fastUrl + videoNode.get("groupName").toString().replaceAll("\"","")
  204. + "/" + videoNode.get("remoteFileName").toString().replaceAll("\"",""));
  205. tempPaths.add(times+"");
  206. if(tempFile!=null){
  207. tempFile.delete();
  208. }
  209. if(pngFile!=null){
  210. pngFile.delete();
  211. }
  212. } else{
  213. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  214. ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream() ,fileExt,"");
  215. tempPaths.add(fastUrl + objectNode.get("groupName").toString().replaceAll("\"","")
  216. + "/" + objectNode.get("remoteFileName").toString().replaceAll("\"",""));
  217. }
  218. }
  219. String urls = String.join(",", tempPaths);
  220. JSONObject json = new JSONObject();
  221. json.put("status", 200);
  222. json.put("success",true);
  223. json.put("msg", "上传成功");
  224. // 图片标识对象的HTTP链接
  225. json.put("urls", urls);
  226. System.out.println("附件上传:" + json.toString());
  227. return json.toString();
  228. } catch (Exception e) {
  229. error(e);
  230. return error(-1, "上传失败");
  231. }
  232. }
  233. /**
  234. * 语音上传
  235. *
  236. * @param request
  237. * @param response
  238. * @return
  239. */
  240. @RequestMapping(value = "voice", method = RequestMethod.POST)
  241. @ResponseBody
  242. public String voice(HttpServletRequest request, HttpServletResponse response) {
  243. // 圖片列表
  244. List<File> voices = new ArrayList<File>();
  245. List<String> tempPaths = new ArrayList<String>();
  246. // 文件保存的临时路径
  247. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  248. // 拼接年月日路径
  249. String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
  250. try {
  251. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  252. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  253. // 创建文件夹
  254. File file = new File(tempPath + datePath);
  255. if (!file.exists()) {
  256. file.mkdirs();
  257. }
  258. String fileName = null;
  259. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  260. // 上传文件
  261. MultipartFile mf = entity.getValue();
  262. fileName = mf.getOriginalFilename();
  263. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  264. // 重命名文件
  265. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  266. String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
  267. File uploadFile = new File(tempPath + datePath + newFileName);
  268. // 拷贝文件流到指定文件路径
  269. FileCopyUtils.copy(mf.getBytes(), uploadFile);
  270. // 添加到上传成功数组中
  271. voices.add(uploadFile);
  272. tempPaths.add(datePath + newFileName);
  273. }
  274. String urls = "";
  275. for (String voice : tempPaths) {
  276. if (urls.length() == 0) {
  277. urls = voice;
  278. } else {
  279. urls += "," + voice;
  280. }
  281. }
  282. JSONObject json = new JSONObject();
  283. json.put("status", 200);
  284. json.put("msg", "上传成功");
  285. // 图片标识对象的HTTP链接
  286. json.put("urls", urls);
  287. System.out.println("语音上传:" + json.toString());
  288. return json.toString();
  289. } catch (Exception e) {
  290. error(e);
  291. try {
  292. // 清除垃圾图片
  293. for (File file : voices) {
  294. FileUtils.forceDelete(file);
  295. }
  296. } catch (Exception e2) {
  297. error(e2);
  298. }
  299. return error(-1, "上传失败");
  300. }
  301. }
  302. /**
  303. * 语音上传
  304. * @param request
  305. * @param response
  306. * @return
  307. */
  308. @RequestMapping(value = "test", method = RequestMethod.POST)
  309. @ResponseBody
  310. public String test(HttpServletRequest request, HttpServletResponse response) throws Exception {
  311. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  312. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  313. List<String> tempPaths = new ArrayList<String>();
  314. String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
  315. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  316. MultipartFile mf = entity.getValue();
  317. String fileName = mf.getOriginalFilename();
  318. String tempPath = CommonUtil.saveVoiceToDisk(mf.getInputStream(),fileName);
  319. String pngPath = tempPath.substring(0,tempPath.lastIndexOf("."))+".png";
  320. File tempFile = new File(tempPath);
  321. File pngFile = new File(pngPath);
  322. long times = CommonUtil.getVideoTimeAndImg(tempPath,pngPath);
  323. ObjectNode imgNode = fastDFSUtil.upload(new FileInputStream(pngPath),"png","");
  324. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  325. ObjectNode videoNode = fastDFSUtil.upload(new FileInputStream(tempPath),fileExt,"");
  326. tempPaths.add(fastUrl + imgNode.get("groupName").toString().replaceAll("\"","")
  327. + "/" + imgNode.get("remoteFileName").toString().replaceAll("\"",""));
  328. tempPaths.add(fastUrl + videoNode.get("groupName").toString().replaceAll("\"","")
  329. + "/" + videoNode.get("remoteFileName").toString().replaceAll("\"",""));
  330. tempPaths.add(times+"");
  331. if(tempFile!=null){
  332. tempFile.delete();
  333. }
  334. if(pngFile!=null){
  335. pngFile.delete();
  336. }
  337. }
  338. String urls = String.join(",", tempPaths);
  339. return urls;
  340. }
  341. }