七牛云如何上传图片
七牛云上传图片的方法:
1,注册七牛云账号,
2,创建一个存储空间bucket,创建的时候回送一个临时的测试域名,这个等上传工具类要用到 ,有效期30天,
3,写java工具类
public class upLoadFile { //...生成上传凭证,然后准备上传 这个是在注册的账户里面有个人中心那里的密钥管理 1 public static String accessKey = “h9r4Vz72dIAEL2PLSYXFEv39GmUOsw99y7wkbQQW”; public static String secretKey = “BWhiWbB7D-lWkI2bP2c6wBYRbak3MzmcUeoyTaNv”; // public static String bucket = “vehicle-picture”; //这个是创建的存储空间的名字 public static String bucket = “image-a”; //这个就是测试域名 创建的时候一般都会赠送一个测试域名 // public static String domainOfBucket = “http://qn.vwaiter.cn”; public static String domainOfBucket = “pn7fhqast.bkt.clouddn.com”; public static void main(String[] args) { getUrl(“FpMjfsJwwMDYC-IXjMIhWm9xiYt4”); } /** * 上传图片 * @param file * @return */ public static String uploadFile(MultipartFile file) { //构造一个带指定Zone对象的配置类 Configuration cfg = new Configuration(Zone.zone0()); //...其他参数参考类注释 UploadManager uploadManager = new UploadManager(cfg); //默认不指定key的情况下,以文件内容的hash值作为文件名 String fileName=null; String key = null; try { Logger.getLogger("start>>>>>>>>>>").info("图片上传"); byte[] uploadBytes=file.getBytes(); ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes); Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); Response response = uploadManager.put(byteInputStream,key,upToken,null, null); DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); //key为空时默认使用hash值作为key System.out.println(putRet.key); System.out.println(putRet.hash); fileName=putRet.hash; Logger.getLogger("end>>>>>>>>>>").info("图片上传"); }catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); } catch (IOException e) { e.printStackTrace(); } return fileName; } /** * 上传图片 * @param bytes 要上传图片的字节流 * @return */ public static String uploadFile(byte[] bytes) { //构造一个带指定Zone对象的配置类 Configuration cfg = new Configuration(Zone.zone0()); //...其他参数参考类注释 UploadManager uploadManager = new UploadManager(cfg); //默认不指定key的情况下,以文件内容的hash值作为文件名 String fileName=null; String key = null; try { Logger.getLogger("start>>>>>>>>>>").info("图片上传"); byte[] uploadBytes=bytes; ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes); Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); Response response = uploadManager.put(byteInputStream,key,upToken,null, null); DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); //key为空时默认使用hash值作为key System.out.println(putRet.key); System.out.println(putRet.hash); fileName=putRet.hash; Logger.getLogger("end>>>>>>>>>>").info("图片上传"); }catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); } catch (IOException e) { e.printStackTrace(); } return fileName; } /** * 字节数组上传 * @return */ public static String upLoadByte(byte[] data) { Configuration cfg = new Configuration(Zone.zone0()); UploadManager uploadManager = new UploadManager(cfg); String fileName=null; String key = null; try { byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8"); Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); try { Response response = uploadManager.put(data, key, upToken); //解析上传成功的结果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); fileName=putRet.hash; } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { //ignore } } } catch (UnsupportedEncodingException ex) { } return fileName; } /** * 生成加签访问url */ public static String getUrl(String key) { String finalUrl=null; String fileName = key; String encodedFileName = null; try { encodedFileName = URLEncoder.encode(fileName, "utf-8"); String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName); Auth auth = Auth.create(accessKey, secretKey); long expireInSeconds = 10800;//1小时,可以自定义链接过期时间 finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(finalUrl); return finalUrl; } /** * 生成加签访问url */ public static String getUrl1(String key) { String finalUrl=null; String fileName = key; String encodedFileName = null; try { encodedFileName = URLEncoder.encode(fileName, "utf-8"); String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName); Auth auth = Auth.create(accessKey, secretKey); long expireInSeconds = 10800;//1小时,可以自定义链接过期时间 finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(finalUrl); return finalUrl; } //删除上传到七牛云的图片 public static boolean deleteOnQn(String key) throws Exception { //密钥配置 try { Auth auth = Auth.create(accessKey,secretKey); Configuration configuration = new Configuration(Zone.autoZone()); BucketManager bucketManager = new BucketManager(auth,configuration); bucketManager.delete(bucket,key); } catch (QiniuException e) { e.printStackTrace(); throw e; } return true; }
4,需要的依赖架
<dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>7.2.11</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.3.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.qiniu</groupId> <artifactId>happy-dns-java</artifactId> <version>0.1.4</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--七牛云图片服务器相关依赖结束-->
5,xml里面需要文件上传处理
<!--文件上传处理器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5000000"/> <property name="defaultEncoding" value="UTF-8"/> </bean>
6,写上传接口的controller和service以及serviceimpl,
更多相关技术文章,请访问PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using JavaSDK to connect to Qiniu Cloud data processing: How to achieve data conversion and analysis? Overview: In the era of cloud computing and big data, data processing is a very important link. Qiniu Cloud provides powerful data processing functions, which can perform image processing, audio and video processing, text processing, etc. on various types of files stored in Qiniu Cloud. This article will introduce how to use JavaSDK to interface with the data processing functions of Qiniu Cloud, and give some commonly used code examples. To install JavaSDK first, we need to introduce it into the project

As one of the most popular short video sharing platforms in the world, Douyin has attracted hundreds of millions of users to join it. When looking at other people's wonderful works, we are often moved by some dynamic, interesting or meaningful moments in them. At this time, we can not only express our opinions and thoughts through text comments, but also express our emotions more vividly through picture comments. So, how to post picture comments on TikTok? First, open the Douyin APP and enter the video you are interested in. Next, we need to determine the operating system of the mobile phone according to the different

Teach you step by step how to use Python to connect to the Qiniu Cloud interface to achieve audio merging. Introduction: In the process of audio processing, sometimes we need to merge multiple audio files into one file. For developers, they can use the Python language to implement the audio merging function by connecting to the Qiniu Cloud interface. This article will introduce in detail how to use Python to connect to the Qiniu Cloud interface to achieve audio merging. Step 1: Install dependent libraries. Before using Python to connect to Qiniu Cloud interface, we need to install the corresponding dependent libraries first. Open a terminal or command

Teach you step by step how to use Python to connect to the Qiniu Cloud interface to achieve audio cutting. In the field of audio processing, Qiniu Cloud is a very excellent cloud storage platform that provides a wealth of interfaces for various processing of audio. This article will use Python as an example to teach you step by step how to connect to the Qiniu Cloud interface to realize the audio cutting function. First, we need to install the corresponding Python library for interacting with Qiniu Cloud. Enter the following command on the command line to install: pipinstallqiniu After the installation is complete, I

Qiniu Cloud Data Processing Management Guide: How does JavaSDK implement data operation and analysis? Introduction: With the advent of the big data era, data processing and analysis are becoming more and more important. As an enterprise focusing on cloud storage and data services, Qiniu Cloud provides a wealth of data processing and analysis functions to facilitate users to process and analyze massive data. This article will introduce how to use Qiniu Cloud's JavaSDK to implement data operations and analysis. 1. Preparation Before starting, we need to prepare some necessary tools and environment: Apply for Qiniu Cloud Account

Integration solution for developing Qiniu cloud interface using Go language Introduction: With the popularity of cloud computing, more and more enterprises have begun to store data on the cloud. As a major cloud storage service provider, Qiniu Cloud provides users with stable and efficient object storage services. This article will introduce how to use Go language to develop the integration solution of Qiniu Cloud interface, and attach code examples. 1. Overview Qiniu Cloud’s interface provides a wealth of functions, including file upload, download, deletion, file list viewing, etc. In order to facilitate developers to use these functions, we can use G

Learn Python to implement Qiniu Cloud interface docking and realize image merging function. Introduction: In recent years, with the continuous development of cloud computing technology, cloud storage services have become one of the important means to solve data storage and backup. As a well-known cloud storage service provider in China, Qiniu Cloud provides developers with rich interfaces to facilitate the storage and management of media resources such as images. This article will introduce how to use Python language to connect to the Qiniu Cloud interface and implement the image merging function. Step 1: Install dependent modules Before starting coding, we first

How to use PHP to convert images from Qiniu Cloud Storage to Base64 format? Pictures play an important role in network transmission and storage. Qiniu Cloud Storage is a widely used cloud storage platform that provides stable and efficient image storage services. Sometimes, we need to convert images in Qiniu cloud storage to Base64 format for use in front-end display or other purposes. In this article, we will introduce how to use PHP to convert images from Qiniu Cloud Storage to Base64 format. Step 1: Install Qiniu Cloud PHPS
