Java 七牛云存储与下载
七牛云的文件上传和下载(私有空间)
1、本篇博客参考网址
https://blog.csdn.net/peaceful000/article/details/53171578
https://blog.csdn.net/albertfly/article/details/51499812
2、在pom.xml中添加需要的jar
<!--七牛云上传图片服务--> <!-- https://mvnrepository.com/artifact/com.qiniu/qiniu-java-sdk --> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>7.0.8</version> </dependency>
3、详细代码
package com.qiniu.upload; import com.qiniu.common.QiniuException; import com.qiniu.http.Response; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import com.qiniu.util.StringMap; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.ResponseBody; import java.io.*; public class QiniuUtil { //设置好账号的ACCESS_KEY和SECRET_KEY String ACCESS_KEY = ""; //这两个登录七牛 账号里面可以找到 String SECRET_KEY = ""; //要上传的空间 String bucketname = ""; //对应要上传到七牛上 你的那个路径(自己建文件夹 注意设置私有) //上传到七牛后保存的文件名 //密钥配置 Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); //创建上传对象 UploadManager uploadManager = new UploadManager(); /** * 简单上传,使用默认策略,只需要设置上传的空间名就可以了 * * @param fileName 文件上传至七牛云空间的名称 * @return */ public String getUpToken(String fileName) { //return auth.uploadToken(bucketname); //<bucket>:<key>,表示只允许用户上传指定key的文件。在这种格式下文件默认允许“修改”,已存在同名资源则会被本次覆盖。 //如果希望只能上传指定key的文件,并且不允许修改,那么可以将下面的 insertOnly 属性值设为 1。 //第三个参数是token的过期时间 return auth.uploadToken(bucketname, fileName, 3600, new StringMap().put("insertOnly", 0)); } /** * 普通上传 * * @param filePath 文件路径 * @param fileName 文件上传至七牛云空间的名称 * @throws IOException */ public void upload(String filePath, String fileName) { try { //调用put方法上传 Response res = uploadManager.put(filePath, fileName, getUpToken(fileName)); //打印返回的信息 System.out.println(res.bodyString()); } catch (QiniuException e) { Response r = e.response; // 请求失败时打印的异常的信息 System.out.println(r.toString()); try { //响应的文本信息 System.out.println(r.bodyString()); } catch (QiniuException e1) { //ignore e.printStackTrace(); } } } /** * 获取下载文件路径,即:donwloadUrl * * @return */ public String getDownloadUrl(String targetUrl) { String downloadUrl = auth.privateDownloadUrl(targetUrl); return downloadUrl; } /** * 文件下载 * * @param targetUrl */ public void download(String targetUrl) { //获取downloadUrl String downloadUrl = getDownloadUrl(targetUrl); //本地保存路径 String filePath = "E:\\chen\\"; download(downloadUrl, filePath); } /** * 通过发送http get 请求获取文件资源 * * @param url * @param filepath * @return */ private static void download(String url, String filepath) { OkHttpClient client = new OkHttpClient(); System.out.println(url); Request req = new Request.Builder().url(url).build(); com.squareup.okhttp.Response resp = null; try { resp = client.newCall(req).execute(); System.out.println(resp.isSuccessful()); if (resp.isSuccessful()) { ResponseBody body = resp.body(); InputStream is = body.byteStream(); byte[] data = readInputStream(is); //判断文件夹是否存在,不存在则创建 File file = new File(filepath); if (!file.exists() && !file.isDirectory()) { System.out.println("===文件夹不存在===创建===="); file.mkdir(); } File imgFile = new File(filepath + "888.jpg"); FileOutputStream fops = new FileOutputStream(imgFile); fops.write(data); fops.close(); } } catch (IOException e) { e.printStackTrace(); System.out.println("Unexpected code " + resp); } } /** * 读取字节输入流内容 * * @param is * @return */ private static byte[] readInputStream(InputStream is) { ByteArrayOutputStream writer = new ByteArrayOutputStream(); byte[] buff = new byte[1024 * 2]; int len = 0; try { while ((len = is.read(buff)) != -1) { writer.write(buff, 0, len); } is.close(); } catch (IOException e) { e.printStackTrace(); } return writer.toByteArray(); } public static void main(String args[]) throws IOException { String fileName = "xiao.jpg"; //上传文件的路径 String FilePath = "C:\\Users\\ChenXiao\\Pictures\\Saved Pictures\\xiao.jpg"; //本地要上传文件路径 // new QiniuUtil().upload(FilePath,fileName); //构造私有空间的需要生成的下载的链接; //格式: http://私有空间绑定的域名/空间下的文件名 String targetUrl = "http://私有空间绑定的域名/下载文件名称.后缀" new QiniuUtil().download(targetUrl); } }
4、在七牛云中拿到ACCESS_KEY和SECRET_KEY值和私有空间绑定的域名如下图所示(注:在创建'私有空间'的时候我是创建在'华东地区'的,不知为何建在'华南地区'竟然上传失败!!!并未去深究缘由,有了解缘由的可在底下留言告知下,谢谢!!!)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了