FileUploadController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 = "image", method = RequestMethod.POST/* , headers = "Accept=image/png" */)
  38. @ResponseBody
  39. public String image(HttpServletRequest request, HttpServletResponse response) {
  40. // 圖片列表
  41. List<File> images = new ArrayList<File>();
  42. List<String> tempPaths = new ArrayList<String>();
  43. // 文件保存的临时路径
  44. String tempPath = SystemConf.getInstance().getTempPath() + 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. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  57. // 上传文件
  58. MultipartFile mf = entity.getValue();
  59. fileName = mf.getOriginalFilename();
  60. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  61. // 重命名文件
  62. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  63. String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
  64. File uploadFile = new File(tempPath + datePath + newFileName);
  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 = "";
  74. for (String image : tempPaths) {
  75. if (urls.length() == 0) {
  76. urls = image;
  77. } else {
  78. urls += "," + image;
  79. }
  80. }
  81. JSONObject json = new JSONObject();
  82. json.put("status", 200);
  83. json.put("msg", "上传成功");
  84. // 图片标识对象的HTTP链接
  85. json.put("urls", urls);
  86. System.out.println("图片上传:" + json.toString());
  87. return json.toString();
  88. } catch (Exception e) {
  89. error(e);
  90. try {
  91. // 清除垃圾图片
  92. for (File file : images) {
  93. FileUtils.forceDelete(file);
  94. }
  95. } catch (Exception e2) {
  96. error(e2);
  97. }
  98. return error(-1, "上传失败");
  99. }
  100. }
  101. /**
  102. * 聊天附件上传
  103. * @return
  104. * @throws IOException
  105. * @throws IllegalStateException
  106. */
  107. @RequestMapping(value = "chat", method = RequestMethod.POST)
  108. @ResponseBody
  109. public String chatFile(HttpServletRequest request, HttpServletResponse response) {
  110. // 圖片列表
  111. List<File> files = new ArrayList<File>();
  112. List<String> tempPaths = new ArrayList<String>();
  113. // 获取聊天文件保存路径
  114. String tempPath = SystemConf.getInstance().getChatPath() + File.separator;
  115. // 拼接年月日路径
  116. String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
  117. try {
  118. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  119. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  120. // 创建文件夹
  121. File file = new File(tempPath + datePath);
  122. if (!file.exists()) {
  123. file.mkdirs();
  124. }
  125. String fileName = null;
  126. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  127. // 上传文件
  128. MultipartFile mf = entity.getValue();
  129. fileName = mf.getOriginalFilename();
  130. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  131. // 重命名文件
  132. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  133. String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
  134. File uploadFile = new File(tempPath + datePath + newFileName);
  135. // 拷贝文件流到指定文件路径
  136. FileCopyUtils.copy(mf.getBytes(), uploadFile);
  137. // 添加到上传成功数组中
  138. files.add(uploadFile);
  139. tempPaths.add(datePath + newFileName);
  140. }
  141. String urls = "";
  142. for (String temp : tempPaths) {
  143. if (urls.length() == 0) {
  144. urls = SystemConf.getInstance().getChatServer() + temp;
  145. } else {
  146. urls += "," + SystemConf.getInstance().getChatServer() + temp;
  147. }
  148. }
  149. JSONObject json = new JSONObject();
  150. json.put("status", 200);
  151. json.put("msg", "上传成功");
  152. // 图片标识对象的HTTP链接
  153. json.put("urls", urls);
  154. System.out.println("附件上传:" + json.toString());
  155. return json.toString();
  156. } catch (Exception e) {
  157. error(e);
  158. try {
  159. // 清除垃圾图片
  160. for (File file : files) {
  161. FileUtils.forceDelete(file);
  162. }
  163. } catch (Exception e2) {
  164. error(e2);
  165. }
  166. return error(-1, "上传失败");
  167. }
  168. }
  169. /**
  170. * 语音上传
  171. * @param request
  172. * @param response
  173. * @return
  174. */
  175. @RequestMapping(value = "voice", method = RequestMethod.POST)
  176. @ResponseBody
  177. public String voice(HttpServletRequest request, HttpServletResponse response) {
  178. // 圖片列表
  179. List<File> voices = new ArrayList<File>();
  180. List<String> tempPaths = new ArrayList<String>();
  181. // 文件保存的临时路径
  182. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  183. // 拼接年月日路径
  184. String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
  185. try {
  186. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  187. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  188. // 创建文件夹
  189. File file = new File(tempPath + datePath);
  190. if (!file.exists()) {
  191. file.mkdirs();
  192. }
  193. String fileName = null;
  194. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  195. // 上传文件
  196. MultipartFile mf = entity.getValue();
  197. fileName = mf.getOriginalFilename();
  198. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  199. // 重命名文件
  200. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  201. String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
  202. File uploadFile = new File(tempPath + datePath + newFileName);
  203. // 拷贝文件流到指定文件路径
  204. FileCopyUtils.copy(mf.getBytes(), uploadFile);
  205. // 添加到上传成功数组中
  206. voices.add(uploadFile);
  207. tempPaths.add(datePath + newFileName);
  208. }
  209. String urls = "";
  210. for (String voice : tempPaths) {
  211. if (urls.length() == 0) {
  212. urls = voice;
  213. } else {
  214. urls += "," + voice;
  215. }
  216. }
  217. JSONObject json = new JSONObject();
  218. json.put("status", 200);
  219. json.put("msg", "上传成功");
  220. // 图片标识对象的HTTP链接
  221. json.put("urls", urls);
  222. System.out.println("语音上传:" + json.toString());
  223. return json.toString();
  224. } catch (Exception e) {
  225. error(e);
  226. try {
  227. // 清除垃圾图片
  228. for (File file : voices) {
  229. FileUtils.forceDelete(file);
  230. }
  231. } catch (Exception e2) {
  232. error(e2);
  233. }
  234. return error(-1, "上传失败");
  235. }
  236. }
  237. }