FastDFS连接及上传下载

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配置相关信息
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文本格式不能很好显示,请见谅(附件里有比较齐整的excel表格统计) 大小类型 传输类型 api方法 文件大小 花费时间 速率byte/ms 速率mb/s 缓存数组 次数 备注 大文件 下载 download_file(group_name, remote_filename, localFile) 1073741824(约1G) 28343ms 37883 36.12804413 无 1 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 29195ms 36778 35.07423401 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 24352ms 44092 42.04940796 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 24831ms 43241 41.23783112 2K 1 实现DownloadCallback,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 25922ms 41422 39.50309753 8K 1 实现DownloadCallback,BufferedOutputStream 普通文件 下载 download_file(group_name, remote_filename, localFile) 59113472(约56M) 382ms 154747 147.5782394 无 1 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约57M) 369ms 160199 152.7776718 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约58M) 499ms 118702 113.2030487 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 59113472(约59M) 592ms 99853 95.22724152 2K 1 实现DownloadCallback,BufferedOutputStream 下载建议:100M内数据使用fastDFS提供的DownloadStream;大于1G的数据,使用BufferedOutputStream和DowloadStream
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值