标签:extension xxx 上传文件 metadata metal shm ada des dig
原文链接:http://blog.csdn.net/xyang81/article/details/52847311
Java客户端源代码和jar:链接:http://pan.baidu.com/s/1jHIwtsq 密码:n757
我将官方提供的sdk封装了一个工具类,将工具类导入工程即可使用,如下所示:
package com.digi_zones.fdfs; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtil; import org.csource.common.NameValuePair; import org.csource.fastdfs.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * <p>Description: FastDFS文件上传下载工具类 </p> * <p>Copyright: Copyright (c) 2016</p> * * @author yangxin * @version 1.0 * @date 2016/10/19 */ public class FastDFSClient { private static final String CONFIG_FILENAME = "src/main/resources/fdfs/fdfs_client.conf"; private static StorageClient1 storageClient1 = null; // 初始化FastDFS Client static { try { ClientGlobal.init(CONFIG_FILENAME); TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group); TrackerServer trackerServer = trackerClient.getConnection(); if (trackerServer == null) { throw new IllegalStateException("getConnection return null"); } StorageServer storageServer = trackerClient.getStoreStorage(trackerServer); if (storageServer == null) { throw new IllegalStateException("getStoreStorage return null"); } storageClient1 = new StorageClient1(trackerServer,storageServer); } catch (Exception e) { e.printStackTrace(); } } /** * 上传文件 * @param file 文件对象 * @param fileName 文件名 * @return */ public static String uploadFile(File file, String fileName) { return uploadFile(file,fileName,null); } /** * 上传文件 * @param file 文件对象 * @param fileName 文件名 * @param metaList 文件元数据 * @return */ public static String uploadFile(File file, String fileName, Map<String,String> metaList) { try { byte[] buff = IOUtil.toByteArray(new FileInputStream(file)); NameValuePair[] nameValuePairs = null; if (metaList != null) { nameValuePairs = new NameValuePair[metaList.size()]; int index = 0; for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) { Map.Entry<String,String> entry = iterator.next(); String name = entry.getKey(); String value = entry.getValue(); nameValuePairs[index++] = new NameValuePair(name,value); } } return storageClient1.upload_file1(buff,FileUtils.getExtension(fileName),nameValuePairs); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取文件元数据 * @param fileId 文件ID * @return */ public static Map<String,String> getFileMetadata(String fileId) { try { NameValuePair[] metaList = storageClient1.get_metadata1(fileId); if (metaList != null) { HashMap<String,String> map = new HashMap<String, String>(); for (NameValuePair metaItem : metaList) { map.put(metaItem.getName(),metaItem.getValue()); } return map; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 删除文件 * @param fileId 文件ID * @return 删除失败返回-1,否则返回0 */ public static int deleteFile(String fileId) { try { return storageClient1.delete_file1(fileId); } catch (Exception e) { e.printStackTrace(); } return -1; } /** * 下载文件 * @param fileId 文件ID(上传文件成功后返回的ID) * @param outFile 文件下载保存位置 * @return */ public static int downloadFile(String fileId, File outFile) { FileOutputStream fos = null; try { byte[] content = storageClient1.download_file1(fileId); fos = new FileOutputStream(outFile); IOUtil.copy(content,fos); return 0; } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return -1; } }
Java客户端配置文件(fdfs_client.conf):
connect_timeout = 10 # 连接tracker服务器超时时长
network_timeout = 30 # socket连接超时时长
charset = UTF-8 # 文件内容编码
http.tracker_http_port = 8888 # tracker服务器端口
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.0.200:22122 # tracker服务器IP和端口(可以写多个)
#tracker_server = xxxx:xxx
Java客户端文件上传、下载、删除和元数据获取测试:
package com.digi_zones.fdfs; import org.junit.Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * <p>Description: </p> * <p>Copyright: Copyright (c) 2016</p> * * @author yangxin * @version 1.0 * @date 2016/10/19 */ public class FastDFSClientTest { /** * 文件上传测试 */ @Test public void testUpload() { File file = new File("C:\\Users\\yangfang\\Pictures\\angularjs_share.jpg"); Map<String,String> metaList = new HashMap<String, String>(); metaList.put("width","1024"); metaList.put("height","768"); metaList.put("author","杨信"); metaList.put("date","20161018"); String fid = FastDFSClient.uploadFile(file,file.getName(),metaList); System.out.println("upload local file " + file.getPath() + " ok, fileid=" + fid); //上传成功返回的文件ID: group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg } /** * 文件下载测试 */ @Test public void testDownload() { int r = FastDFSClient.downloadFile("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg", new File("DownloadFile_fid.jpg")); System.out.println(r == 0 ? "下载成功" : "下载失败"); } /** * 获取文件元数据测试 */ @Test public void testGetFileMetadata() { Map<String,String> metaList = FastDFSClient.getFileMetadata("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg"); for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) { Map.Entry<String,String> entry = iterator.next(); String name = entry.getKey(); String value = entry.getValue(); System.out.println(name + " = " + value ); } } /** * 文件删除测试 */ @Test public void testDelete() { int r = FastDFSClient.deleteFile("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg"); System.out.println(r == 0 ? "删除成功" : "删除失败"); } }
标签:extension xxx 上传文件 metadata metal shm ada des dig
原文地址:http://www.cnblogs.com/hd3013779515/p/6884268.html