123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.doc.biz.controller;
- import com.alibaba.fastjson2.JSON;
- import com.doc.biz.domain.DocMsg;
- import com.doc.biz.service.IMongoService;
- import com.doc.biz.vo.DocumentVO;
- import com.doc.common.core.controller.BaseController;
- import com.doc.common.core.domain.AjaxResult;
- import com.doc.common.utils.SecurityUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import org.yaml.snakeyaml.util.UriEncoder;
- import javax.annotation.Resource;
- import java.util.List;
- import java.util.Objects;
- /**
- * 文件上传
- *
- * @author wukai
- * @date 2023-08-15
- */
- @Slf4j
- @RestController
- @RequestMapping("/api")
- public class FileUploadController extends BaseController {
- /**
- * 文件上传实现类
- */
- @Resource
- private IMongoService mongoService;
- @Resource
- private StringRedisTemplate stringRedisTemplate;
- /**
- * 文件上传
- *
- * @param msg
- * @return
- */
- @PostMapping("/send")
- public AjaxResult send(@RequestBody DocMsg msg) {
- try {
- msg.setFrom(SecurityUtils.getUserId());
- stringRedisTemplate.convertAndSend("ws-chat", JSON.toJSONString(msg));
- return success();
- } catch (Exception e) {
- log.error("文件上传失败:", e);
- return error(e.getMessage());
- }
- }
- /**
- * 文件上传
- *
- * @param file
- * @return
- */
- @PostMapping("/upload")
- public AjaxResult uploadFile(@RequestParam(value = "file") MultipartFile file) {
- try {
- return success(mongoService.uploadFile(file));
- } catch (Exception e) {
- log.error("文件上传失败:", e);
- return error(e.getMessage());
- }
- }
- /**
- * 多文件上传
- *
- * @param files
- * @return
- */
- @PostMapping("/uploadFiles")
- public AjaxResult uploadFile(@RequestParam(value = "files") List<MultipartFile> files) {
- try {
- return success(mongoService.uploadFiles(files));
- } catch (Exception e) {
- return error(e.getMessage());
- }
- }
- /**
- * 文件下载
- *
- * @param fileId
- * @return
- */
- @GetMapping("/download/{fileId}")
- public ResponseEntity<Object> fileDownload(@PathVariable(name = "fileId") String fileId) {
- DocumentVO mongoFileVo = mongoService.downloadFile(fileId);
- if (Objects.nonNull(mongoFileVo)) {
- return ResponseEntity.ok()
- .header(HttpHeaders.CONTENT_DISPOSITION, "filename=\"" + UriEncoder.encode(mongoFileVo.getFileName()) + "\"")
- .header(HttpHeaders.CONTENT_TYPE, mongoFileVo.getContentType())
- .header(HttpHeaders.CONTENT_LENGTH, mongoFileVo.getFileSize() + "").header("Connection", "close")
- .body(mongoFileVo.getData());
- } else {
- return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
- }
- }
- /**
- * 文件删除
- *
- * @param fileId
- * @return
- */
- @DeleteMapping("/remove/{fileId}")
- public AjaxResult removeFile(@PathVariable(name = "fileId") String fileId) {
- mongoService.removeFile(fileId);
- return success("删除成功");
- }
- }
|