文件上传现在七牛云还是每月10GB免费流量,我们就最简单的实现七牛云上传文件。
1、首先注册七牛云,在个人信息,密钥管理中获取AccessKey 和SecretKey,
2、创建Buckets,产品主页、对象存储,新建存储空间
3、然后点击绑定域名,填写你的私有域名,如果不添加就要通过api获取文件,操作起来有点麻烦,绑定个域名,设置DNS解析Cname 指向七牛分配给你的域名就可以了,https是收费的,如果通信协议不选择https接入,而你的后台管理页面又开启了https,会提示网站中有不安全内容,可以忽略,也可以nginx代理,自己百度下处理吧,
4、言归正传,我们需要设置几个重要的参数,引入依赖,在applocation.yml中blade下添加七牛云相关配置参数,:
com.qiniu
qiniu-java-sdk
[7.2.0, 7.2.99]
blade: secure: url: exclude-patterns: - /blade-test/** client: - client-id: sword path-patterns: - /blade-sword/** - client-id: saber path-patterns: - /blade-saber/** tenant: column: tenant_code tables: - blade_notice qiniu: domain: http://oss.blade.vip prefix: blade #可以理解为存储桶上的文件夹 accesskey: 4i_zJNVB93TRiGhUlI9qPBfFQz2sG secretkey: ii0lB8Dfwv9syhg4V9FjJBQmT8htL bucketname: blade #存储桶名称
5、config文件,common包下的congtig添加 OssConfig类,代码如下:
package org.springblade.common.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; /** * 七牛OSS配置文件 * @author S.L */ @Data @Configuration @ConfigurationProperties(prefix="blade.qiniu") public class OssConfig { /** * 七牛外链域名 */ private String domain; /** * 路径前缀 */ private String prefix; /** * ACCESS_KEY */ private String accessKey; /** * SECRET_KEY */ private String secretKey; /** * 存储空间名 */ private String bucketName; }
6、system包下添加controller,OssController
package org.springblade.modules.system.controller; import com.qiniu.common.Zone; import com.qiniu.http.Response; import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springblade.common.config.OssConfig; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.Func; import org.springframework.beans.factory.annotation.Autowired; 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; /** * 文件上传 * * @author S.L */ @RestController @RequestMapping("/blade-system/oss") @Api(tags="文件上传") public class OssController { private UploadManager uploadManager; private String token; @Autowired OssConfig ossConfig; @PostMapping("upload") @ApiOperation(value = "上传文件") public R upload(@RequestParam("file") MultipartFile file) throws Exception { uploadManager = new UploadManager(new Configuration(Zone.autoZone())); token = Auth.create(ossConfig.getAccessKey(),ossConfig.getSecretKey()). uploadToken(ossConfig.getBucketName()); /** * 生成上传文件名 */ String path = ossConfig.getPrefix() + "/" + Func.randomUUID() + file.getOriginalFilename(); System.out.println(path); Response res = uploadManager.put(file.getBytes(), path, token); String fileUrl = ossConfig.getDomain() + "/" + path; return R.data(fileUrl); } }
7、为了上传方便,暂时放行文件上传api接口
package org.springblade.common.config; import org.springblade.core.secure.registry.SecureRegistry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * Blade配置 * * @author Chill */ @Configuration public class BladeConfiguration implements WebMvcConfigurer { @Bean public SecureRegistry secureRegistry() { SecureRegistry secureRegistry = new SecureRegistry(); secureRegistry.excludePathPatterns("/blade-auth/**"); secureRegistry.excludePathPatterns("/blade-system/oss/**"); secureRegistry.excludePathPatterns("/blade-log/**"); secureRegistry.excludePathPatterns("/blade-system/menu/auth-routes"); secureRegistry.excludePathPatterns("/doc.html"); secureRegistry.excludePathPatterns("/js/**"); secureRegistry.excludePathPatterns("/webjars/**"); secureRegistry.excludePathPatterns("/swagger-resources/**"); return secureRegistry; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/"); registry.addResourceHandler("doc.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
8、application启动类上添加注解:
@EnableConfigurationProperties(OssConfig.class)
到这里就可以上传了,有没有觉得太简单?截个上传成功的控制台日志吧
最后,saber前端也是比较省事的,userinfo里面有个avatar上传的demo,api接口一换就可以测试下了,
画叉那个位置 是我封装了后端的返回格式,按照前面的代码只要把原来的data.0,修改成data就可以了。
这个sns的富文本编辑简直。。。。。
扫一扫访问 Blade技术社区 移动端