阿里云OSS实现头像上传

编程入门 行业动态 更新时间:2024-10-24 12:25:11

<a href=https://www.elefans.com/category/jswz/34/1770131.html style=阿里云OSS实现头像上传"/>

阿里云OSS实现头像上传

目录结构:

application.yml

server:port: 8002
spring:application:name: service-ossprofiles:active: devaliyun:oss:file:endpoint: oss-cn-beijing.aliyuncskeyid: LTAI4G8q7nBEsUb1NxXCkeysecret: 3FEJ5vh5EttqiiLdviC1dVjuvU#bucket可以在控制台创建也可以在代码中创建bucketname: edu-guli-study

ConstantPropertiesUtils:(用于注入配置文件中的阿里云配置信息)

@Component //实现InitializingBean接口可以执行执行赋值后的一些逻辑(由于属性是private的)
public class ConstantPropertiesUtils implements InitializingBean {@Value("${aliyun.oss.file.endpoint}")private String endpoint;@Value("${aliyun.oss.file.keyid}")private String keyId;@Value("${aliyun.oss.file.keysecret}")private String keySecret;@Value("${aliyun.oss.file.bucketname}")private String bucketName;/*定义公有静态常量*/public static String ENDPOINT;public static String KEYID;public static String KEYSECRET;public static String BUCKETNAME;@Overridepublic void afterPropertiesSet() throws Exception {ENDPOINT = endpoint;KEYID = keyId;KEYSECRET = keySecret;BUCKETNAME = bucketName;}
}

OssApplication:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {"com.coderkun"}) //为了swagger扫描到该包下的Controller
public class OssApplication {public static void main(String[] args) {SpringApplication.run(OssApplication.class,args);}
}

OSSController:

@RestController
@Api(description = "oss文件上传")
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class OssController {@Autowiredprivate OssService ossService;//上传头像方法@PostMappingpublic R uploadOssFile(MultipartFile file){String url = ossService.uploadOssFileAvatar(file);return R.ok().data("url",url);}
}

OssService:

public interface OssService {//上传头像到ossString uploadOssFileAvatar(MultipartFile file);
}

OssServiceImpl:

@Service
public class OssServiceImpl implements OssService {@Overridepublic String uploadOssFileAvatar(MultipartFile file) {// Endpoint以杭州为例,其它Region请按实际情况填写。String endpoint = ConstantPropertiesUtils.ENDPOINT;// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录  创建。String accessKeyId = ConstantPropertiesUtils.KEYID;String accessKeySecret = ConstantPropertiesUtils.KEYSECRET;String bucketName = ConstantPropertiesUtils.BUCKETNAME;try {// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 上传文件流。InputStream inputStream = file.getInputStream();//获取文件名称String fileName = file.getOriginalFilename();//1.由于文件名重复会覆盖,生成随机文件名String uuid = UUID.randomUUID().toString().replaceAll("-","");fileName = uuid + fileName;//2.把文件按照日期分类String datePath = new DateTime().toString("yyyy/MM/dd");fileName = datePath + "/" + fileName; //2020/6/4/dafdf.jpg//第二个参数:上传到oss的文件路径和文件名称  /aa/bb1.jpgossClient.putObject(bucketName,fileName, inputStream);// 关闭OSSClient。ossClient.shutdown();//把上传之后oss返回的文件url返回()//url格式:/%25U%7EHW%2502P2OH6FXR%29%5B8%60T2A.pngString url = "https://"+bucketName+"."+endpoint+"/"+fileName;return url;} catch (IOException e) {e.printStackTrace();return null;}}
}

欢迎关注公众号Java技术大本营,会不定期分享BAT面试资料等福利。


更多推荐

阿里云OSS实现头像上传

本文发布于:2024-03-23 02:06:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阿里   头像   上传   OSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!