Jfinal的七牛云存储插件:qiniuPlugin for jfinal.

不多说 直接贴代码,使用开源,回馈开源。这部分是来之于爱睦家商城(www.imujia.com)的部分源代码。七牛sdk不支持分页获取 资源信息,鄙视!!

插件:

QiniuPlugin.java

package com.imujia.server.plugin.qiniu;

import com.jfinal.plugin.IPlugin;

/**
 * 
 * @author michael QQ&微信:198819880
 * 爱睦家商城 www.imujia.com
 * 
 */
public class QiniuPlugin  implements IPlugin {

	private String propFile = "qiniu.properties";
	
	public QiniuPlugin() {}
	public QiniuPlugin(String propFile) {
		this.propFile = propFile;
	}
	
	@Override
	public boolean start() {
		QiniuKit.init(new QiniuConfig(propFile));
		return true;
	}

	@Override
	public boolean stop() {
		return true;
	}

}



QiniuConfig.java

package com.imujia.server.plugin.qiniu;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.json.JSONException;

import com.qiniu.api.auth.AuthException;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.rs.PutPolicy;
import com.qiniu.api.rs.RSClient;
import com.qiniu.api.rsf.RSFClient;

/**
 * 
 * @author michael QQ&微信:198819880
 * 爱睦家商城 www.imujia.com
 * 
 */
public class QiniuConfig {
	private static final Logger logger = Logger.getLogger(QiniuConfig.class);
	
	private String ak;
	private String sk;
	
	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");
	}

	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  getToken(String bucketName){
		Mac mac = new Mac(ak, sk);
        PutPolicy putPolicy = new PutPolicy(bucketName);
        try {
			return putPolicy.token(mac);
		} catch (JSONException e) {
			logger.error("qiniu config get token", e);
		} catch (AuthException e) {
			logger.error("qiniu config get token", e);
		}
        return null;
	}
	
	public RSFClient getRSFClient(){
		Mac mac = new Mac(ak, sk);
		RSFClient client = new RSFClient(mac);
		return client;
	}
	
	
	public RSClient getRSClient(){
		Mac mac = new Mac(ak, sk);
		RSClient client = new RSClient(mac);
		return client;
	}
	
	

}




QiniuKit.java

package com.imujia.server.plugin.qiniu;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.RSClient;
import com.qiniu.api.rsf.ListItem;
import com.qiniu.api.rsf.ListPrefixRet;
import com.qiniu.api.rsf.RSFClient;

/**
 * 
 * @author michael QQ&微信:198819880
 * 爱睦家商城 www.imujia.com
 * 
 */
public class QiniuKit {

	static QiniuConfig mConfig;
	static void init(QiniuConfig config){
		mConfig = config;
	}
	
	
	public static PutRet put(String bucketName,String filePath,InputStream is){
		 String token = mConfig.getToken(bucketName);
		 return IoApi.Put(token, filePath, is, new PutExtra());
	}
	
	
	public static PutRet put(String bucketName,String filePath,File file){
		 String token = mConfig.getToken(bucketName);
		 return IoApi.putFile(token, filePath, file, new PutExtra());
	}
	
	
	public static List<ListItem> list(String bucketName) {
		return list(bucketName,0);
	}
	
	public static List<ListItem> list(String bucketName,int count) {
		return list(bucketName, "*", count);
	}
	

	public static List<ListItem> list(String bucketName,String directoryPath) {
		return list(bucketName, directoryPath, 0);
	}
	
	public static List<ListItem> list(String bucketName,String directoryPath,int count) {
		if(directoryPath == null)
		{
			directoryPath = "*";
		}
		if(count == 0)
		{
			count = Integer.MAX_VALUE;
		}
		RSFClient  client =  mConfig.getRSFClient();
		
		ListPrefixRet list = client.listPrifix(bucketName, directoryPath, "", count);
		if(list == null) return null;
		
		List<ListItem> items = new ArrayList<ListItem>();
		items.addAll(list.results);
		
		return items ;
	}
	
	
	public static void remove(String bucketName, String filePath){
		RSClient client = mConfig.getRSClient();
		client.delete(bucketName, filePath);
	}
	
}




需要到的jar包有:

httpclient-4.3.6.jar
httpcore-4.3.3.jar
httpmime-4.3.6.jar
qiniu-java-sdk-6.1.7.1.jar
没有jar包的联系我微信我发给你,maven的自己配置啦

使用方法:

1、src目录下添加配置文件qiniu.properties ,内容如下

ak = your qiniu ak
sk = your qiniu sk



2、在jfinal的配置类的

configPlugin方法里添加QiniuPlugin

3、配置好后,如下使用:

QiniuKit.put(bucketName, filePathAndName, file);



4、完毕。



海哥个人QQ&微信号:198819880 ,欢迎交流。

你可能感兴趣的:(jFinal,七牛,七牛云存储)