|
@ -0,0 +1,110 @@
|
|
|
package com.yihu.wlyy.web.third.gateway.controller;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.yihu.wlyy.util.CommonUtil;
|
|
|
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
|
|
|
import com.yihu.wlyy.web.third.gateway.vo.UploadModel;
|
|
|
import com.yihu.wlyy.web.third.gateway.vo.base.BaseResultModel;
|
|
|
import com.yihu.wlyy.web.third.gateway.vo.base.ResultOneModel;
|
|
|
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.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/8/29.
|
|
|
*/
|
|
|
@Controller
|
|
|
@RequestMapping(value = "/wlyygc/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
@ResponseBody
|
|
|
@Api(description = "文件上传")
|
|
|
public class GcFileUploadController {
|
|
|
|
|
|
@Autowired
|
|
|
FastDFSUtil fastDFSUtil;
|
|
|
@Autowired
|
|
|
private com.yihu.wlyy.util.CommonUtil CommonUtil;
|
|
|
@Value("${fastDFS.fastdfs_file_url}")
|
|
|
private String fastdfs_file_url;
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(GcFileUploadController.class);
|
|
|
|
|
|
/**
|
|
|
* 聊天附件上传
|
|
|
*
|
|
|
* @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());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/files", method = RequestMethod.POST)
|
|
|
@ApiOperation(value = "多文件上传")
|
|
|
public ResultOneModel uploadFiles(
|
|
|
@ApiParam(value = "文件", allowMultiple = true, required = true) @RequestPart(value = "files", required = true) MultipartFile[] files
|
|
|
) {
|
|
|
try {
|
|
|
List<UploadModel> uploadModels = new ArrayList<>();
|
|
|
for (MultipartFile file : files) {
|
|
|
uploadModels.add(fileUpload(file));
|
|
|
}
|
|
|
return new ResultOneModel(uploadModels);
|
|
|
} catch (Exception e) {
|
|
|
return new ResultOneModel(new JSONArray());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 文件上传到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;
|
|
|
}
|
|
|
}
|