2021-12-09 spring boot 集成 华为云obs
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-12-09 spring boot 集成 华为云obs相关的知识,希望对你有一定的参考价值。
参考技术A 首先作为一个00后的程序员小白,集成第三方的东西真的是挺费劲的,百度之后写完发现全是坑。无奈自己用比较笨的方法写出来一套。奉献给各位新入职的程序员小白,如果项目要求不高可以使用。第一步:引入依赖 集成sdk
第二步 : 配置yaml文件
第三步 : 写配置类
/**
* OBS桶名
*/
@Value("$obs.bucketname")
private String bucketname;
/**
* 上传至OBS桶的存储对象名
*/
//@Value("$obs.config.objectname")
private String objectname;
/**
* 连接OBS的endPoint
*/
@Value("$obs.endPoint")
private String endPoint;
/**
* AK
*/
@Value("$obs.ak")
private String ak;
/**
* SK
*/
@Value("$obs.sk")
private String sk;
/** 图片位置 */
private String picLocation;
/** http或https 域名 */
@Value("$obs.domain-name")
private String domainName;
public static String BUCKET_NAME;
public static String END_POINT;
public static String AK;
public static String SK;
public static String DOMAINNAME;
@Override
public void afterPropertiesSet()throws Exception
BUCKET_NAME =this.bucketname;
END_POINT =this.endPoint;
AK =this.ak;
SK =this.sk;
DOMAINNAME =this.domainName;
最后写一个工具了类随意调用
public class ObsUtil
/**
* 文件上传
* @param file 待上传的文件
* @param directory 指定存储的文件夹
* @return
*/
public static String uploadIdentity(MultipartFile file,String directory)
if (file ==null) return"文件不可以为空!";
String url ="";
String endPoint =ObsConfig.END_POINT;
String ak =ObsConfig.AK;
String sk =ObsConfig.SK;
String bucketname =ObsConfig.BUCKET_NAME;
String domainName =ObsConfig.DOMAINNAME;
// 创建ObsClient实例
ObsClient obsClient =new ObsClient(ak,sk,endPoint);
String str = file.getOriginalFilename();
String name =str.substring(str.lastIndexOf(".")+1);
SnowflakeIdWorker idWorker =new SnowflakeIdWorker(0,0);
long id =idWorker.nextId();
String name1 =id +"."+name;
String objectName =directory +name1;
try
// 待上传的本地文件路径,需要指定到具体的文件名
obsClient.putObject(bucketname,objectName, file.getInputStream());
url ="https://"+domainName +"/" +objectName;
catch (IOException e)
e.printStackTrace();
return url;
/**
*
* @param path
* @param directory
* @return
*/
public static String upload(String path,String directory)
if (path ==null) return"文件不可以为空!";
String url ="";
String endPoint =ObsConfig.END_POINT;
String ak =ObsConfig.AK;
String sk =ObsConfig.SK;
String bucketname =ObsConfig.BUCKET_NAME;
// 创建ObsClient实例
ObsClient obsClient =new ObsClient(ak,sk,endPoint);
path = path.trim();
String objectName = directory+
path.substring(path.lastIndexOf("/")+1);
FileInputStream fis =null;
String url1 ="";
try
fis =new FileInputStream(new File(path));
obsClient.putObject(ObsConfig.BUCKET_NAME,objectName, fis);
url1 =endPoint +"/" +objectName;
catch (FileNotFoundException e)
e.printStackTrace();
return url1;
spring boot 集成 mybatis
spring boot 集成 mybatis 需要加入pom依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
数据库连接驱动和mybatis的starter
因为springboot建议不使用xml配置文件的导入方式来配置,所以将集成mybatis的配置全部设置在了application.properties
如图:
###spring datasource###
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/sms?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=zhenghuasheng
spring.datasource.password=zhs2111014
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=10
spring.datasource.max-wait=10000
spring.datasource.max-idle=5
spring.datasource.min-idle=0
###mybatis config###
mybatis.mapper-locations=classpath:/mybatis/*Mapper.xml
mybatis.type-aliases-package=com.sample.model
###spring mvc 静态文件指定####
spring.mvc.static-path-pattern=/**
spring.resources.cache-period=864000
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
相应的mapper的xml文件和mapper接口正常定义
spring boot入口类添加注解扫描mapper接口,如下所示:
@SpringBootApplication
@MapperScan("com.sample.dao")
public class SampleMybatisApplication
private static Logger logger = Logger.getLogger(SampleMybatisApplication.class);
/**
* Main Start
*/
public static void main(String[] args)
SpringApplication.run(SampleMybatisApplication.class, args);
logger.info("============= SpringBoot Start Success =============");
集成完成
以上是关于2021-12-09 spring boot 集成 华为云obs的主要内容,如果未能解决你的问题,请参考以下文章