자바 fastfds 작업 파일

9013 단어
fdfs_client. conf 는 다음 과 같 습 니 다.
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
tracker_server = 192.168.42.29:22122

 
package com.ctl.utils;

import com.ctl.utils.json.JsonDateValueProcessor;
import com.ctl.utils.json.JsonNumberValueProcessor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 

Title: FastFdsUtil

*

Description:

*

Copyright: Copyright (c) 2018

*

Company: www.ctl.com

* @author guolin * @version 1.0 * @date 2018-08-14 21:04 */ public class FastFdsUtil { static Logger logger = LoggerFactory.getLogger(FastFdsUtil.class); static JsonConfig jsonConfig = new JsonConfig(); static { try { String confPath = FastFdsUtil.class.getClassLoader().getResource("fdfs_client.conf").getPath(); ClientGlobal.init(confPath); Map map = new HashMap<>(); jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor()); jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateValueProcessor()); jsonConfig.registerJsonValueProcessor(Integer.class, new JsonNumberValueProcessor()); jsonConfig.registerJsonValueProcessor(Long.class, new JsonNumberValueProcessor()); jsonConfig.registerJsonValueProcessor(Byte.class, new JsonNumberValueProcessor()); jsonConfig.registerJsonValueProcessor(Float.class, new JsonNumberValueProcessor()); jsonConfig.registerJsonValueProcessor(Double.class, new JsonNumberValueProcessor()); } catch (IOException e) { logger.error("FastFds ", e); } catch (MyException e) { logger.error("FastFds ", e); } } /** * * @param localFilePath * @return */ public String[] fileLocalUpload(String localFilePath) { return fileLocalUpload(localFilePath, null); } /** * * @param localFilePath * @param nvp NameValuePair * @return */ public String[] fileLocalUpload(String localFilePath, NameValuePair[] nvp) { try { TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); String fileIds[] = storageClient.upload_file(localFilePath, "png", nvp); logger.info("fileIds.length={}", fileIds.length); logger.info(" ={}", fileIds[0]); logger.info(" ={} ", fileIds[1]); return fileIds; } catch (FileNotFoundException e) { logger.error("FastFds ", e); } catch (IOException e) { logger.error("FastFds ", e); } catch (MyException e) { logger.error("FastFds ", e); } return null; } /** * * @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png * @return */ public boolean fileServerDownload(String remoteFilename) { return fileServerDownload("group1", remoteFilename, System.getProperty("java.io.tmpdir") + UUID.randomUUID().toString() + ".tmp"); } /** * * @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png * @param localDownloadPath * @return */ public boolean fileServerDownload(String remoteFilename, String localDownloadPath) { return fileServerDownload("group1", remoteFilename, localDownloadPath); } /** * * @param groupName * @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png * @param localDownloadPath * @return */ public boolean fileServerDownload(String groupName, String remoteFilename, String localDownloadPath) { try { TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); byte[] b = storageClient.download_file(groupName, remoteFilename); IOUtils.write(b, new FileOutputStream(localDownloadPath)); return true; } catch (Exception e) { logger.error("FastFds ", e); return false; } } /** * * @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png * @return */ public FileInfo getFileInfo(String remoteFilename) { return getFileInfo("group1", remoteFilename); } /** * * @param groupName * @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png * @return */ public FileInfo getFileInfo(String groupName, String remoteFilename) { try { TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); FileInfo fi = storageClient.get_file_info(groupName, remoteFilename); System.out.println(fi.getSourceIpAddr()); System.out.println(fi.getFileSize()); System.out.println(fi.getCreateTimestamp()); System.out.println(fi.getCrc32()); return fi; } catch (Exception e) { logger.error("FastFds ", e); return null; } } public NameValuePair[] getFileMate(String remoteFilename) { return getFileMate("group1", remoteFilename); } public NameValuePair[] getFileMate(String groupName, String remoteFilename) { try { TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); NameValuePair nvps[] = storageClient.get_metadata(groupName, remoteFilename); for (NameValuePair nvp : nvps) { System.out.println(nvp.getName() + ":" + nvp.getValue()); } return nvps; } catch (Exception e) { logger.error("FastFds ", e); return null; } } /** * * @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png * @return */ public boolean delete(String remoteFilename) { return delete("group1", remoteFilename); } /** * * @param groupName * @param remoteFilename : M00/00/00/wKgqHVty2ZCAHaBvAAE0vHMtwgw608.png * @return */ public boolean delete(String groupName, String remoteFilename) { try { TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); int i = storageClient.delete_file("group1", remoteFilename); logger.info(i == 0 ? " " : " :" + i); return i == 0 ? true : false; } catch (Exception e) { logger.error("FastFds ", e); return false; } } public static void main(String[] args) { FastFdsUtil fastFdsTest = new FastFdsUtil(); String[] strings = fastFdsTest.fileLocalUpload("E:\\fAPP\\_20180814101019.png"); logger.info(JSONArray.fromObject(strings,jsonConfig).toString()); logger.info(JSONObject.fromObject(fastFdsTest.getFileInfo(strings[1]),jsonConfig).toString()); logger.info("downresult={}", fastFdsTest.fileServerDownload("group1", strings[1], "e:\\"+UUID.randomUUID()+"test.png")); logger.info("delresult={}", fastFdsTest.delete( strings[1])); } }

좋은 웹페이지 즐겨찾기