UploadPicController.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.fjs.scenic.controller;
  2. import java.io.*;
  3. import java.net.SocketException;
  4. import javax.annotation.PostConstruct;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpSession;
  7. import com.fasterxml.jackson.annotation.ObjectIdGenerators;
  8. import org.apache.tomcat.util.http.fileupload.FileUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.util.Assert;
  12. import org.springframework.util.StringUtils;
  13. import org.springframework.web.bind.annotation.DeleteMapping;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.springframework.web.context.request.RequestContextHolder;
  21. import org.springframework.web.context.request.ServletRequestAttributes;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import com.fjs.scenic.upload.FileUtil;
  24. import com.fjs.scenic.utils.Cons;
  25. import com.fjs.scenic.utils.Data;
  26. import com.fjs.scenic.utils.FtpFileUtil;
  27. import com.fjs.scenic.utils.ImgeNameUtils;
  28. import com.fjs.scenic.utils.ReturnResult;
  29. /**
  30. * @author 作者 :yangya
  31. * @version 创建时间:2018年3月29日 下午2:10:36 类说明:
  32. */
  33. @RestController
  34. @RequestMapping("/upload")
  35. public class UploadPicController {
  36. @Value("${FTP_ADDRESS}")
  37. private String ftpAddress;
  38. @Value("${FTP_PORT}")
  39. private int ftpPort;
  40. @Value("${FTP_USERNAME}")
  41. private String ftpUserName;
  42. @Value("${FTP_PASSWORD}")
  43. private String ftpPassword;
  44. @Value("${FTP_BASEPATH}")
  45. private String ftpBasePath;
  46. @Value("${FTP_VOID_BASEPATH}")
  47. private String ftpVoidBasePath;
  48. @Value("${NGINX_PORT}")
  49. private String nginxPort;
  50. @Autowired
  51. private FileUtil fileUtil;
  52. @PostConstruct
  53. public void init() {
  54. FtpFileUtil.FTP_ADDRESS = this.ftpAddress;
  55. FtpFileUtil.FTP_PORT = this.ftpPort;
  56. FtpFileUtil.FTP_USERNAME = this.ftpUserName;
  57. FtpFileUtil.FTP_PASSWORD = this.ftpPassword;
  58. FtpFileUtil.FTP_BASEPATH = this.ftpBasePath;
  59. FtpFileUtil.FTP_VOID_BASEPATH = this.ftpVoidBasePath;
  60. FtpFileUtil.NGINX_PORT = this.nginxPort;
  61. }
  62. /**
  63. * 上传图片
  64. */
  65. @PostMapping("/pic")
  66. public ReturnResult uploadImg(@RequestParam("upload") MultipartFile file) throws IOException {
  67. try {
  68. String uploadFile = fileUtil.uploadFile(file);
  69. return ReturnResult.ok(new Data(uploadFile, ""));
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. return ReturnResult.error("上传失败");
  73. }
  74. // return upload(file);
  75. }
  76. /**
  77. * 上传视频
  78. */
  79. @PostMapping("/void")
  80. public ReturnResult uploadVoid(@RequestParam("upload") MultipartFile file, HttpSession session,String sessionID) throws IOException {
  81. // ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  82. // HttpServletRequest request = servletRequestAttributes.getRequest();
  83. return ReturnResult.ok(FtpFileUtil.uploadFile(file, session,sessionID));
  84. }
  85. /**
  86. * 获取进度
  87. */
  88. @PostMapping("/getProcess")
  89. public ReturnResult getProcess(HttpSession session,String sessionID) throws IOException {
  90. String process = (String) session.getAttribute(sessionID);
  91. System.out.println(process);
  92. if(process==null){
  93. return ReturnResult.ok(0);
  94. }else if (process.equals("100")) {
  95. session.removeAttribute(sessionID);
  96. return ReturnResult.ok(100);
  97. } else {
  98. return ReturnResult.ok(process);
  99. }
  100. }
  101. /**
  102. * 上传
  103. *
  104. * @param file 上传文件
  105. * @param isVoid 是否是上传视频
  106. * @return
  107. * @throws IOException
  108. */
  109. public ReturnResult upload(MultipartFile file, boolean isVoid) throws IOException {
  110. String fileName = file.getOriginalFilename();
  111. String newName = ImgeNameUtils.genImageName();
  112. // 新名字
  113. newName = newName + fileName.substring(fileName.lastIndexOf("."));
  114. InputStream inputStream = file.getInputStream();
  115. String picUrl = isVoid ? FtpFileUtil.uploadVoidFile(newName, inputStream) : FtpFileUtil.uploadFile(newName, inputStream);
  116. if (!StringUtils.isEmpty(picUrl)) {
  117. return ReturnResult.ok(new Data(picUrl, ""));
  118. } else {
  119. return ReturnResult.build(Cons.RESULT_ERR, "上传失败");
  120. }
  121. }
  122. /**
  123. * 上传图片
  124. *
  125. * @param file
  126. * @return
  127. * @throws IOException
  128. */
  129. public ReturnResult upload(MultipartFile file) throws IOException {
  130. return upload(file, false);
  131. }
  132. /**
  133. * 上传图片
  134. */
  135. @PostMapping("/data")
  136. public ReturnResult uploadData(@RequestParam("fileData") MultipartFile file, String fileName) throws IOException {
  137. Assert.notNull(fileName, "manager-文件后缀名不能为空.");
  138. // 新名字
  139. String newName = ImgeNameUtils.genImageName();
  140. newName = newName + fileName.substring(fileName.lastIndexOf("."));
  141. InputStream inputStream = file.getInputStream();
  142. String picUrl = FtpFileUtil.uploadFile(newName, inputStream);
  143. if (!StringUtils.isEmpty(picUrl)) {
  144. return ReturnResult.ok(new Data(picUrl, ""));
  145. } else {
  146. return ReturnResult.build(Cons.RESULT_ERR, "上传失败");
  147. }
  148. }
  149. /**
  150. * 删除图片
  151. */
  152. @DeleteMapping("/delete/{fileName}/{fix}")
  153. public @ResponseBody
  154. ReturnResult refundList(@PathVariable String fileName, @PathVariable String fix) {
  155. Assert.notNull(fileName, "文件名不能为空.");
  156. Assert.notNull(fix, "文件格式后缀不能为空.");
  157. fileName = fileName + "." + fix;
  158. return new ReturnResult(Cons.RESULT_OK, "请求成功", FtpFileUtil.deleteFile(fileName));
  159. }
  160. /**
  161. * 下载视频到本地
  162. *
  163. * @param fileName 文件名称
  164. * @return 文件相对地址
  165. */
  166. @PostMapping("/downVoid")
  167. public ReturnResult downVoid(String fileName) {
  168. if (fileName == null) {
  169. return ReturnResult.build(500, "文件名不能爲空");
  170. }
  171. fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
  172. char seq = File.separatorChar;
  173. //判断文件是否已存在
  174. String picPath = "fjs-scenic-manager" + seq + "src" + seq + "main" + seq + "resources" + seq + "static" + seq + "void" + seq;
  175. File pic = new File(picPath + fileName);
  176. File pic1 = new File("fjs-scenic-manager");
  177. if (!pic.exists()) {
  178. try {
  179. FileUtils.cleanDirectory(new File(picPath));
  180. pic.createNewFile();
  181. FtpFileUtil.downloadFtpVoidFile(picPath, fileName);
  182. } catch (IOException e) {
  183. e.printStackTrace();
  184. }
  185. }
  186. return ReturnResult.ok("../../void/" + fileName);
  187. }
  188. }