FileUploadController.java 12 KB

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