整合了DFS+DHT+MyFastClitn的java客户端实现

[b]使用FastDFS的一套东西DFS+DHT+myfastClient在WEB应用中就可以很好的解决很多的问题,同样的在DHT中的读取配置文件也存在问题的,本想也改一改的,后来转念一想,干脆就把这三哥儿们集成到一块去吧。集成完的项目结构[/b]
[img]http://dl2.iteye.com/upload/attachment/0097/8440/f85dd8c3-5246-30e6-a31e-94caf52dfc48.png[/img]
[b]并没有改太多的核心实现,实在把很多无用的import去掉了,把Test类给去掉了,把读取配置类重新实现了。使用起来就很简单了,提供一个UTIL类的实现代码[/b]

/**
*
*
* @ClassName: FastDFSUtil
* @Description: TODO( FastDfs 操作工具类)
* @author LiuYi
* @date 2014年6月5日 下午4:39:19
*
*/
public class FastDFSUtil {

private static final FastDFSUtil fastDFSUtil = new FastDFSUtil();
private static boolean isInit = true;
public static final FastDFSUtil getInstance() {
return fastDFSUtil;
}

static{
try {
PropertiesLoader pl = SpringContextHolder.getBean(PropertiesLoader.class);
DFSCustomIdClient.init(pl.getProperty("dfs"), pl.getProperty("dht"));
} catch (Exception e) {
e.printStackTrace();
}
}

public static DFSCustomIdClient getClient(String fdhtNamespace) {
if(isInit){
getInstance();
isInit = false;
}
try {
return new DFSCustomIdClient(fdhtNamespace);
} catch (MyException e) {
e.printStackTrace();
}
return null;
}

/**
*
* @Description: TODO(上传文件到FastDFS)
* @author LiuYi
* @date 2014年6月5日 下午4:33:39
* @param fileName
* 自定义的文件名
* @param file
* 文件
* @param suffix
* 后缀名
* @return
* @throws IOException
* @throws Exception
* String
*/
public static String upFile(String fileName, File file, String suffix) throws IOException, Exception {
DFSCustomIdClient client = getClient("seafood");
try {
client.uploadFile(fileName, FileUtils.readFileToByteArray(file), suffix);
return fileName + "." + suffix;
} catch (Exception e) {
throw e;
} finally {
client.close();
}
}

/**
*
* @Description: TODO(根据当前年与日生成文件名如
* /2014041806/akdhfajhfdabfdjahfdjafda.png)
* @author LiuYi
* @date 2014年6月5日 下午4:34:55
* @param file
* @param suffix
* @return
* @throws IOException
* @throws Exception
* String
*/
public static String upFile(File file, String suffix) throws IOException, Exception {
DFSCustomIdClient client = getClient("seafood");
try {
Calendar cal = Calendar.getInstance();// 使用日历类
int year = cal.get(Calendar.YEAR);// 得到年
int month = cal.get(Calendar.MONTH) + 1;// 得到月,因为从0开始的,所以要加1
int day = cal.get(Calendar.DAY_OF_MONTH);// 得到天
int hour = cal.get(Calendar.HOUR);// 得到小时
int minute = cal.get(Calendar.MINUTE);// 得到分钟
StringBuffer buffer = new StringBuffer("/").append(year).append(month).append(day).append(hour)
.append(minute).append("/").append(UUID.randomUUID().toString());
client.uploadFile(buffer.toString(), FileUtils.readFileToByteArray(file), suffix);
return buffer.toString() + "." + suffix;
} catch (Exception e) {
throw e;
} finally {
if (client != null)
client.close();
}
}
/**
*
* @Description: TODO(通过DB中的URL 获取到真正的在FastDFS上的URL)
* @author LiuYi
* @date 2014年6月5日 下午5:29:29
* @param dbUrl
* @return String
*/
public static String dbImgUrl2FdfsUrl(String dbUrl){
DFSCustomIdClient client = getClient("seafood");
try {
return client.getFdfsFileId(dbUrl);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(client!=null){
client.close();
}
}
return dbUrl;
}


}

附上源码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值