java 高性能文件系统_高可用高性能分布式文件系统FastDFS实践Java程序

package com.james.utils;

import org.csource.common.MyException;

import org.csource.common.NameValuePair;

import org.csource.fastdfs.*;

import java.io.BufferedOutputStream;

import java.io.IOException;

import java.net.URLDecoder;

/**

* Created by James on 2015/11/14.

* FastDFS文件上传

*/

public class FastDFSClientUtils {

private TrackerClient trackerClient = null;

private TrackerServer trackerServer = null;

private StorageServer storageServer = null;

private StorageClient1 storageClient = null;

public FastDFSClientUtils(String conf) throws Exception {

if (conf.contains("classpath:")) {

String path = this.getClass().getResource("/").getPath();

conf = conf.replace("classpath:", URLDecoder.decode(path, "UTF-8"));

}

ClientGlobal.init(conf);

trackerClient = new TrackerClient();

trackerServer = trackerClient.getConnection();

storageServer = null;

storageClient = new StorageClient1(trackerServer, storageServer);

}

/**

* 上传文件方法

*

Title: uploadFile

*

Description:

*

* @param fileName 文件全路径

* @param extName  文件扩展名,不包含(.)

* @param metas    文件扩展信息

* @return

* @throws Exception

*/

public String uploadFile(String fileName, String extName, NameValuePair[] metas) {

String result = null;

try {

result = storageClient.upload_file1(fileName, extName, metas);

} catch (IOException e) {

e.printStackTrace();

} catch (MyException e) {

e.printStackTrace();

}        return result;

}

/**

* 上传文件,传fileName

*

* @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg

* @return null为失败

*/

public String uploadFile(String fileName) {

return uploadFile(fileName, null, null);

}

/**

* @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg

* @param extName  文件的扩展名 如 txt jpg等

* @return null为失败

*/

public String uploadFile(String fileName, String extName) {

return uploadFile(fileName, extName, null);

}

/**

* 上传文件方法

*

Title: uploadFile

*

Description:

*

* @param fileContent 文件的内容,字节数组

* @param extName    文件扩展名

* @param metas      文件扩展信息

* @return

* @throws Exception

*/

public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) {

String result = null;

try {

result = storageClient.upload_file1(fileContent, extName, metas);

} catch (IOException e) {

e.printStackTrace();

} catch (MyException e) {

e.printStackTrace();

}        return result;

}

/**

* 上传文件

*

* @param fileContent 文件的字节数组

* @return null为失败

* @throws Exception

*/

public String uploadFile(byte[] fileContent) throws Exception {

return uploadFile(fileContent, null, null);

}    /**

* 上传文件

*

* @param fileContent 文件的字节数组

* @param extName    文件的扩展名 如 txt  jpg png 等

* @return null为失败

*/

public String uploadFile(byte[] fileContent, String extName) {

return uploadFile(fileContent, extName, null);

}    /**

* 文件下载到磁盘

*

* @param path  图片路径

* @param output 输出流 中包含要输出到磁盘的路径

* @return -1失败,0成功

*/

public int download_file(String path, BufferedOutputStream output) {

//byte[] b = storageClient.download_file(group, path);

int result = -1;

try {

byte[] b = storageClient.download_file1(path);

try {

if (b != null) {

output.write(b);

result = 0;

}

} catch (Exception e) {

} //用户可能取消了下载

finally {

if (output != null)

try {

output.close();

} catch (IOException e) {

e.printStackTrace();

}

}

} catch (Exception e) {

e.printStackTrace();

}        return result;

}

/**

* 获取文件数组

*

* @param path 文件的路径 如group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg

* @return

*/

public byte[] download_bytes(String path) {

byte[] b = null;

try {

b = storageClient.download_file1(path);

} catch (IOException e) {

e.printStackTrace();

} catch (MyException e) {

e.printStackTrace();

}        return b;

}

/**

* 删除文件

*

* @param group      组名 如:group1

* @param storagePath 不带组名的路径名称 如:M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg

* @return -1失败,0成功

*/

public Integer delete_file(String group, String storagePath) {

int result = -1;

try {

result = storageClient.delete_file(group, storagePath);

} catch (IOException e) {

e.printStackTrace();

} catch (MyException e) {

e.printStackTrace();

}        return result;

}

/**

* @param storagePath 文件的全部路径 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg

* @return -1失败,0成功

* @throws IOException

* @throws Exception

*/

public Integer delete_file(String storagePath) {

int result = -1;

try {

result = storageClient.delete_file1(storagePath);

} catch (IOException e) {

e.printStackTrace();

} catch (MyException e) {

e.printStackTrace();

}        return result;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值