文件上传接口实现
This commit is contained in:
parent
6725f7bf32
commit
2352814466
|
|
@ -2,11 +2,16 @@ package com.czg.controller;
|
|||
|
||||
import com.czg.account.service.CommonService;
|
||||
import com.czg.resp.CzgResult;
|
||||
import com.czg.service.account.util.AliOssUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 公共接口
|
||||
|
|
@ -17,6 +22,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
public class CommonController {
|
||||
@Resource
|
||||
private CommonService commonService;
|
||||
@Resource
|
||||
private AliOssUtil aliOssUtil;
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
|
|
@ -27,4 +34,14 @@ public class CommonController {
|
|||
public CzgResult<Boolean> sendSms(@RequestParam String type) {
|
||||
return CzgResult.success(commonService.sendSms(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param file 文件对象
|
||||
* @return 文件地址
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
public CzgResult<String> upload(@RequestParam MultipartFile file) throws Exception {
|
||||
return CzgResult.success(aliOssUtil.uploadSuffix(file.getBytes(), FilenameUtils.getExtension(file.getOriginalFilename())));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@
|
|||
<dubbo.version>3.3.3</dubbo.version>
|
||||
<alipay-sdk-java.version>4.40.54.ALL</alipay-sdk-java.version>
|
||||
<aliyun-sms.version>2.0.24</aliyun-sms.version>
|
||||
<aliyun.oss.version>2.8.3</aliyun.oss.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
@ -44,6 +45,13 @@
|
|||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 阿里云oss -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>${aliyun.oss.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 阿里云sms -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
|
|
|
|||
|
|
@ -38,6 +38,11 @@
|
|||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -6,10 +6,8 @@ import com.czg.account.dto.staff.ShopStaffAddDTO;
|
|||
import com.czg.account.dto.staff.ShopStaffEditDTO;
|
||||
import com.czg.account.dto.staff.ShopStaffRemoveDTO;
|
||||
import com.czg.account.entity.ShopStaff;
|
||||
import com.czg.account.entity.SysRole;
|
||||
import com.czg.account.entity.SysUser;
|
||||
import com.czg.account.service.ShopStaffService;
|
||||
import com.czg.account.service.SysRoleService;
|
||||
import com.czg.account.service.SysUserService;
|
||||
import com.czg.account.service.SysUsersRolesService;
|
||||
import com.czg.exception.ApiNotPrintException;
|
||||
|
|
@ -33,8 +31,6 @@ public class ShopStaffServiceImpl extends ServiceImpl<ShopStaffMapper, ShopStaff
|
|||
@Resource
|
||||
private SysUserService sysUserService;
|
||||
@Resource
|
||||
private SysRoleService sysRoleService;
|
||||
@Resource
|
||||
private SysUsersRolesService sysUsersRolesService;
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
/**
|
||||
* Copyright (c) 2018 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.czg.service.account.util;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 阿里云存储
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@Component
|
||||
public class AliOssUtil{
|
||||
|
||||
private final String endPoint = "oss-cn-beijing.aliyuncs.com";
|
||||
private final String accessKey = "LTAI5tPdEfYSZcqHbjCrtPRD";
|
||||
private final String secretKey = "DZjyHBq3nTujF0NMLxnZgsecU8ZCvy";
|
||||
private final String bucketName = "cashier-oss";
|
||||
private final String url = "cashier-oss.oss-cn-beijing.aliyuncs.com";
|
||||
private final String prefix = "upload";
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
* @param prefix 前缀
|
||||
* @param suffix 后缀
|
||||
* @return 返回上传路径
|
||||
*/
|
||||
public String getPath(String prefix, String suffix) {
|
||||
//生成uuid
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
//文件路径
|
||||
String path = DateUtil.date().getDay() + "/" + uuid;
|
||||
|
||||
if(StrUtil.isNotBlank(prefix)){
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
|
||||
return path + "." + suffix;
|
||||
}
|
||||
|
||||
public String upload(InputStream inputStream, String path) throws Exception {
|
||||
OSSClient client = new OSSClient(endPoint, accessKey, secretKey);
|
||||
try {
|
||||
client.putObject(bucketName, path, inputStream);
|
||||
client.shutdown();
|
||||
} catch (Exception e){
|
||||
throw new Exception("上传异常");
|
||||
}
|
||||
|
||||
return "https://" + url + "/" + path;
|
||||
}
|
||||
|
||||
public String uploadSuffix(byte[] data, String suffix) throws Exception {
|
||||
return upload(new ByteArrayInputStream(data), getPath(prefix, suffix));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -7,8 +7,8 @@
|
|||
<select id="selectByUserId" resultType="com.czg.account.entity.SysMenu">
|
||||
select c.*
|
||||
from sys_users_roles as a
|
||||
left join sys_roles_menus as b on a.role_id = b.role_id
|
||||
left join sys_menu as c on c.menu_id = b.menu_id
|
||||
left join sys_roles_menus as b on a.role_id = b.role_id
|
||||
left join sys_menu as c on c.menu_id = b.menu_id
|
||||
where a.user_id = #{userId}
|
||||
<if test="type != null">
|
||||
and c.type=#{type}
|
||||
|
|
|
|||
Loading…
Reference in New Issue