7牛图床的使用

本贴最后更新于 3012 天前,其中的信息可能已经时移俗易

上传策略

上传策略_使用指南_对象存储 - 七牛开发者中心

上传凭证

上传凭证_使用指南_对象存储 - 七牛开发者中心

各种凭证的例子

http://blog.csdn.net/netdxy/article/details/48467985

sample

import java.net.URLDecoder;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.qiniu.util.StringUtils;
import com.qiniu.util.UrlSafeBase64;
/**

  • 业务实现类

*/
public class ImageServiceBizImpl {

// 设置好账号的ACCESS_KEY和SECRET_KEY
public static String ACCESS_KEY =  PropertiesUtil.getStringValue("QINIU_ACCESS_KEY");
public static String SECRET_KEY =  PropertiesUtil.getStringValue("QINIU_SECRET_KEY");
public static String BUCKETNAME =  PropertiesUtil.getStringValue("QINIU_BUCKETNAME");
public static String BASE_URL = PropertiesUtil.getStringValue("QINIU_BASE_URL");
public static String CALL_BACK_URL =  PropertiesUtil.getStringValue("QINIU_CALL_BACK_URL");

//获取上传的token
public String getUpLoadtoken() {
	String uptoken = null;
	// 请确保该bucket已经存在
	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.HOUR_OF_DAY, 1);
	long deadline = cal.getTimeInMillis() / 1000;
	try {
		StringBuffer putpolicy = new StringBuffer(
				"{\"scope\":\"" + BUCKETNAME + "\",\"deadline\":" + deadline + ",");
		putpolicy.append("\"callbackUrl\":\"" + CALL_BACK_URL + "\",");
		putpolicy.append("\"callbackBody\":\"bucket=$(bucket)&fname=$(fname)"); 
		putpolicy.append("&imageInfo=$(imageInfo)");
		putpolicy.append("&hash=$(etag)&key=$(key)&fsize=$(fsize)&mimeType=$(mimeType)\"");
		putpolicy.append("}");
		String t3 = UrlSafeBase64.encodeToString(putpolicy.toString());
		Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
		mac.init(new SecretKeySpec(StringUtils.utf8Bytes(SECRET_KEY), "HmacSHA1"));
		String t2 = UrlSafeBase64.encodeToString(mac.doFinal(StringUtils.utf8Bytes(t3)));
		uptoken = ACCESS_KEY + ":" + t2 + ":" + t3;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return uptoken;
}

//回调反馈内容
public Map callback(HttpServletRequest req) {
	Map retMap = null ;
	try { 
		String ret = readPostData(req);
		System.out.println(" req:" + ret );
		// 返回给客户端结果
		Map map = getUrlParams(ret);
		if ( map.containsKey("key")) { 
			boolean bRet = true;
			retMap = getResult(map);
			if ( ! bRet  ){
				retMap = null ;
			}
			System.out.println(" saveImage 2 db :" + bRet  );
		} 
		System.out.println(" retMap:" + map.toString()  );
		return retMap;
	} catch (Exception e) { 
		e.printStackTrace();  
		return retMap; 
	}
}
public Map<String,Object> getResult(Map map){
	
	Map<String,Object> retMap  =new HashMap<String,Object>(0); 
	try{
		retMap.put("file_name", map.get("key").toString());
		if ( map.containsKey("mimeType") ) {
			retMap.put("mimeType", map.get("mimeType").toString());
		}
		if ( map.containsKey("userId") ) {
			retMap.put("userId", map.get("userId").toString());
		}
		if ( map.containsKey("imageInfo") ) {
			String imageInfo = map.get("imageInfo").toString();
			Map mapImagInfo = JSON.parseObject(imageInfo); 
			retMap.put("imageInfo",mapImagInfo);
		}
		if ( map.containsKey("fsize") ) {
			retMap.put("fsize", map.get("fsize").toString());
		}
		if ( map.containsKey("msgId") ) {
			retMap.put("msgId", map.get("msgId").toString());
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	return retMap;
}

private String readPostData(HttpServletRequest request) {

	String strBody = null;
	byte[] xmldataByte = null;
	try {
		ServletInputStream sis = null;
		// 取HTTP请求流
		sis = request.getInputStream();
		// 取HTTP请求流长度
		int size = request.getContentLength();
		// 用于缓存每次读取的数据
		byte[] buffer = new byte[size];
		// 用于存放结果的数组
		xmldataByte = new byte[size];
		int count = 0;
		int rbyte = 0;

		// 循环读取
		while (count < size) {
			// 每次实际读取长度存于rbyte中
			rbyte = sis.read(buffer);
			for (int i = 0; i < rbyte; i++) {
				xmldataByte[count + i] = buffer[i];
			}
			count += rbyte;
		}

		strBody = new String(xmldataByte, "UTF-8");
		strBody = URLDecoder.decode(strBody, "UTF-8");
		strBody = strBody.replace("\r", "").replace("\n", "");

	} catch (Exception e) {
		 e.printStackTrace();
	}

	return strBody;
}

/**
 * 将参数转换为map
 * @param param
 * @return
 */
public Map<String, Object> getUrlParams(String param) {  
	 Map<String, Object> map = new HashMap<String, Object>(0);  
	 try{
		 
		    if ( StringHelp.isBlank(param) ) {  
		        return map;  
		    }  
		    String[] params = param.split("&");  
		    for (int i = 0; i < params.length; i++) {  
		        String[] p = params[i].split("=");  
		        if (p.length == 2) {  
		            map.put(p[0], p[1]);  
		        }  
		    }  
		    return map; 
	 } catch (Exception e) {
		e.printStackTrace();
	}
	 return map;
}

}

  • 七牛云

    七牛云是国内领先的企业级公有云服务商,致力于打造以数据为核心的场景化 PaaS 服务。围绕富媒体场景,七牛先后推出了对象存储,融合 CDN 加速,数据通用处理,内容反垃圾服务,以及直播云服务等。

    27 引用 • 225 回帖 • 169 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...