我是靠谱客的博主 开放钢笔,最近开发中收集的这篇文章主要介绍springboot集成oss上传头像themleaf模板1、前端页面themleaf后端springboot,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1、前端页面themleaf
<form class="ui large form" method="post" action="#" th:action="@{/admin/register}" enctype="multipart/form-data">
<div class="ui segment">
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="text" name="username" placeholder="用户名">
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="lock icon"></i>
<input type="password" name="password" placeholder="密码">
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="text" name="email" placeholder="邮箱">
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="text" name="nickname" placeholder="真实姓名">
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="file" name="file" accept="image/jpeg" placeholder="上传头像">
</div>
</div>
<button class="ui fluid large teal submit button">注 册</button>
</div>
<div class="ui error mini message"></div>
<div class="ui mini negative message" th:unless="${#strings.isEmpty(msg)}" th:text="${msg}">用户名和密码错误</div>
</form>
运行效果
后端springboot
oss依赖
<!--阿里云对象存储oss-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
controller层代码
public String zhuce(@RequestParam("file") MultipartFile file, User user, RedirectAttributes attributes) throws IOException {
if (file.isEmpty()){
attributes.addFlashAttribute("msg","头像为空!!!");
return "redirect:/admin/register";
}
// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
String endpoint = "oss-cn-shanghai.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "LJHLTAI5tRHjXo6cYh9XyfPcWwF";
String accessKeySecret = "554VzAx9ccw5BwFunu9CIByVAAJBHCMdJ";
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String backetName="ljhyyds";
//如果桶不存在,动态创建桶
if(!ossClient.doesBucketExist(backetName)){
//创建bucket
ossClient.createBucket(backetName);
//设置oss实例的访问权限,公共读
ossClient.setBucketAcl(backetName, CannedAccessControlList.PublicRead);
}
InputStream inputStream = file.getInputStream();
String originalFilename = file.getOriginalFilename();
String imgSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFilename= UUID.randomUUID().toString()+imgSuffix;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String datePath = dateFormat.format(new Date());
String fileUrl=datePath+"/"+newFilename;
PutObjectRequest putObjectRequest = new PutObjectRequest(backetName, fileUrl, inputStream);
ossClient.putObject(putObjectRequest);
ossClient.shutdown();
System.out.println("=========================>图片地址:"+"https://"+backetName+"."+endpoint+"/"+fileUrl);
user.setAvatar("https://"+backetName+"."+endpoint+"/"+fileUrl);
user.setCreateTime(new Date());
User user1 = loginRepository.save(user);
attributes.addFlashAttribute("message","注册成功!!!");
return "redirect:/admin";
}
最后
以上就是开放钢笔为你收集整理的springboot集成oss上传头像themleaf模板1、前端页面themleaf后端springboot的全部内容,希望文章能够帮你解决springboot集成oss上传头像themleaf模板1、前端页面themleaf后端springboot所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复