文档章节

简单 fastdfs java client 连接池

yysf
 yysf
发布于 2017/05/02 13:19
字数 633
阅读 141
收藏 1
package com.xyh.core.fastdfs;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.DownloadCallback;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.csource.fastdfs.UploadCallback;
import org.springframework.http.HttpHeaders;

public class FastDfsPool {
	//默认连接池大小
	public static int connection_size = 20;
	private static TrackerClient trackerClient;
	private static LinkedBlockingQueue<StorageClientInfo> storageClient1s = new LinkedBlockingQueue<StorageClientInfo>(
			connection_size);
	private static int current_index;

	static {
		try {
			String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();
			String fdfsClientConfigFilePath = classPath + File.separator + FileManagerConfig.CLIENT_CONFIG_FILE;
			ClientGlobal.init(fdfsClientConfigFilePath);
			trackerClient = new TrackerClient();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private FastDfsPool() {

	}

	public static String getFileExt(String local_filename) {
		int nPos = local_filename.lastIndexOf('.');
		if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
			return local_filename.substring(nPos + 1);
		}
		return "";
	}

	private static void createStorageClientInfo() {
		synchronized (trackerClient) {
			if (current_index < connection_size) {
				try {
					System.out.println("创建连接");
					TrackerServer server = trackerClient.getConnection();
					StorageClient1 storageClient1 = new StorageClient1(trackerClient.getConnection(), null);
					StorageClientInfo storageClientInfo = new StorageClientInfo(current_index, storageClient1, server);
//					System.out.println(storageClientInfo.id+"====================");
					if (storageClient1s.offer(storageClientInfo)) {
						current_index++;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static StorageClientInfo findStorageClient() {
		// 尝试获取一个有用的客户端连接信息
		StorageClientInfo clientInfo = storageClient1s.poll();
		if (clientInfo == null) {
//			System.out.println("没有找到可用的空闲连接");
			if (current_index < connection_size) {
				createStorageClientInfo();
			}else{
//				System.out.println("等待连接可用");
			}
			try {
				clientInfo=storageClient1s.poll(10,TimeUnit.SECONDS);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return clientInfo;
	}

	public static void downloadFile(String path, HttpServletRequest request, HttpServletResponse response) {
		StorageClientInfo client1 = findStorageClient();
		if(client1!=null){
			try {
				String type = request.getServletContext().getMimeType(path);
				if (type != null) {
					response.setContentType(type);
				}
				if (path.startsWith("/")) {
					path = path.substring(1);
				}
				FileDownLoad fileDownLoad = new FileDownLoad(response);
//				System.out.println("down load file "+path+" from "+client1.id+" server");
				client1.storageClient1.download_file1(path, fileDownLoad);
				if (fileDownLoad.out == null) {
					response.setStatus(404);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (MyException e) {
				e.printStackTrace();
			}finally {
				storageClient1s.offer(client1);
			}
		}else{
			try {
				response.sendError(500, "fastdfs file down server error");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @param in
	 *            文件输入流
	 * @param valuePairs
	 *            元信息
	 * @param local_filename
	 *            文件名
	 * @param size
	 *            文件大小
	 * @return 上传后的文件路径
	 * @throws MyException
	 * @throws IOException
	 */

	public static String upload(String local_filename, long size, InputStream in, NameValuePair[] valuePairs){

		StorageClientInfo client1 = findStorageClient();
		if(client1!=null){
			
			String file_ext_name = null;
			if (file_ext_name == null) {
				int nPos = local_filename.lastIndexOf('.');
				if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
					file_ext_name = local_filename.substring(nPos + 1);
				}
			}
			
			String path=null;
			try {
				path=client1.storageClient1.upload_file1(null, size, new FileUpload(in), file_ext_name, valuePairs);
			} catch (Exception e1) {
				e1.printStackTrace();
			}finally {
				try {
					if(client1.trackerServer.getSocket().isConnected()){
						storageClient1s.offer(client1);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return path;
		}
		return null;
		
		

	}

	private static class StorageClientInfo {
		public int id;
		public StorageClient1 storageClient1;
		public TrackerServer trackerServer;

		public StorageClientInfo(int id, StorageClient1 storageClient1, TrackerServer trackerServer) {
			this.storageClient1 = storageClient1;
			this.trackerServer = trackerServer;
			this.id=id;
		}

	}

	private static class FileUpload implements UploadCallback {
		private InputStream in;

		public FileUpload(InputStream in) {
			this.in = in;
		}

		public int send(OutputStream out) throws IOException {
			try {
				BufferedOutputStream bufferOut = new BufferedOutputStream(out);
				if (in instanceof FileInputStream) {
					in = new BufferedInputStream(in);
				}
				byte[] buff = new byte[4096];
				int off = -1;
				while ((off = in.read(buff)) != -1) {
					bufferOut.write(buff, 0, off);
				}
				bufferOut.flush();
			} catch (Exception e) {
				throw e;
			} finally {
				in.close();
			}
			return 0;
		}
	}

	private static class FileDownLoad implements DownloadCallback {
		private HttpServletResponse response;
		private OutputStream out;

		public FileDownLoad(HttpServletResponse response) {
			this.response = response;
		}

		@Override
		public int recv(long file_size, byte[] data, int bytes) {
			if (out == null) {
				response.setContentLengthLong(file_size);
				response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=864000");
				response.setHeader("Pragma", "Pragma");
				response.setDateHeader("Last-Modified", System.currentTimeMillis());
				try {
					out = response.getOutputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.write(data, 0, bytes);
				} catch (IOException e) {
					e.printStackTrace();
					return -1;
				}
			} else {
				return -1;
			}
			return 0;
		}

	}

	public static void deleteFile(String groupName, String remoteFileName) {
		StorageClientInfo client = findStorageClient();
		try {
			client.storageClient1.delete_file(groupName, remoteFileName);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}finally{
			storageClient1s.offer(client);
		}
	}

}

 

© 著作权归作者所有

共有 人打赏支持
yysf
粉丝 4
博文 12
码字总数 3652
作品 0
武汉
程序员
私信 提问
服务器后端开发系列——《实战FastDFS分布式文件系统》

1、FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS 内容:讲解FastDFS的背景、基本原理,并讲述基本的配置、部署和测试的内容。 2、FastDFS的配置、部署与API使用解读(2)...

晨曦之光
2012/04/24
327
0
fastdfs-client-java操作fastdfs5.0.4

一、在https://github.com/happyfish100/fastdfs-client-java 下载客户端,解压后并执行ant命令,在E:toolslibsfastdfsfastdfs-client-java-mastersrcbuild下会生成fastdfsclient.jar如图示 ......

chaun
2015/06/16
0
1
FastDFS服务器集群部署和集成客户端到SpringBoot

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题,同时也能做到在集群环...

maoqitian
2018/11/01
0
0
FastDFS并发诡异问题

我们现在有一个影像查看系统用的FastDFS问题,现在有个很诡异的问题,如果应用和tracker刚跑起来打开图片很多的项,服务器之间就会一直有建立的十几个socket连接,图片看不了;如果一开始查看...

AxelForm
2016/11/19
518
1
FastDFS的配置、部署与API使用解读(8)FastDFS多种文件上传接口详解

《Poechant的FastDFS的配置、部署与API使用教程》 FastDFS的配置、部署与API使用解读——(1)入门使用教程 FastDFS的配置、部署与API使用解读——(2)Java API:根据InputStream、文件名、...

晨曦之光
2012/04/24
2.9K
0

没有更多内容

加载失败,请刷新页面

加载更多

matlab-线性代数 简单方程组求根(有唯一解) 非齐次线性方程组:常数项不全为零

  matlab : R2018a 64bit     OS : Windows 10 x64 typesetting : Markdown    blog : my.oschina.net/zhichengjiu    gitee : gitee.com/zhichengjiu   code clearclc% 2x+......

志成就
36分钟前
2
0
Ubuntu 时间同步配置备忘

缘起 目前使用的 Ubuntu 18 下经常出现时间错误,查了下是默认读取 NTP 服务器的时候出现了 timeout,几次手工修改后一重启就故态复萌了,至于这个问题应该是怪机房还是 GFW,就不清楚了。 ...

郁也风
57分钟前
2
0
计算最佳线程数

计算出应该用于应用程序的理论最佳线程数有助于我们的程序的性能,应用程序运行时特征主要有CPU密集型工作和主要等待IO两种特征,或者是混合一起。 CPU 任务 threads = number of CPUs + 1 在...

woshixin
今天
3
0
搜索引擎(Solr-索引详解2)

学习目标 1.掌握SolrJ的使用。 2.掌握索引API 3.掌握结构化数据导入DIH SolrJ介绍 SolrJ是什么? Solr提供的用于JAVA应用中访问solr服务API的客户端jar。在我们的应用中引入solrj: <depende...

这很耳东先生
今天
3
0
待整理完--分享如何一个月在阿里云账户多了700元

服务器领券地址

吴伟祥
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部