FastDFS操作类
package com.iflytek.atp.util;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.DownloadStream;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import com.iflytek.property.cache.MemoryPropertyPlaceholderConfigurer;
public class FastDFSFileUtils {
static {
try {
String trackerServer = PropertyUtil.getProperty("fast_dfs_tracker_server");
String connect_timeout = PropertyUtil.getProperty("fast_dfs_connect_timeout");
String network_timeout = PropertyUtil.getProperty("fast_dfs_network_timeout");
String charset = PropertyUtil.getProperty("fast_dfs_charset");
String tracker_http_port = PropertyUtil.getProperty("fastdfs_tracker_port");
Map<String, String> params = new HashMap<String, String>();
params.put("connect_timeout", connect_timeout);
params.put("network_timeout", network_timeout);
params.put("charset", charset);
params.put("tracker_server", trackerServer);
params.put("http.tracker_http_port", tracker_http_port);
ClientGlobal.init(params);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
public static FastDFSFileUtils instance = new FastDFSFileUtils();
private FastDFSFileUtils() {
}
public static FastDFSFileUtils getInstance() {
if (instance == null) {
instance = new FastDFSFileUtils();
}
return instance;
}
/**
* fast fds 上传文件
*
* @param is
* 输入流
* @param fileName
* 原始文件名
* @param fileSuffixName
* 文件后缀名,不带.
* @param bizId
* 远程文件名,建议用UUID.randomUUID().toString()
* @return 返回fastfds 文件名
* 例如:group1/M02/2D/FA/rBAKHlhH01GANb10AAAAJp0UsIA50.conf
*/
public static String uloadFileByFds(InputStream is, String fileName,String fileSuffixName, String bizId) {
byte[] data = null;
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new ByteArrayOutputStream();
int count = 0;
byte[] buff = new byte[' '];
while ((count = bis.read(buff)) != -1) {
bos.write(buff, 0, count);
}
bos.flush();
data = bos.toByteArray();
String maxSizeStr = MemoryPropertyPlaceholderConfigurer.getContextProperty("upload_file_size_limit");
if (StringUtils.isBlank(maxSizeStr)) {
maxSizeStr = "30";
}
long maxSize = Long.parseLong(maxSizeStr);
maxSize = maxSize * 1028 * 1024;
long fileSize = data.length;
if (fileSize > maxSize) {
System.out.println("文件上传超限!");
}
// 建立连接
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer,storageServer);
// 设置元信息
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName", bizId);
metaList[1] = new NameValuePair("fileExtName", fileSuffixName);
metaList[2] = new NameValuePair("fileLength",String.valueOf(fileSize));
// 上传文件
return client.upload_file1(data, fileSuffixName, metaList);
} catch (Exception e) {
e.printStackTrace();
System.out.println("文件上传有误!" + e.getMessage());
}
return bizId;
}
/**
*
* @param fileId
* @return
*/
public static InputStream downloadFile(String fileId) {
try {
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer;
trackerServer = tracker.getConnection();
StorageServer storageServer = null;
org.csource.fastdfs.StorageClient1 storageClient = new org.csource.fastdfs.StorageClient1(
trackerServer, storageServer);
byte[] datas = storageClient.download_file1(fileId);
return new ByteArrayInputStream(datas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param fileId
* 远程文件ID,该ID必须是上传接口返回的fileId
* @param out
* 本地文件的输出流
* @return 返回0表示成功,其它则表示失败
*/
public int downloadFile(String fileId, OutputStream out) {
try {
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer;
trackerServer = tracker.getConnection();
StorageServer storageServer = null;
org.csource.fastdfs.StorageClient1 storageClient = new org.csource.fastdfs.StorageClient1(
trackerServer, storageServer);
int statusCode = storageClient.download_file1(fileId,
new DownloadStream(out));
return statusCode;
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return 0;
}
public List<String> unZipFiles(File zipFile,String descDir)throws IOException{
ZipFile zip = new ZipFile(zipFile,Charset.forName("GBK"));
List<String> list = new ArrayList<String>();
for(Enumeration entries = zip.entries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
list.add(zipEntryName);
InputStream in = zip.getInputStream(entry);
String outPath = descDir+"/"+zipEntryName;
// //判断路径是否存在,不存在则创建文件路径
// if(outPath.lastIndexOf('/') != -1) {
// outPath = outPath.substring(0, outPath.lastIndexOf('/'));
// }
File file = new File(outPath);
inputStreamToFile(in,file,descDir);
}
return list;
}
public void inputStreamToFile(InputStream is,File file,String dir) throws IOException{
System.out.println("dir" + dir);
//创建目录
if(!StringUtils.isEmpty(dir)) {
File pathFile = new File(dir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
}
System.out.println("file:" + file);
//创建文件
if(!file.exists()){
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();
}
}
获取配置信息类
package com.iflytek.atp.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PropertyUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
private static Properties props;
static {
loadProps();
}
synchronized static private void loadProps(){
logger.info("开始加载properties文件内容.......");
props = new Properties();
InputStream in = null;
try {
//第一种,通过类加载器进行获取properties文件流-->
in = PropertyUtil.class.getClassLoader().getResourceAsStream("common_config.properties");
props.load(in);
} catch (FileNotFoundException e) {
logger.error("jdbc.properties文件未找到");
} catch (IOException e) {
logger.error("出现IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
logger.error("jdbc.properties文件流关闭出现异常");
}
}
logger.info("加载properties文件内容完成...........");
logger.info("properties文件内容:" + props);
}
public static String getProperty(String key) {
if (null == props) {
loadProps();
}
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
if (null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}
common_config.properties配置相关信息