上传图片到七牛云存储
步骤1:maven配置,增加七牛的sdk
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.0</version>
</dependency>
步骤2:运行七牛的官方Demo
package uk.co.phoenix.flickr;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import java.io.IOException;
/**
* Created by Administrator on 2017/2/16.
*/
public class QiNiuDemo {
//设置好账号的ACCESS_KEY和SECRET_KEY
String ACCESS_KEY = "B82veaKXYaTOD1wu_j8Ut1Mamlm7ArnHNe5soCyY";
String SECRET_KEY = "Dz3t0KYzN4JPLsVjWSsUsUP903EI7ZJp2kv8KO1H";
//要上传的空间
String bucketname = "fengwuze-feed";
//上传到七牛后保存的文件名
String key = "3.jpg";
//上传文件的路径
String FilePath = "G:\\萌宝宝\\3.jpg";
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
///////////////////////指定上传的Zone的信息//////////////////
//第一种方式: 指定具体的要上传的zone
//注:该具体指定的方式和以下自动识别的方式选择其一即可
//要上传的空间(bucket)的存储区域为华东时
// Zone z = Zone.zone0();
//要上传的空间(bucket)的存储区域为华北时
// Zone z = Zone.zone1();
//要上传的空间(bucket)的存储区域为华南时
// Zone z = Zone.zone2();
//第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
Zone z = Zone.autoZone();
Configuration c = new Configuration(z);
//创建上传对象
UploadManager uploadManager = new UploadManager(c);
public static void main(String args[]) throws IOException {
new QiNiuDemo().upload();
}
//简单上传,使用默认策略,只需要设置上传的空间名就可以了
public String getUpToken() {
return auth.uploadToken(bucketname);
}
public void upload() throws IOException {
try {
//调用put方法上传
Response res = uploadManager.put(FilePath, key, getUpToken());
//打印返回的信息
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
}
}
}
}
其他
Idea设置jdk编译版本
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>