目录
一.根据id 更新品牌:
1)发送请求:(http://127.0.0.1:88/api/product/brand/update)
2)Controller:
/**
* 修改 品牌
*/
@RequestMapping(value = "/update", method = RequestMethod.POST)
public R update(@RequestBody BrandEntity brand) {
brandService.updateById(brand);
return R.ok();
}
3)Service:
/**
* 根据 ID 选择修改
*
* @param entity 实体对象
*/
default boolean updateById(T entity) {
return SqlHelper.retBool(getBaseMapper().updateById(entity));
}
二.文件上传:
1)云存储开通与访问:
a:文件存储方式:
b:文件上传方式:
-1.浏览器 上传给 应用服务器,应用服务器 上传给 服务器。---> 应用服务器压力增大
-2.服务端签名后直传,浏览器向应用服务器获取令牌签名,获取后浏览器直接传给 文件服务器。
2)开通 OSS ,测试简单文件上传:
3)云存储开通与访问:
a:新建 第三方 服务,写 yml 配置文件:
spring:
cloud:
nacos:
discovery:
server-addr: 114.215.173.88:8848
# alicloud:
# access-key: LTAI4G4sU26BpNUEUJ9QS1VG
# secret-key: UzhTHz3yxee2Pu5Vu6kVlaVCw3gnOS
# oss:
# endpoint: oss-cn-beijing.aliyuncs.com
# bucket: gulimail-zhanglaoshi
application:
name: gulimail-third-party
server:
port: 30000
b:继承 公共文件后,去除 mysql,否则启动报错:
<dependency>
<groupId>com.guigu.gulimail</groupId>
<artifactId>gulimail-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
c:将 oss 配置,写入到 配置文件中:
d:编写 Controller 类,返回 前端上传 所需要的信息:
/**
* @author Zhang Xudong
* @date 2021/3/9 10:02 下午
*/
@RestController
@RequestMapping(value = "/oss")
public class OssController {
@Value("${spring.cloud.alicloud.oss.endpoint}")
private String endpoint;
@Value("${spring.cloud.alicloud.oss.bucket}")
private String bucket;
@Value("${spring.cloud.alicloud.access-key}")
private String accessId;
@Autowired
private OSS ossClient;
@RequestMapping(value = "/policy")
public Map policy() {
// host的格式为 bucketname.endpoint
String host = "https://" + bucket + "." + endpoint;
// callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
// String callbackUrl = "http://88.88.88.88:8888";
String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
// 用户上传文件时指定的前缀。
String dir = "format";
Map<String, String> respMap = null;
try {
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
// PostObject请求最大可支持的文件大小为5 GB,即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = ossClient.calculatePostSignature(postPolicy);
respMap = new LinkedHashMap<String, String>();
respMap.put("accessid", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
// respMap.put("expire", formatISO8601Date(expiration));
} catch (Exception e) {
// Assert.fail(e.getMessage());
System.out.println(e.getMessage());
} finally {
ossClient.shutdown();
}
return respMap;
}
}