UtilController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.om.controller.admin;
  2. import com.om.utils.HuaweiObsUtil;
  3. import com.om.utils.Result;
  4. import io.swagger.annotations.Api;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import org.springframework.web.multipart.MultipartHttpServletRequest;
  13. import javax.servlet.ServletOutputStream;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. @RestController
  22. @Api(tags = "文件上传控制类")
  23. public class UtilController {
  24. //人脸信息储存位置
  25. @Value("${files.upload.path}")
  26. private String fileUploadPath;
  27. //临时照片存储位置
  28. @Value("${photo.tmp}")
  29. private String tmpPhoto;
  30. private final HuaweiObsUtil huaweiObsUtil;
  31. public UtilController(HuaweiObsUtil huaweiObsUtil) {
  32. this.huaweiObsUtil = huaweiObsUtil;
  33. }
  34. @ApiOperation("文件下载接口")
  35. @GetMapping("/download")
  36. public void download(String name, HttpServletResponse response) {
  37. try {
  38. //输入流,通过输入流读取文件内容
  39. FileInputStream fileInputStream = new FileInputStream(fileUploadPath + name);
  40. //输出流,通过输出流将文件写回浏览器,在浏览器展示图片
  41. ServletOutputStream outputStream = response.getOutputStream();
  42. response.setContentType("image/png");
  43. int len = 0;
  44. byte[] bytes = new byte[1024];
  45. while ((len = fileInputStream.read(bytes)) != -1) {
  46. outputStream.write(bytes, 0, len);
  47. }
  48. outputStream.close();
  49. fileInputStream.close();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. @ApiOperation("图片上传接口")
  55. @PostMapping("/photo")
  56. public Result photo(@RequestParam("file") MultipartFile file,
  57. HttpServletRequest request,
  58. @RequestParam("id") Integer id) {
  59. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  60. MultipartFile headerImage = multipartRequest.getFile("file");
  61. String fileName = headerImage.getOriginalFilename();
  62. //根据当前时间生成文件名
  63. fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + "." + fileName;
  64. // 确定文件存放的路径
  65. File dest = new File(fileUploadPath + fileName);
  66. //判断当前目录是否存在
  67. if (!dest.exists()) {
  68. //目录不存在,需要创建
  69. dest.mkdir();
  70. }
  71. try {
  72. // 存储文件
  73. headerImage.transferTo(dest);
  74. } catch (IOException e) {
  75. throw new RuntimeException("上传文件失败,服务器发生异常!", e);
  76. }
  77. System.out.println("文件名:" + fileName);
  78. return Result.succ(fileName);
  79. }
  80. @ApiOperation("视频下载接口")
  81. @GetMapping("/downloadv")
  82. public void downloadv(String name, HttpServletResponse response) {
  83. try {
  84. //输入流,通过输入流读取文件内容
  85. FileInputStream fileInputStream = new FileInputStream(fileUploadPath + name);
  86. //输出流,通过输出流将文件写回浏览器,在浏览器展示图片
  87. ServletOutputStream outputStream = response.getOutputStream();
  88. response.setContentType("video/mp4");
  89. int len;
  90. byte[] bytes = new byte[4096];
  91. while ((len = fileInputStream.read(bytes)) != -1) {
  92. outputStream.write(bytes, 0, len);
  93. }
  94. outputStream.close();
  95. fileInputStream.close();
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. @ApiOperation("视频上传接口")
  101. @PostMapping(value = "/video",headers="content-type=multipart/form-data")
  102. public Result video(@RequestParam("file") MultipartFile file,
  103. HttpServletRequest request,
  104. @RequestParam("prefix") String prefix) throws IOException {
  105. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  106. MultipartFile headerImage = multipartRequest.getFile("file");
  107. if (headerImage != null) {
  108. String fileUrl = huaweiObsUtil.upload(headerImage, prefix);
  109. if (fileUrl == null) return Result.fail("上传失败!");
  110. else return Result.succ(fileUrl);
  111. } else {
  112. return Result.fail("文件不正确!");
  113. }
  114. // 以下是上传到本服务器的代码:
  115. // String fileName = headerImage.getOriginalFilename();
  116. //
  117. // //根据当前时间生成文件名
  118. // fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + "." + fileName;
  119. // // 确定文件存放的路径
  120. // File dest = new File(fileUploadPath + fileName);
  121. // //判断当前目录是否存在
  122. // if (!dest.exists()) {
  123. // //目录不存在,需要创建
  124. // dest.mkdir();
  125. // }
  126. // try {
  127. // // 存储文件
  128. // headerImage.transferTo(dest);
  129. // } catch (IOException e) {
  130. // throw new RuntimeException("上传文件失败,服务器发生异常!", e);
  131. // }
  132. //
  133. // System.out.println("文件名:" + fileName);
  134. //
  135. // return Result.succ(fileName);
  136. }
  137. }