1. 业务开发
默认集成了七牛云存储。
1.1. 扩展第三方云存储
默认集成第三方接口步骤:
1、新建对应云存储类,并继承IUploadBaseService
类,重写upload
方法,自定义一个 beanName
名称
2、 配置文件上传类型,打开菜单 自定义->自定义字典->文件上传类型
添加字典,标签名为 '存储类型' ,值为存储类的beanName,如:七牛云
uploadQIniuServier
3、 修改存储配置,打开菜单 文件管理->存储设置
下拉选择存储方式
;
1.2. 七牛云范例
1.2.1. 创建存储类 UploadQIniuServierImpl
/**
* 七牛云的上传实现类
*/
@Service("uploadQIniuServier")
public class UploadQIniuServierImpl extends IUploadBaseService {
@Override
public ResultData upload(String uploadPath, MultipartFile file, boolean rename) {
// 过滤掉的文件类型
String[] errorType = ConfigUtil.getString(Const.CONFIG_UPLOAD, "uploadDenied", "exe,jsp").split(",");
//文件上传类型限制
String fileName = file.getOriginalFilename();
if (StringUtils.isBlank(fileName)) {
return ResultData.build().error("文件名不能为空!");
}
if(fileName.lastIndexOf(".") < 0){
return ResultData.build().error(getResString("err.error", getResString("file.name")));
}
String fileType=fileName.substring(fileName.lastIndexOf("."));
//修改文件名
if(!rename){
//Windows 系统下文件名最后会去掉. 这种文件默认拒绝 xxx.jsp. => xxx.jsp
if(System.getProperty("os.name").startsWith("Windows")){
if(fileName.endsWith(".")) {
return ResultData.build().error(getResString("err.error", getResString("file.type")));
}
fileName = FileNameUtil.cleanInvalid(fileName);
}
fileType=fileName.substring(fileName.lastIndexOf("."));
}else {
//取随机名
fileName=System.currentTimeMillis()+fileType;
}
for (String type : errorType) {
if((fileType).equalsIgnoreCase(type)){
return ResultData.build().error(getResString("err.error", getResString("file.type")));
}
}
/**
* 获取七牛云配置
*/
Map<String,String> map = ConfigUtil.getMap("七牛云配置");
String qiniuAk = map.get("qiniuAk");
String qiniuSk = map.get("qiniuSk");
String qiniuDomain = map.get("qiniuDomain");
String bucketName = map.get("bucketName");
if(!qiniuDomain.endsWith("/")){
qiniuDomain += "/";
}
/**
* 七牛云上传逻辑
*/
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.autoRegion());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = qiniuAk;
String secretKey = qiniuSk;
String bucket = bucketName;
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = fileName;
String filePath = null;
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);
filePath = qiniuDomain + putRet.key;
System.out.println("完整文件路径:\t"+ filePath);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
}catch (IOException e){
System.err.println(e.toString());
}
return ResultData.build().success(filePath);
}
}
[!tip] 代码中的七牛云配置信息通过自定义配置实现
beanName
名称为 uploadQIniuServier
1.2.2. 配置文件上传类型 字典
1.2.3. 存储设置
[!tip] 通过自定义配置实现