fastdfs5.x Java客户端简单例子
下载源码, 使用maven编译并安装
https://github.com/happyfish100/fastdfs-client-java.git
新建maven工程,引入fastdfs-client-java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.xiaojf</groupId>
<artifactId>fastdfs-demo</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
</dependencies> </project>
属性文件 fastdfs-client.properties
## 客户端连接超时时间
fastdfs.connect_timeout_in_seconds = 5
## 客户端网络超时时间
fastdfs.network_timeout_in_seconds = 30
## 编码格式
fastdfs.charset = UTF-8 fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
## fastdfs tracker server 的http 端口号
fastdfs.http_tracker_http_port = 80
## fastdfs tracker server的地址
fastdfs.tracker_servers = 192.168.1.19:22122
上传
package cn.xiaojf.fastdfs; import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*; import java.io.File;
import java.io.IOException; /**
* 上传
* @author xiaojf 2017/6/28 15:59
*/
public class UploadDemo {
private static StorageClient1 client = null;
static {
try {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); if (trackerServer == null) {
throw new RuntimeException("获取Tracker server 发生异常");
} StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
client = new StorageClient1(trackerServer,storageServer);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
} /**
* 上传
* @param file 文件
* @author xiaojf 2017/6/28 16:37
*/
public static String upload(File file) throws IOException, MyException {
//指定额外的存储信息
NameValuePair[] nameValuePairs = new NameValuePair[1];
nameValuePairs[0] = new NameValuePair("filename",file.getName());
//上传文件
if (client == null) {
throw new RuntimeException("fastdfs 客户端未初始化");
}
return client.upload_file1(file.getAbsolutePath(), "exe", nameValuePairs);
} public static void main(String[] args) throws IOException, MyException {
File file = new File("c:/windows/system32/notepad.exe");
System.out.println(upload(file));
}
}
下载
package cn.xiaojf.fastdfs; import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*; import java.io.IOException; /**
* 下载
* @author xiaojf 2017/6/28 15:59
*/
public class DownloadDemo {
private static StorageClient1 client = null;
static {
try {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); if (trackerServer == null) {
throw new RuntimeException("获取Tracker server 发生异常");
} StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
client = new StorageClient1(trackerServer,storageServer);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
} /**
* 下载
* @param token fastdfs 上传后返回的字符串
* @author xiaojf 2017/6/28 16:37
*/
public static byte[] download(String token) throws IOException, MyException {
//获取上传时候,写入的文件信息
NameValuePair[] metadata1 = client.get_metadata1(token);
for (NameValuePair nameValuePair : metadata1) {
System.out.println(nameValuePair.getName() + " -> " + nameValuePair.getValue());
} if (client == null) {
throw new RuntimeException("fastdfs 客户端未初始化");
} //下载,返回文件字节数组
return client.download_file1(token);
} public static void main(String[] args) throws IOException, MyException {
System.out.println(DownloadDemo.download("group1/M00/00/00/wKgBE1lTagmAXsRVAAL0ADCuSWA576.exe").length);
}
}
fastdfs5.x Java客户端简单例子的更多相关文章
- Java RMI简单例子HelloWorld
Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...
- Mongodb系列- java客户端简单使用(CRUD)
Mongodb提供了很多的客户端: shell,python, java, node.js...等等. 以 java 为例实现简单的增删改查 pom文件: <dependencies> & ...
- java 多线程简单例子
实现线程的方式是一,继承Thread类,重写父类的run()方法 二,实现接口Runnable中的run()方法. 下面是简单的例子 例子1:银行存取钱问题 package com.direct.de ...
- java grpc简单例子
原文地址:http://blog.csdn.net/jek123456/article/details/53465033 用eclipse新建一个maven项目,Id信息如下 <groupId& ...
- RabbitMQ JAVA客户端调用例子
1.安装erlang 下载地址:http://www.erlang.org/downloads 设置ERLANG环境变量 2.安装RabbitMQ 下载地址: http://www.rabbitmq. ...
- java多态简单例子
/* 对象的多态性:动物 x = new 猫(); 函数的多态性:函数重载.重写 1.多态的体现 父类的引用指向了自己的子类对象 父类的引用也可以接收自己的对象 2.多态的前提 必须是类与类之间只有关 ...
- java 使用 comet4j 主动向客户端推送信息 简单例子
[背景] 今天,一个前端的师弟问我怎样做实时聊天窗口,我毫不犹豫地说:在前台定时访问服务端呀!师弟默默地百度了一番,最后告诉我,有一种技术是后服务端动推送信息给客户端的,这种技术的名字叫comet,我 ...
- java socket编程开发简单例子 与 nio非阻塞通道
基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
随机推荐
- oracle 插入每年每天数据
create or replace procedure PROC_P_ABC is v_sumday ; i ; v_calendar_date number :=null; v_day number ...
- [Java多线程]-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- Java常量池详解之Integer缓存
一个Java question,求输出结果 public class IntegerTest { public static void main(String[] args) { objPoolT ...
- 图:centrality
[定义]Centrality:图中每个节点v的相对重要度c(v),重要度是什么可根据具体应用定义. [估计方法] Degree centrality Betweenness centrality Cl ...
- NOIP模拟1
期望得分:100+100+100=300 实际得分:94+96+97=287 T1 #6090. 「Codeforces Round #418」尘封思绪 #include<cstdio> ...
- 2-sat基础题 uvalive 3211
蓝书325页的基础题 二分+2-sat //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using n ...
- bluebird -1 New Promise方法
new Promise new Promise(function(function resolve, function reject) resolver) -> Promise 创建一个Prom ...
- [转]C语言指针详解(经典,非常详细)
博文地址:https://blog.csdn.net/constantin_/article/details/79575638 写得很好啊! 这里写一下笔记好了 int p; //这是一个普通的整型变 ...
- 【洛谷 P2120】 [ZJOI2007]仓库建设(斜率优化)
题目链接 斜率优化+1,好吧不水分了. 玩具装箱那题以后再做,当作复习吧. \(f[i]=f[j]-(sum[i]-sum[j])*dis[i]+p[i]\) \(f[j]=-dis[i]*sum[j ...
- python-cookbook读书笔记
今天开始读<python-cookbook>,书里有许多python优雅的写法,可以作为python的一本进阶书. 感谢译者.项目地址: https://github.com/yidao6 ...