- 论坛徽章:
- 0
|
本帖最后由 eighteencold 于 2013-06-13 11:45 编辑
发下代码吧,有遇到相同问题的可以参考一下:
重写StorageClient的query_file_info方法,在storageServer 置空之前, 获取它的InetSocketAddress拿到storageServer 的ip;新增getHttpURI
这们我就能在调用get_file_info方法之后再调用getHttpURI获取httpURL。- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.util.Arrays;
- import java.util.Properties;
- import org.csource.common.MyException;
- import org.csource.fastdfs.ClientGlobal;
- import org.csource.fastdfs.FileInfo;
- import org.csource.fastdfs.ProtoCommon;
- import org.csource.fastdfs.StorageServer;
- import org.csource.fastdfs.TrackerServer;
- public class StorageClient extends org.csource.fastdfs.StorageClient {
- public final static String confPath = "FileServer_client.conf";
-
- private static Properties prop = new Properties();
-
- private static boolean isInit=false;
-
- protected String uri;
- public StorageClient(TrackerServer trackerServer, StorageServer storageServer) {
- super(trackerServer, storageServer);
- init();
- }
- public StorageClient() {
- super();
- init();
- }
- public void init(){
- if(isInit){
- return;
- }
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- InputStream in = cl.getResourceAsStream(confPath);
- try {
- prop.load(in);
- in.close();
- isInit=true;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public String getHttpURI() {
- return uri;
- }
- @Override
- public FileInfo query_file_info(String group_name, String remote_filename)
- throws IOException, MyException {
- boolean bNewConnection = this.newUpdatableStorageConnection(group_name,
- remote_filename);
- Socket storageSocket = this.storageServer.getSocket();
- try {
- byte[] header;
- byte[] groupBytes;
- byte[] filenameBytes;
- byte[] bs;
- int groupLen;
- ProtoCommon.RecvPackageInfo pkgInfo;
- filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset);
- groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
- bs = group_name.getBytes(ClientGlobal.g_charset);
- Arrays.fill(groupBytes, (byte) 0);
- if (bs.length <= groupBytes.length) {
- groupLen = bs.length;
- } else {
- groupLen = groupBytes.length;
- }
- System.arraycopy(bs, 0, groupBytes, 0, groupLen);
- header = ProtoCommon.packHeader(
- ProtoCommon.STORAGE_PROTO_CMD_QUERY_FILE_INFO,
- +groupBytes.length + filenameBytes.length, (byte) 0);
- OutputStream out = storageSocket.getOutputStream();
- byte[] wholePkg = new byte[header.length + groupBytes.length
- + filenameBytes.length];
- System.arraycopy(header, 0, wholePkg, 0, header.length);
- System.arraycopy(groupBytes, 0, wholePkg, header.length,
- groupBytes.length);
- System.arraycopy(filenameBytes, 0, wholePkg, header.length
- + groupBytes.length, filenameBytes.length);
- out.write(wholePkg);
- pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
- ProtoCommon.STORAGE_PROTO_CMD_RESP, 3
- * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE
- + ProtoCommon.FDFS_IPADDR_SIZE);
- this.errno = pkgInfo.errno;
- if (pkgInfo.errno != 0) {
- return null;
- }
- long file_size = ProtoCommon.buff2long(pkgInfo.body, 0);
- int create_timestamp = (int) ProtoCommon.buff2long(pkgInfo.body,
- ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
- int crc32 = (int) ProtoCommon.buff2long(pkgInfo.body,
- 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
- String source_ip_addr = (new String(pkgInfo.body,
- 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE,
- ProtoCommon.FDFS_IPADDR_SIZE)).trim();
- return new FileInfo(file_size, create_timestamp, crc32,
- source_ip_addr);
- } catch (IOException ex) {
- if (!bNewConnection) {
- try {
- this.storageServer.close();
- } catch (IOException ex1) {
- ex1.printStackTrace();
- } finally {
- this.storageServer = null;
- }
- }
- throw ex;
- } finally {
- if (bNewConnection) {
- try {
- this.storageServer.close();
- } catch (IOException ex1) {
- ex1.printStackTrace();
- } finally {
- String ip = super.storageServer.getInetSocketAddress().getHostName();
- uri=prop.getProperty("httpURI."+ip);
- if(!uri.endsWith("/")){
- uri=uri+"/";
- }
- this.storageServer = null;
- }
- }
- }
- }
- }
复制代码 配置文件:- connect_timeout = 2
- network_timeout = 30
- charset = UTF-8
- http.tracker_http_port = 80
- http.anti_steal_token = no
- http.secret_key = FastDFS1234567890
- tracker_server = 192.168.15.201:22122
- httpURI.192.168.15.201=http://192.168.15.201
复制代码 |
|