博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FastDFS单机搭建以及java客户端Demo
阅读量:4692 次
发布时间:2019-06-09

本文共 6160 字,大约阅读时间需要 20 分钟。

 

参考了这几个搭建了FastDFS文件系统

主要是fastDFS,nginx,以及在nginx中加入fastDFS模块;这里只有一台服务器,所以搭建的是单机版的。

 

至于java客户端的包可以自己用maven编译,分分钟的事儿

编译完了打包到maven仓库中,直接用下面这个dependency

org.csource
fastdfs-client-java
1.27-SNAPSHOT

 

demo里面一共就引了这么几个:

commons-io
commons-io
1.4
org.csource
fastdfs-client-java
1.27-SNAPSHOT
junit
junit
4.11
test

 

 

客户端操作的Demo:

public class FileUtilsDemo {    private static StorageClient1 storageClient1 = null;    // 初始化FastDFS Client    static {        try {            ClassLoader classLoader = FileUtilsDemo.class.getClassLoader();            URL resource = classLoader.getResource("fastdfs.conf");            String path = resource.getPath();            System.out.println(path);            ClientGlobal.init(path);            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
metaList) { try { byte[] buff = IOUtils.toByteArray(new FileInputStream(file)); NameValuePair[] nameValuePairs = null; if (metaList != null) { nameValuePairs = new NameValuePair[metaList.size()]; int index = 0; for (Iterator
> iterator = metaList.entrySet().iterator(); iterator.hasNext();) { Map.Entry
entry = iterator.next(); String name = entry.getKey(); String value = entry.getValue(); nameValuePairs[index++] = new NameValuePair(name,value); } } return storageClient1.upload_file1(buff, FilenameUtils.getExtension(fileName),nameValuePairs); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取文件元数据 * @param fileId 文件ID * @return */ public static Map
getFileMetadata(String fileId) { try { NameValuePair[] metaList = storageClient1.get_metadata1(fileId); if (metaList != null) { HashMap
map = new HashMap
(); 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); IOUtils.copy(new ByteInputStream(content, content.length),fos); return 0; } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return -1; }}

客户端测试的Demo:

public class FastDFSClientTest {    /**     * 文件上传测试     */    @Test    public void testUpload() {        File file = new File("C:\\Users\\023b5bb5c9ea15ce4ed7034ebc003af33a87b2b8.jpg");        Map
metaList = new HashMap
(); metaList.put("width","1024"); metaList.put("height","768"); metaList.put("date","20161018"); String fid = FileUtilsDemo.uploadFile(file,file.getName(), metaList); System.out.println("upload local file " + file.getPath() + " ok, fileid=" + fid); } /** * 文件下载测试 */ @Test public void testDownload() { int r = FileUtilsDemo.downloadFile("group1/M00/00/00/cjdUkFo3fXeAAvUDAAFiCZy66os105.jpg", new File("DownloadFile_fid.jpg")); System.out.println(r == 0 ? "下载成功" : "下载失败"); } /** * 获取文件元数据测试 */ @Test public void testGetFileMetadata() { Map
metaList = FileUtilsDemo.getFileMetadata("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg"); for (Iterator
> iterator = metaList.entrySet().iterator(); iterator.hasNext();) { Map.Entry
entry = iterator.next(); String name = entry.getKey(); String value = entry.getValue(); System.out.println(name + " = " + value ); } } /** * 文件删除测试 */ @Test public void testDelete() { int r = FileUtilsDemo.deleteFile("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg"); System.out.println(r == 0 ? "删除成功" : "删除失败"); }}

配置文件:

connect_timeout = 10network_timeout = 30charset = UTF-8http.tracker_http_port = 8999http.anti_steal_token = nohttp.secret_key = FastDFS1234567890tracker_server = 192.168.88.520:22122

反正我这个文件里面写了注释,然后就报错了,我就把注释删除了就好了。一套下来接近2个小时可以搞定,搞不定问我,哈哈

 

转载于:https://www.cnblogs.com/tuhooo/p/8058424.html

你可能感兴趣的文章
Salesforce的基础用户界面定制
查看>>
vue里面的路由监听
查看>>
GJM : HTC vive
查看>>
mongodb 备份 还原 导出 导入
查看>>
Android 动画特效
查看>>
14. 类似正则表达式的字符处理问题
查看>>
bzoj1874: [BeiJing2009 WinterCamp]取石子游戏(博弈论+SG函数入门)
查看>>
使用apache自带日志分割模块rotatelogs,分割日志
查看>>
Linux下V4L2捕捉画面+H264压缩视频+帧缓冲显示视频————V4L2捕捉画面
查看>>
MySQL Database on Azure 支持 5.7 版本啦!
查看>>
补交进度条
查看>>
Kylin简介
查看>>
js正则表达式中=s.g表示什么意思
查看>>
caffe深度学习网络(.prototxt)在线可视化工具:Netscope Editor
查看>>
.net获取当前网址url(各种参数值)
查看>>
如何配置Log4net-Step by step
查看>>
VBA中find使用解释
查看>>
YSLOW 中文文摘
查看>>
python笔记(1)-关于我们应不应该继续学习
查看>>
python 读写XML
查看>>