import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qiniu.util.Auth; public class QiniuConfig { final Logger logger = LoggerFactory.getLogger(QiniuConfig.class); private String ak; private String sk; private String domail; private String style; private String bucket; public QiniuConfig(String propertiesFile) { Properties properties = new Properties(); InputStream is = QiniuConfig.class.getClassLoader().getResourceAsStream(propertiesFile); try { properties.load(is); } catch (IOException e) { logger.error("QiniuConfig", e); } ak = properties.getProperty("ak"); sk = properties.getProperty("sk"); domail = properties.getProperty("domail"); style = properties.getProperty("style"); bucket = properties.getProperty("bucket"); } public String getAk() { return ak; } public void setAk(String ak) { this.ak = ak; } public String getSk() { return sk; } public void setSk(String sk) { this.sk = sk; } public String getDomail() { return domail; } public void setDomail(String domail) { this.domail = domail; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public String getBucket() { return bucket; } public void setBucket(String bucket) { this.bucket = bucket; } // bucket 空间名 // key key,可为 null // expires 有效时长,单位秒。默认3600s // policy 上传策略的其它参数,如 new // StringMap().put("endUser","uid").putNotEmpty("returnBody", "")。scope通过 // bucket、key间接设置,deadline 通过 // expires 间接设置 // strict 是否去除非限定的策略字段,默认true // 生成的上传token // 简单上传,使用默认策略 public String getToken(String bucketName) { Auth auth = Auth.create(ak, sk); try { return auth.uploadToken(bucketName); } catch (Exception e) { logger.debug("策略获取失败"); e.printStackTrace(); } return null; } // 覆盖上传 public String getUpTokenCover(String bucketName, String key) { Auth auth = Auth.create(ak, sk); return auth.uploadToken(bucketName, key); }
import java.io.File; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qiniu.common.QiniuException; import com.qiniu.http.Response; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; public class QiniuKit { static final Logger logger = LoggerFactory.getLogger(QiniuKit.class); static QiniuConfig mConfig; static void init(QiniuConfig config) { mConfig = config; } static UploadManager uploadManager = new UploadManager(); // 上传内存中数据 public static String upload(byte[] data, String key) { String path = ""; try { Auth auth = Auth.create(mConfig.getAk(), mConfig.getSk()); Response res = uploadManager.put(data, key, auth.uploadToken(mConfig.getBucket())); if (res.isOK()) { path = mConfig.getDomail().concat("/").concat(key); return path; } else { return path; } } catch (QiniuException e) { Response r = e.response; // 请求失败时简单状态信息 logger.error(r.toString()); try { // 响应的文本信息 logger.error(r.bodyString()); } catch (QiniuException e1) { // ignore } return path; } } /** * @Title: upload * @author leizhen.wang * @Description: TODO(上传文件) * @param filePath * 文件地址 * @param key * 文件名称 * @param 设定文件 * @return String 返回类型 * @throws */ public static String upload(String filePath, String key) { String path = ""; try { Auth auth = Auth.create(mConfig.getAk(), mConfig.getSk()); Response res = uploadManager.put(filePath, key, auth.uploadToken(mConfig.getBucket())); if (res.isOK()) { // success path = mConfig.getDomail().concat("/").concat(key); return path; } else { return path; } } catch (QiniuException e) { Response r = e.response; // 请求失败时简单状态信息 logger.error(r.toString()); try { // 响应的文本信息 logger.error(r.bodyString()); } catch (QiniuException e1) { // ignore } return path; } } /** * @Title: upload * @author leizhen.wang * @Description: TODO(上传文件) * @param File * file 对象 * @param key * 文件名称 * @param 设定文件 * @return String 返回类型 * @throws */ public static String upload(File file, String key) { String path = ""; try { Auth auth = Auth.create(mConfig.getAk(), mConfig.getSk()); Response res = uploadManager.put(file, key, auth.uploadToken(mConfig.getBucket())); if (res.isOK()) { path = mConfig.getDomail().concat("/").concat(key); return path; } else { return path; } } catch (QiniuException e) { Response r = e.response; // 请求失败时简单状态信息 logger.error(r.toString()); try { // 响应的文本信息 logger.error(r.bodyString()); } catch (QiniuException e1) { e1.printStackTrace(); } return path; } }
import jetbrick.web.mvc.plugin.Plugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class QiniuPlugin implements Plugin { final Logger log = LoggerFactory.getLogger(QiniuPlugin.class); private String propFile = "config/qiniu.properties"; public QiniuPlugin() { } public QiniuPlugin(String propFile) { this.propFile = propFile; } @Override public void destory() { } @Override public void initialize() { log.info("------------------开始启动qiniu插件--------------------"); QiniuKit.init(new QiniuConfig(propFile)); log.info("------------------启动完毕qiniu插件--------------------"); }
jar :okhttp-2.4.0.jar,okio-1.5.0.jar,qiniu-java-sdk-7.0.4.jar
时间: 2024-11-09 21:38:57