GcFileUploadController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package cn.stylefeng.guns.zjxl.cnotroller;
  2. import cn.stylefeng.guns.zjxl.model.UploadModel;
  3. import cn.stylefeng.guns.zjxl.model.ret.BaseResultModel;
  4. import cn.stylefeng.guns.zjxl.model.ret.ResultOneModel;
  5. import cn.stylefeng.guns.zjxlUtil.FastDFSUtil;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.fasterxml.jackson.databind.node.ObjectNode;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import io.swagger.annotations.ApiParam;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestMethod;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import java.io.IOException;
  23. /***
  24. * @ClassName: GcFileUploadController
  25. * @Description:
  26. * @Auther: shi kejing
  27. * @Date: 2020/11/12 19:47
  28. */
  29. @Controller
  30. @RequestMapping(value = "/zjxl/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  31. @ResponseBody
  32. @Api(description = "文件上传")
  33. public class GcFileUploadController {
  34. private Logger logger = (Logger) LoggerFactory.getLogger(GcFileUploadController.class);
  35. @Autowired
  36. FastDFSUtil fastDFSUtil;
  37. @Value("${fastDFS.fastdfs_file_url}")
  38. private String fastdfs_file_url;
  39. /**
  40. * 聊天附件上传
  41. *
  42. * @return
  43. * @throws IOException
  44. * @throws IllegalStateException
  45. */
  46. @RequestMapping(value = "/file", method = RequestMethod.POST)
  47. @ApiOperation(value = "单文件上传")
  48. public ResultOneModel<UploadModel> uploadFile(
  49. @ApiParam(value = "文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file
  50. ) {
  51. try {
  52. logger.info("单文件上传file:"+file);
  53. UploadModel uploadModel = fileUpload(file);
  54. return new ResultOneModel<UploadModel>(uploadModel);
  55. } catch (Exception e) {
  56. return new ResultOneModel(BaseResultModel.statusEm.file_upload_error.getCode(), BaseResultModel.statusEm.file_upload_error.getMessage() + "," + e.getMessage(), new JSONObject());
  57. }
  58. }
  59. /**
  60. * 文件上传到fastDfs
  61. *
  62. * @param mf
  63. * @return
  64. */
  65. private UploadModel fileUpload(MultipartFile mf) throws Exception {
  66. UploadModel uploadModel = new UploadModel();
  67. // 得到文件的完整名称 xxx.txt
  68. String fullName = mf.getOriginalFilename();
  69. //得到文件类型
  70. String fileType = fullName.substring(fullName.lastIndexOf(".") + 1).toLowerCase();
  71. String fileName = fullName.substring(0, fullName.lastIndexOf("."));
  72. //上传到fastdfs
  73. logger.info("GcFileUploadController→mf==="+mf);
  74. ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream(), fileType, "");
  75. //解析返回的objectNode
  76. uploadModel.setFileName(fileName);
  77. uploadModel.setFileType(fileType);
  78. uploadModel.setFullUri(objectNode.get("fid").toString().replaceAll("\"", ""));
  79. uploadModel.setFullUrl(fastdfs_file_url + objectNode.get("fid").toString().replaceAll("\"", ""));
  80. return uploadModel;
  81. }
  82. }