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+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
package com.fuint.module.backendApi.controller;
|
|
|
|
|
|
-import com.aliyun.oss.OSS;
|
|
|
import com.fuint.common.dto.AccountInfo;
|
|
|
import com.fuint.common.service.SettingService;
|
|
|
-import com.fuint.common.util.AliyunOssUtil;
|
|
|
import com.fuint.common.util.CommonUtil;
|
|
|
import com.fuint.common.util.DateUtil;
|
|
|
import com.fuint.common.util.TokenUtil;
|
|
|
import com.fuint.framework.web.BaseController;
|
|
|
import com.fuint.framework.web.ResponseObject;
|
|
|
import com.fuint.utils.StringUtil;
|
|
|
+import com.obs.services.model.PutObjectRequest;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
@@ -19,16 +211,15 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
-import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.File;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
+import com.obs.services.ObsClient;
|
|
|
|
|
|
|
|
|
* 文件上传管理控制类
|
|
@@ -36,7 +227,7 @@ import java.util.UUID;
|
|
|
* Created by FSQ
|
|
|
* CopyRight https:
|
|
|
*/
|
|
|
-@Api(tags="管理端-文件上传相关接口")
|
|
|
+@Api(tags = "管理端-文件上传相关接口")
|
|
|
@RestController
|
|
|
@AllArgsConstructor
|
|
|
@RequestMapping(value = "/backendApi/file")
|
|
@@ -46,143 +237,137 @@ public class BackendFileController extends BaseController {
|
|
|
|
|
|
|
|
|
* 环境变量
|
|
|
- * */
|
|
|
+ */
|
|
|
private Environment env;
|
|
|
|
|
|
|
|
|
* 系统设置服务接口
|
|
|
- * */
|
|
|
+ */
|
|
|
private SettingService settingService;
|
|
|
|
|
|
|
|
|
* 后台上传文件
|
|
|
*
|
|
|
- * @param request
|
|
|
- * @throws
|
|
|
+ * @param uploadFile
|
|
|
+ * @param token
|
|
|
+ * @return ResponseObject
|
|
|
*/
|
|
|
@ApiOperation(value = "后台上传文件")
|
|
|
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
|
|
@CrossOrigin
|
|
|
- public ResponseObject uploadFileLocal(HttpServletRequest request) {
|
|
|
- String token = request.getHeader("Access-Token");
|
|
|
- String action = request.getParameter("action") == null ? "" : request.getParameter("action");
|
|
|
+ public ResponseObject uploadFileLocal(MultipartFile uploadFile, String token) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
- AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
|
|
|
- if (accountInfo == null) {
|
|
|
- return getFailureResult(1001, "请先登录");
|
|
|
+
|
|
|
+ if (uploadFile == null || uploadFile.isEmpty()) {
|
|
|
+ return getFailureResult(201, "上传的文件不能为空");
|
|
|
}
|
|
|
|
|
|
- if (action.equals("config")) {
|
|
|
- Map<String, Object> outParams = new HashMap();
|
|
|
- outParams.put("imageActionName", "upload");
|
|
|
- outParams.put("fileActionName", "upload");
|
|
|
- outParams.put("fileFieldName", "file");
|
|
|
- outParams.put("imageFieldName", "file");
|
|
|
- outParams.put("fileUrlPrefix", "");
|
|
|
- outParams.put("imageUrlPrefix", "");
|
|
|
- return getSuccessResult(outParams);
|
|
|
- }
|
|
|
-
|
|
|
- MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
- String sourcePic = request.getParameter("sourcePic");
|
|
|
- MultipartFile file = multipartRequest.getFile(sourcePic);
|
|
|
-
|
|
|
- if (file == null) {
|
|
|
- Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
- if (fileMap.size() > 0) {
|
|
|
- file = fileMap.get("file");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, String> resultMap = new HashMap<>();
|
|
|
- String originalFilename = file.getOriginalFilename();
|
|
|
+
|
|
|
+ String originalFilename = uploadFile.getOriginalFilename();
|
|
|
if (StringUtil.isEmpty(originalFilename)) {
|
|
|
- return getFailureResult(201, "上传出错啦");
|
|
|
+ return getFailureResult(201, "上传文件获取失败");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
String maxSizeStr = env.getProperty("images.upload.maxSize");
|
|
|
-
|
|
|
-
|
|
|
- float maxSize = 2;
|
|
|
+ float maxSize = 2;
|
|
|
try {
|
|
|
maxSize = Float.parseFloat(maxSizeStr);
|
|
|
} catch (NumberFormatException e) {
|
|
|
logger.error("图片允许的大小设置不正确", e);
|
|
|
}
|
|
|
- if (file.getSize() > (maxSize * 1024 * 1024)) {
|
|
|
- return getFailureResult(201, "上传的文件不能大于" + maxSize + "MB");
|
|
|
+
|
|
|
+
|
|
|
+ if (uploadFile.getSize() > (maxSize * 1024 * 1024)) {
|
|
|
+ return getFailureResult(201, "上传的文件不能大于 " + maxSize + "MB");
|
|
|
}
|
|
|
|
|
|
- String fileType = file.getContentType();
|
|
|
+
|
|
|
+ String fileType = uploadFile.getContentType();
|
|
|
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
- String fileName = saveFile(file);
|
|
|
+ String fileName = saveFile(uploadFile);
|
|
|
String baseImage = settingService.getUploadBasePath();
|
|
|
String filePath = baseImage + fileName;
|
|
|
String url = filePath;
|
|
|
|
|
|
-
|
|
|
- String mode = env.getProperty("aliyun.oss.mode");
|
|
|
- if (mode.equals("1") && fileType.indexOf("image") >= 0) {
|
|
|
- String endpoint = env.getProperty("aliyun.oss.endpoint");
|
|
|
- String accessKeyId = env.getProperty("aliyun.oss.accessKeyId");
|
|
|
- String accessKeySecret = env.getProperty("aliyun.oss.accessKeySecret");
|
|
|
- String bucketName = env.getProperty("aliyun.oss.bucketName");
|
|
|
- String folder = env.getProperty("aliyun.oss.folder");
|
|
|
- String domain = env.getProperty("aliyun.oss.domain");
|
|
|
- OSS ossClient = AliyunOssUtil.getOSSClient(accessKeyId, accessKeySecret, endpoint);
|
|
|
+
|
|
|
+ String mode = env.getProperty("huaweicloud.obs.mode");
|
|
|
+ if (mode.equals("1") && fileType.indexOf("image") >= 0) {
|
|
|
+ String endpoint = env.getProperty("huaweicloud.obs.endpoint");
|
|
|
+ String accessKeyId = env.getProperty("huaweicloud.obs.accessKey");
|
|
|
+ String accessKeySecret = env.getProperty("huaweicloud.obs.secretKey");
|
|
|
+ String bucketName = env.getProperty("huaweicloud.obs.bucketName");
|
|
|
+ String folder = env.getProperty("huaweicloud.obs.folder");
|
|
|
+ String domain = env.getProperty("huaweicloud.obs.domain");
|
|
|
+
|
|
|
+
|
|
|
+ ObsClient obsClient = new ObsClient(accessKeyId, accessKeySecret, endpoint);
|
|
|
String pathRoot = env.getProperty("images.root");
|
|
|
if (pathRoot == null || StringUtil.isEmpty(pathRoot)) {
|
|
|
pathRoot = ResourceUtils.getURL("classpath:").getPath();
|
|
|
}
|
|
|
- File ossFile = new File(pathRoot + fileName);
|
|
|
- fileName = AliyunOssUtil.upload(ossClient, ossFile, bucketName, folder);
|
|
|
- filePath = domain + fileName;
|
|
|
+ File obsFile = new File(pathRoot + fileName);
|
|
|
+ fileName = folder + "/" + fileName;
|
|
|
+
|
|
|
+ obsClient.putObject(new PutObjectRequest(bucketName, fileName, obsFile));
|
|
|
+
|
|
|
+ filePath = domain + "/" + fileName;
|
|
|
url = filePath;
|
|
|
}
|
|
|
|
|
|
+ Map<String, String> resultMap = new HashMap<>();
|
|
|
resultMap.put("status", "success");
|
|
|
resultMap.put("domain", baseImage);
|
|
|
resultMap.put("filePath", filePath);
|
|
|
resultMap.put("fileName", fileName);
|
|
|
resultMap.put("state", "SUCCESS");
|
|
|
- resultMap.put("original", file.getOriginalFilename());
|
|
|
- resultMap.put("size", file.getSize()+"");
|
|
|
+ resultMap.put("original", uploadFile.getOriginalFilename());
|
|
|
+ resultMap.put("size", uploadFile.getSize() + "");
|
|
|
resultMap.put("title", fileName);
|
|
|
- resultMap.put("type", file.getContentType());
|
|
|
+ resultMap.put("type", uploadFile.getContentType());
|
|
|
resultMap.put("url", url);
|
|
|
- String ip = CommonUtil.getIPFromHttpRequest(request);
|
|
|
+ String ip = CommonUtil.getIPFromHttpRequest(null);
|
|
|
logger.info("用户ip:{},上传文件url:{}", ip, url);
|
|
|
+
|
|
|
+ return getSuccessResult(resultMap);
|
|
|
} catch (Exception e) {
|
|
|
return getFailureResult(201, "上传失败,请检查上传配置及权限");
|
|
|
}
|
|
|
-
|
|
|
- return getSuccessResult(resultMap);
|
|
|
}
|
|
|
|
|
|
- public String saveFile(MultipartFile file) throws Exception {
|
|
|
- String fileName = file.getOriginalFilename();
|
|
|
-
|
|
|
- String imageName = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+
|
|
|
+ * 保存文件到本地磁盘
|
|
|
+ *
|
|
|
+ * @param uploadFile
|
|
|
+ * @return 文件路径
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public String saveFile(MultipartFile uploadFile) throws Exception {
|
|
|
+ String fileName = uploadFile.getOriginalFilename();
|
|
|
+ String fileExtension = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
|
|
String pathRoot = env.getProperty("images.root");
|
|
|
if (pathRoot == null || StringUtil.isEmpty(pathRoot)) {
|
|
|
pathRoot = ResourceUtils.getURL("classpath:").getPath();
|
|
|
}
|
|
|
- String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
-
|
|
|
String baseImage = env.getProperty("images.path");
|
|
|
- String filePath = baseImage + DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
|
|
|
-
|
|
|
- String path = filePath + uuid + imageName;
|
|
|
+ String filePath = baseImage + DateUtil.formatDate(new Date(), "yyyyMMdd") + "/";
|
|
|
+ String path = filePath + uuid + fileExtension;
|
|
|
|
|
|
try {
|
|
|
File tempFile = new File(pathRoot + path);
|
|
|
if (!tempFile.getParentFile().exists()) {
|
|
|
tempFile.getParentFile().mkdirs();
|
|
|
}
|
|
|
- CommonUtil.saveMultipartFile(file, pathRoot + path);
|
|
|
+ CommonUtil.saveMultipartFile(uploadFile, pathRoot + path);
|
|
|
} catch (Exception e) {
|
|
|
throw new Exception("上传失败,请检查目录是否可写");
|
|
|
}
|
|
@@ -190,3 +375,5 @@ public class BackendFileController extends BaseController {
|
|
|
return path;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|