|
@@ -1,7 +1,5 @@
|
|
package com.miaxis.system.controller.common;
|
|
package com.miaxis.system.controller.common;
|
|
|
|
|
|
-import com.miaxis.common.aliyunOSS.AliyunConfig;
|
|
|
|
-import com.miaxis.common.aliyunOSS.AliyunUpload;
|
|
|
|
import com.miaxis.common.annotation.Log;
|
|
import com.miaxis.common.annotation.Log;
|
|
import com.miaxis.common.config.MiaxisConfig;
|
|
import com.miaxis.common.config.MiaxisConfig;
|
|
import com.miaxis.common.constant.Constants;
|
|
import com.miaxis.common.constant.Constants;
|
|
@@ -15,10 +13,20 @@ import com.miaxis.common.utils.file.FileUtils;
|
|
import com.miaxis.file.domain.FileInfo;
|
|
import com.miaxis.file.domain.FileInfo;
|
|
import com.miaxis.file.service.IFileInfoService;
|
|
import com.miaxis.file.service.IFileInfoService;
|
|
import com.miaxis.framework.config.ServerConfig;
|
|
import com.miaxis.framework.config.ServerConfig;
|
|
-import io.swagger.annotations.*;
|
|
|
|
|
|
+import com.qcloud.cos.COSClient;
|
|
|
|
+import com.qcloud.cos.ClientConfig;
|
|
|
|
+import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
|
|
+import com.qcloud.cos.auth.COSCredentials;
|
|
|
|
+import com.qcloud.cos.model.PutObjectRequest;
|
|
|
|
+import com.qcloud.cos.model.PutObjectResult;
|
|
|
|
+import com.qcloud.cos.region.Region;
|
|
|
|
+import io.swagger.annotations.Api;
|
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
|
+import io.swagger.annotations.ApiParam;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
@@ -27,9 +35,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.text.SimpleDateFormat;
|
|
|
|
-import java.util.Date;
|
|
|
|
|
|
+import java.util.Calendar;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 通用请求处理
|
|
* 通用请求处理
|
|
@@ -48,6 +56,16 @@ public class CommonController
|
|
@Autowired
|
|
@Autowired
|
|
private IFileInfoService fileInfoService;
|
|
private IFileInfoService fileInfoService;
|
|
|
|
|
|
|
|
+ @Value("${cos.bucketName}")
|
|
|
|
+ private String bucketName;
|
|
|
|
+ @Value("${cos.path}")
|
|
|
|
+ private String path;
|
|
|
|
+ @Value("${cos.preffix}")
|
|
|
|
+ private String preffix;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ COSClient cosClient;
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -56,9 +74,6 @@ public class CommonController
|
|
@Log(title = "上传文件", businessType = BusinessTypeEnum.INSERT)
|
|
@Log(title = "上传文件", businessType = BusinessTypeEnum.INSERT)
|
|
@PostMapping(Constants.OPEN_PREFIX+"/common/file")
|
|
@PostMapping(Constants.OPEN_PREFIX+"/common/file")
|
|
@ApiOperation("通用上传请求")
|
|
@ApiOperation("通用上传请求")
|
|
-// @ApiImplicitParams({
|
|
|
|
-// @ApiImplicitParam(name = "fileType",value = "业务类型:1-举报图片 99-其他" ,dataType = "int", paramType = "query", required = true)
|
|
|
|
-// })
|
|
|
|
public Response<FileInfo> updateload(MultipartFile file,
|
|
public Response<FileInfo> updateload(MultipartFile file,
|
|
@ApiParam(name = "fileType", value = "业务类型:98-音视频文件、1-商户logo、99-其他")
|
|
@ApiParam(name = "fileType", value = "业务类型:98-音视频文件、1-商户logo、99-其他")
|
|
@RequestParam("fileType") Integer fileType) throws IOException {
|
|
@RequestParam("fileType") Integer fileType) throws IOException {
|
|
@@ -71,87 +86,34 @@ public class CommonController
|
|
if (!validateFileSuffix(suffix,fileUploadTypeEnum)){
|
|
if (!validateFileSuffix(suffix,fileUploadTypeEnum)){
|
|
throw new CustomException("文件类型不合法");
|
|
throw new CustomException("文件类型不合法");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
|
|
|
|
+ String bucketName = this.bucketName;
|
|
|
|
+ // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
|
|
|
|
+ // 大文件上传请参照 API 文档高级 API 上传
|
|
|
|
+ File localFile = null;
|
|
|
|
+ localFile = File.createTempFile("temp",null);
|
|
|
|
+ file.transferTo(localFile);
|
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
|
+ int year = cal.get(Calendar.YEAR);
|
|
|
|
+ int month=cal.get(Calendar.MONTH)+ 1;
|
|
|
|
+ int day=cal.get(Calendar.DATE);
|
|
//存储的文件名
|
|
//存储的文件名
|
|
String storagefileName = System.currentTimeMillis() + suffix;
|
|
String storagefileName = System.currentTimeMillis() + suffix;
|
|
- String savePath = AliyunConfig.ZZJS_PATH
|
|
|
|
- + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +"/"+
|
|
|
|
- fileUploadTypeEnum.getFileType() +"/";
|
|
|
|
- String fileUrl = AliyunUpload.uploadForStream(file.getInputStream(), savePath, storagefileName);
|
|
|
|
|
|
+ // 指定要上传到 COS 上的路径
|
|
|
|
+ String key = "/"+this.preffix+"/"+fileUploadTypeEnum.getFileType()+"/"+year+"/"+month+"/"+day+"/"+storagefileName;
|
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
|
|
|
|
+ PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
|
+ String fileUrl=this.path + putObjectRequest.getKey();
|
|
|
|
+
|
|
FileInfo fileInfo = new FileInfo();
|
|
FileInfo fileInfo = new FileInfo();
|
|
fileInfo.setFileType(fileUploadTypeEnum.getFileType());
|
|
fileInfo.setFileType(fileUploadTypeEnum.getFileType());
|
|
- fileInfo.setFilePath(savePath+storagefileName);
|
|
|
|
|
|
+ fileInfo.setFilePath(key+storagefileName);
|
|
fileInfo.setFileUrl(fileUrl);
|
|
fileInfo.setFileUrl(fileUrl);
|
|
fileInfoService.save(fileInfo);
|
|
fileInfoService.save(fileInfo);
|
|
return Response.success(fileInfo);
|
|
return Response.success(fileInfo);
|
|
}
|
|
}
|
|
|
|
|
|
-// /**
|
|
|
|
-// * 通用下载请求
|
|
|
|
-// *
|
|
|
|
-// * @param fileName 文件名称
|
|
|
|
-// * @param delete 是否删除
|
|
|
|
-// */
|
|
|
|
-// @GetMapping("common/download")
|
|
|
|
-// @ApiOperation("通用下载请求")
|
|
|
|
-// public void fileDownload(
|
|
|
|
-// @ApiParam(name = "fileName", value = "文件名称")
|
|
|
|
-// @RequestParam(name = "fileName", required = true) String fileName,
|
|
|
|
-// @ApiParam(name = "delete", value = "是否删除")
|
|
|
|
-// @RequestParam(name = "delete", required = true) Boolean delete,
|
|
|
|
-// HttpServletResponse response,
|
|
|
|
-// HttpServletRequest request)
|
|
|
|
-// {
|
|
|
|
-// try
|
|
|
|
-// {
|
|
|
|
-// if (!FileUtils.isValidFilename(fileName))
|
|
|
|
-// {
|
|
|
|
-// throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
|
|
|
|
-// }
|
|
|
|
-// String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
|
|
|
|
-// String filePath = MiaxisConfig.getDownloadPath() + fileName;
|
|
|
|
-//
|
|
|
|
-// response.setCharacterEncoding("utf-8");
|
|
|
|
-// response.setContentType("multipart/form-data");
|
|
|
|
-// response.setHeader("Content-Disposition",
|
|
|
|
-// "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
|
|
|
|
-// FileUtils.writeBytes(filePath, response.getOutputStream());
|
|
|
|
-// if (delete)
|
|
|
|
-// {
|
|
|
|
-// FileUtils.deleteFile(filePath);
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// catch (Exception e)
|
|
|
|
-// {
|
|
|
|
-// log.error("下载文件失败", e);
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// /**
|
|
|
|
-// * 通用上传请求
|
|
|
|
-// */
|
|
|
|
-// @PostMapping("/common/upload")
|
|
|
|
-// @ApiOperation("通用上传请求")
|
|
|
|
-// public Response<UploadFileDTO> uploadFile(MultipartFile file) throws Exception
|
|
|
|
-// {
|
|
|
|
-// try
|
|
|
|
-// {
|
|
|
|
-// // 上传文件路径
|
|
|
|
-// String filePath = MiaxisConfig.getUploadPath();
|
|
|
|
-// // 上传并返回新文件名称
|
|
|
|
-// String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
|
-// String url = serverConfig.getUrl() + fileName;
|
|
|
|
-// UploadFileDTO uploadFileDTO = new UploadFileDTO();
|
|
|
|
-// uploadFileDTO.setFileName(fileName);
|
|
|
|
-// uploadFileDTO.setUrl(url);
|
|
|
|
-//
|
|
|
|
-// return Response.success(uploadFileDTO);
|
|
|
|
-// }
|
|
|
|
-// catch (Exception e)
|
|
|
|
-// {
|
|
|
|
-// return Response.error().setMsg(e.getMessage());
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 本地资源通用下载
|
|
* 本地资源通用下载
|
|
*/
|
|
*/
|