Article directory
File upload using Qiniu cloud object storage
1. Why not save the file to the application server?
Because the file is stored in the application server, the file will be larger than ordinary JSON data. Each time the file is read, the file must be read from the application server, which will consume more time and bandwidth, and the response time of the application server will increase. In the actual development process, we will deploy the files separately.
2. Basic use of Qiniu Cloud
Address: https://www.qiniu.com/
requires authentication.
After authentication, enter the console -> Object Storage -> Space Management
to create a new space
remember your space name
Go to key management and copy your AK and SK
3. Configuration file
maven dependencies
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.7.0, 7.7.99]</version>
</dependency>
yml
fill in what you just copied here
Fourth, the server
Note: files cannot be serialized, so log printing cannot be done! ! ! (The blogger annotated the log printout)
@PostMapping("/applyRoleFile")
@PreAuthorize("hasAuthority('normal')")
//@SystemLog(businessName = "申请角色")
public ResponseResult applyRoleFile(@RequestBody MultipartFile file) {
return userService.applyRoleFile(file);
}
@Data
@Service
@ConfigurationProperties(prefix = "oss")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
//上传凭证
private String accessKey;
private String secretKey;
private String bucket;
@Override
public ResponseResult applyRoleFile(MultipartFile file) {
//判断文件类型
//获取原始文件名
String originalFilename = file.getOriginalFilename();
//对原始文件名进行判断.
// 只要pdf文件
if (!originalFilename.endsWith(".pdf")) {
throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
}
//生成默认文件名
String filePath = PathUtils.generateFilePath(originalFilename);
//判断通过上传文件到OSS
String url = uploadOSS(file, filePath);
/**
这里是将地址url更新到数据库 这里就不展示了 主要是前面的那一块
*/
return ResponseResult.okResult();
}
private String uploadOSS(MultipartFile file, String filePath) {
//构造一个带指定 Region 对象的配置类
// 即你的空间是哪个地区,这里使用自动帮你填写
Configuration cfg = new Configuration(Region.autoRegion());
UploadManager uploadManager = new UploadManager(cfg);
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = filePath;
try {
InputStream inputStream = file.getInputStream();
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(inputStream, key, upToken, null, null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//这个是上传到七牛云服务器的路径定义
return "http://r9yeel75m.hn-bkt.clouddn.com/" + key;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
5. Test
Test with postman