|
@ -0,0 +1,84 @@
|
|
|
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.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 {
|
|
|
|
|
|
@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 {
|
|
|
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
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
}
|