FileUploadController.java 11 KB

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