完整代码 分享
Dns
package cn.wizzer.common.qiniu;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
public interface Dns {
List<InetAddress> lookup(String hostname) throws UnknownHostException;
}
Configuration
package cn.wizzer.common.qiniu;
import com.qiniu.common.Constants;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.common.ZoneReqInfo;
import com.qiniu.http.ProxyConfiguration;
/**
* 该类封装了SDK相关配置参数
*/
public final class Configuration implements Cloneable {
/**
* 使用的Zone
*/
public Zone zone;
/**
* 空间相关上传管理操作是否使用 https , 默认 是
*/
public boolean useHttpsDomains = true;
/**
* 如果文件大小大于此值则使用断点上传, 否则使用Form上传
*/
public int putThreshold = Constants.BLOCK_SIZE;
/**
* 连接超时时间 单位秒(默认10s)
*/
public int connectTimeout = Constants.CONNECT_TIMEOUT;
/**
* 写超时时间 单位秒(默认 0 , 不超时)
*/
public int writeTimeout = Constants.WRITE_TIMEOUT;
/**
* 回复超时时间 单位秒(默认30s)
*/
public int readTimeout = Constants.READ_TIMEOUT;
/**
* 底层HTTP库所有的并发执行的请求数量
*/
public int dispatcherMaxRequests = Constants.DISPATCHER_MAX_REQUESTS;
/**
* 底层HTTP库对每个独立的Host进行并发请求的数量
*/
public int dispatcherMaxRequestsPerHost = Constants.DISPATCHER_MAX_REQUESTS_PER_HOST;
/**
* 底层HTTP库中复用连接对象的最大空闲数量
*/
public int connectionPoolMaxIdleCount = Constants.CONNECTION_POOL_MAX_IDLE_COUNT;
/**
* 底层HTTP库中复用连接对象的回收周期(单位分钟)
*/
public int connectionPoolMaxIdleMinutes = Constants.CONNECTION_POOL_MAX_IDLE_MINUTES;
/**
* 上传失败重试次数
*/
public int retryMax = 5;
/**
* 外部dns
*/
public Dns dns;
/*
* 解析域名时,优先使用host配置,主要针对内部局域网配置
*/
public boolean useDnsHostFirst;
/**
* 代理对象
*/
public ProxyConfiguration proxy;
/**
* 特殊默认域名
*/
public static String defaultRsHost = "rs.qiniu.com";
public static String defaultApiHost = "api.qiniu.com";
public static String defaultUcHost = "uc.qbox.me";
public Configuration() {
}
public Configuration(Zone zone) {
this.zone = zone;
}
@Override
public com.qiniu.storage.Configuration clone() {
try {
return (com.qiniu.storage.Configuration) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
public String upHost(String upToken) throws QiniuException {
ZoneReqInfo zoneReqInfo = new ZoneReqInfo(upToken);
if (zone == null) {
zone = Zone.autoZone();
}
return useHttpsDomains ? zone.getUpHttps(zoneReqInfo)
: zone.getUpHttp(zoneReqInfo);
}
public String upHostBackup(String upToken) throws QiniuException {
ZoneReqInfo zoneReqInfo = new ZoneReqInfo(upToken);
if (zone == null) {
zone = Zone.autoZone();
}
return useHttpsDomains ? zone.getUpBackupHttps(zoneReqInfo)
: zone.getUpBackupHttp(zoneReqInfo);
}
public String ioHost(String ak, String bucket) {
ZoneReqInfo zoneReqInfo = new ZoneReqInfo(ak, bucket);
if (zone == null) {
zone = Zone.autoZone();
}
return useHttpsDomains ? zone.getIovipHttps(zoneReqInfo)
: zone.getIovipHttp(zoneReqInfo);
}
public String apiHost(String ak, String bucket) {
ZoneReqInfo zoneReqInfo = new ZoneReqInfo(ak, bucket);
if (zone == null) {
zone = Zone.autoZone();
}
return useHttpsDomains ? zone.getApiHttps(zoneReqInfo)
: zone.getApiHttp(zoneReqInfo);
}
public String rsHost() {
String scheme = "http://";
if (useHttpsDomains) {
scheme = "https://";
}
return scheme + defaultRsHost;
}
public String apiHost() {
String scheme = "http://";
if (useHttpsDomains) {
scheme = "https://";
}
return scheme + defaultApiHost;
}
public String rsHost(String ak, String bucket) {
ZoneReqInfo zoneReqInfo = new ZoneReqInfo(ak, bucket);
if (zone == null) {
zone = Zone.autoZone();
}
return useHttpsDomains ? zone.getRsHttps(zoneReqInfo)
: zone.getRsHttp(zoneReqInfo);
}
public String rsfHost(String ak, String bucket) {
ZoneReqInfo zoneReqInfo = new ZoneReqInfo(ak, bucket);
if (zone == null) {
zone = Zone.autoZone();
}
return useHttpsDomains ? zone.getRsfHttps(zoneReqInfo)
: zone.getRsfHttp(zoneReqInfo);
}
public String ucHost() {
String scheme = "http://";
if (useHttpsDomains) {
scheme = "https://";
}
return scheme + defaultUcHost;
}
}
QiniuConfig
package cn.wizzer.common.qiniu;
import com.qiniu.util.Auth;
public final class QiniuConfig {
//dummy ak & sk
public static final String dummyAccessKey = "填写你的accessKey";
public static final String dummySecretKey = "填写你的SecretKey";
public static final Auth dummyAuth = Auth.create(dummyAccessKey, dummySecretKey);
public static final String dummyBucket = "";
public static final String dummyKey = "no-such-dummy-key";
public static final String dummyDomain = "";
public static final String dummyUrl= "http://七牛云图片地址";
public static final String dummyUptoken = "ak:token:putpolicy";
public static final String dummyInvalidUptoken = "invalidtoken";
//code
public static final int ERROR_CODE_BUCKET_NOT_EXIST = 631;
public static final int ERROR_CODE_KEY_NOT_EXIST = 612;
private QiniuConfig() {
}
public static boolean isTravis() {
return "travis".equals(System.getenv("QINIU_TEST_ENV"));
}
}
工具类
package cn.wizzer.common.qiniu;
import cn.wizzer.common.util.DateUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import org.nutz.lang.random.R;
import org.nutz.mvc.upload.TempFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
/**
* 七牛云 文件上传工具类
*
* @author Hamming_Yu on 2018/11/15.
*/
public class UpLoadUtil {
private static UploadManager uploadManager = new UploadManager(new Configuration());
/**
* 文件上传工具类
*
* @param tf
* @return
*/
public static String upLoadFile(TempFile tf) {
if (tf == null) {
return "";
}
String name = DateUtil.format(new Date(), "yyyyMMdd") + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
try (InputStream is = tf.getInputStream()) {
String token = QiniuConfig.dummyAuth.uploadToken(QiniuConfig.dummyBucket);
byte[] byteData = new byte[is.available()];
is.read(byteData);
Response response = uploadManager.put(byteData, name, token);
} catch (QiniuException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return QiniuConfig.dummyUrl + "/" + name;
}
}
maven 引入
<!--七牛云-->
<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>