123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package cn.stylefeng.guns.zjxl.cnotroller;
- import cn.stylefeng.guns.zjxl.model.UploadModel;
- import cn.stylefeng.guns.zjxl.model.ret.BaseResultModel;
- import cn.stylefeng.guns.zjxl.model.ret.ResultOneModel;
- import cn.stylefeng.guns.zjxlUtil.FastDFSUtil;
- import com.alibaba.fastjson.JSONObject;
- import com.fasterxml.jackson.databind.node.ObjectNode;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.IOException;
- /***
- * @ClassName: GcFileUploadController
- * @Description:
- * @Auther: shi kejing
- * @Date: 2020/11/12 19:47
- */
- @Controller
- @RequestMapping(value = "/zjxl/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
- @ResponseBody
- @Api(description = "文件上传")
- public class GcFileUploadController {
- private Logger logger = (Logger) LoggerFactory.getLogger(GcFileUploadController.class);
- @Autowired
- FastDFSUtil fastDFSUtil;
- @Value("${fastDFS.fastdfs_file_url}")
- private String fastdfs_file_url;
- /**
- * 聊天附件上传
- *
- * @return
- * @throws IOException
- * @throws IllegalStateException
- */
- @RequestMapping(value = "/file", method = RequestMethod.POST)
- @ApiOperation(value = "单文件上传")
- public ResultOneModel<UploadModel> uploadFile(
- @ApiParam(value = "文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file
- ) {
- try {
- logger.info("单文件上传file:"+file);
- UploadModel uploadModel = fileUpload(file);
- return new ResultOneModel<UploadModel>(uploadModel);
- } catch (Exception e) {
- return new ResultOneModel(BaseResultModel.statusEm.file_upload_error.getCode(), BaseResultModel.statusEm.file_upload_error.getMessage() + "," + e.getMessage(), new JSONObject());
- }
- }
- /**
- * 文件上传到fastDfs
- *
- * @param mf
- * @return
- */
- private UploadModel fileUpload(MultipartFile mf) throws Exception {
- UploadModel uploadModel = new UploadModel();
- // 得到文件的完整名称 xxx.txt
- String fullName = mf.getOriginalFilename();
- //得到文件类型
- String fileType = fullName.substring(fullName.lastIndexOf(".") + 1).toLowerCase();
- String fileName = fullName.substring(0, fullName.lastIndexOf("."));
- //上传到fastdfs
- logger.info("GcFileUploadController→mf==="+mf);
- ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream(), fileType, "");
- //解析返回的objectNode
- uploadModel.setFileName(fileName);
- uploadModel.setFileType(fileType);
- uploadModel.setFullUri(objectNode.get("fid").toString().replaceAll("\"", ""));
- uploadModel.setFullUrl(fastdfs_file_url + objectNode.get("fid").toString().replaceAll("\"", ""));
- return uploadModel;
- }
- }
|