//控制层
@Slf4j
@RestController
@RequestMapping("/upload")
@SuppressWarnings("all")
@Api(value = "", tags = {"图片视频上传接口"})
public class UploadController {
@Autowired
private UploadFileService uploadFileService;
@PostMapping("/video")
@ApiOperation(value = "图片。视频上传 type 1个人信息等图片 2商品信息等图片 3视频", notes = "图片。视频上传 type 1个人信息等图片 2商品信息等图片 3视频")
public JsonResult uploadVideo(@RequestParam("file") MultipartFile file, Integer type) {
if (type == null) {
return ResultTool.fail(ResultCode.PARAM_IS_BLANK);
}
Map<String, Object> map = uploadFileService.upLoadVideoFileService(file, type);
return ResultTool.success(map);
}
}
//service
/**
* 上传视频图片
* @param file
* @param type
*/
Map<String,Object> upLoadVideoFileService(MultipartFile file, Integer type);
//实现类
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UploadFileServiceImpl implements UploadFileService {
@Value("${system.baseUrl}")
private String baseUrl;
@Value("${aliyun.oss.accessKey}")
private String accessKey;
@Value("${aliyun.oss.accessKeySeret}")
private String accessKeySeret;
@Value("${aliyun.oss.bucket}")
private String bucket;
@Value("${aliyun.oss.endPoint}")
private String endPoint;
@Value("${aliyun.oss.domain}")
private String domain;
/**
* 上传视频图片
*
* @param file
* @param type
*/
@Override
public Map<String,Object> upLoadVideoFileService(MultipartFile file, Integer type) {
long startTime = System.currentTimeMillis();
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
String image = "info/";
if (type == 1) {
image = "auth/";
} else if (type == 2) {
image = "info/";
} else if (type == 3) {
image = "video/";
}
String newName = image + UUIDUtil.getUUID() + "." + suffix;
String url = "";
Map map = new HashMap();
try {
// Endpoint以杭州为例,其它Region请按实际情况填写。
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录
// 创建OSSClient实例。
OSSClient ossClient = new OSSClient(endPoint, accessKey, accessKeySeret);
// 上传文件流。
ossClient.putObject(bucket, newName, file.getInputStream());
url = domain + "/" + newName;
if (type == 3) {//视频
String fengMianUrl = url + "?x-oss-process=video/snapshot,t_10000,f_jpg,w_800,h_600,m_fast";
map.put("fengmianUrl", fengMianUrl);
}
map.put("url", url);
// 关闭OSSClient。
ossClient.shutdown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("*********上传耗时 ***********: " + (endTime - startTime));
return map;
}
}