七牛OSS图床使用
项目中用到了图片服务器 这次选择的是七牛 每个月10G免费流量 还挺好用 记录一下怎么上传文件的
申请七牛对象存储
最后是这个样
上传图片
直接在网站按步骤上传就可以 然后点击外链就能用了
那么在Java中怎么上传和使用呢
1.引入jar包
在pom.xml中引入jar包,使用Maven,jar包版本在7.4.0~7.4.99之间的,比如:7.4.0取不到就区7.4.1依次往后
<!--七牛上传图片--> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>[7.4.0, 7.4.99]</version> </dependency>
2.添加配置文件
在application.yml配置
# 七牛云 配置 qiniuyun: #你的accessKey accessKey: qBWiwDjwcgyBl*********** #你的secretKey secretKey: 0o1unTBffzLm************ #你的空间名 bucket: dandelion-pgy #刚刚绑定的域名 domain: http://rhwamrwoo.hb-bkt.clouddn.com/
密钥在这里找
3.创建上传文件的工具类
放在Utils包下即可
import java.io.InputStream; import java.util.UUID; import com.google.gson.Gson; import com.qiniu.common.QiniuException; import com.qiniu.http.Response; import com.qiniu.storage.Region; import com.qiniu.storage.UploadManager; import com.qiniu.storage.model.DefaultPutRet; import com.qiniu.util.Auth; import com.qiniu.storage.Configuration; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * @description: 七牛云上传图片 **/ @Component @PropertySource(value = "classpath:application.yml") @ConfigurationProperties(prefix = "qiniuyun") public class QiniuUtil { // 设置需要操作的账号的AK和SK @Value("${accessKey}") private String accessKey; @Value("${secretKey}") private String secretKey; // 要上传的空间 @Value("${bucket}") private String bucket; //外链地址 @Value("${domain}") private String domain; /** * 上传文件并且返回文件地址 * * @param inputStream 文件 */ public String setUploadManager(InputStream inputStream) { //设置密钥、文件连接、文件名等等属性 //构造一个带指定 Region 对象的配置类 Configuration cfg = new Configuration(Region.huabei()); //服务器选的华北 所以是huabei() //...其他参数参考类注释 UploadManager uploadManager = new UploadManager(cfg); //设置连接地址 Auth auth = Auth.create(accessKey, secretKey); String prefix = ""; int Guid = 100; try { String s = auth.uploadToken(bucket); //生成文件名 String s1 = UUID.randomUUID().toString().replaceAll("-",""); //去掉uuid中的 - //实现文件上传 Response response = uploadManager.put(inputStream, s1, s, null, null); //解析上传成功结果 DefaultPutRet defaultPutRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); //返回文件外链地址 return domain + defaultPutRet.key; } catch (QiniuException e) { e.printStackTrace(); return null; } } }
4.Service层代码
public String uploadHeadImg(Long userId, MultipartFile file) { //文件名 try { String path = qiniuUtil.setUploadManager(file.getInputStream()); if (path == null) { throw new Exception("输入的字符不能为空!"); } System.out.println(path); /* * 这段代码可根据业务逻辑修改 */ DanUserinfo userinfo = danUserinfoMapper.selectDanUserinfoById(userId); userinfo.setHeadImgUrl(path); danUserinfoMapper.updateDanUserinfo(userinfo); return path; } catch (IOException e) { e.printStackTrace(); return "文件上传失败"; } catch (Exception e) { e.printStackTrace(); return "文件上传失败"; }
其他的就根据自己的业务需求修改即可
参考 https://blog.csdn.net/m0_51079637/article/details/121018951
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)