一、jar 包 

  jar包下载:https://pan.baidu.com/s/1nwkAHU5 密码:tlv6

  或者

  下载工程,安装到 maven 本地仓库

  工程下载:https://pan.baidu.com/s/1i6SIm3b 密码:jkjt

二、工具类代码

package com.common.fastFDS;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer; public class FastDFSClient
{ private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception
{
if (conf.contains("classpath:"))
{
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception
{
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
} public String uploadFile(String fileName) throws Exception
{
return uploadFile(fileName, null, null);
} public String uploadFile(String fileName, String extName) throws Exception
{
return uploadFile(fileName, extName, null);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception
{ String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
} public String uploadFile(byte[] fileContent) throws Exception
{
return uploadFile(fileContent, null, null);
} public String uploadFile(byte[] fileContent, String extName) throws Exception
{
return uploadFile(fileContent, extName, null);
}
}

 三、测试代码

@Test
public void testFastFDSClient() throws Exception
{
//1.创建一个配置文件 client.conf (该文件名任意)
//配置文件内容:tracker_server=192.168.**.**:22122 (你自己 FastDFS 服务器 ip+端口号) //2.创建 fastDFSClient 实例
FastDFSClient fastDFSClient = new FastDFSClient("E:/workspaces/conf/client.conf"); //3.执行图片上传,并返回图片路径
String string = fastDFSClient.uploadFile("C:/waiguoren.jpg");
System.out.println(string);
}

四、

 可以借鉴:http://www.cnblogs.com/fangwu/p/8367293.html

FastDFS 工具类实现文件上传_02的更多相关文章

  1. java http工具类和HttpUrlConnection上传文件分析

    利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...

  2. fastDFS与java整合文件上传下载

    准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugins是必需要的,是maven用来编译的插件,第二个是maven打源码包的,可以不要. ...

  3. C# FTPClientHelper共公类 实现文件上传,目录操作,下载等动作

    文档说明 本文档使用Socket通信方式来实现ftp文件的上传下载等命令的执行 1.基本介绍 由于最近的项目是客户端的程序,需要将客户端的图片文件[切图]-[打包]-[ftp上传],现在就差最后一步了 ...

  4. PHP异常处理类(文件上传提示)

    知识点: 大部分时候我们的代码总有各种各样的bug,新手程序员(比如我)最经常的工作就是不停的报错和echo变量,一个好的异常处理类可以帮我们更快+更容易理解报错代码的问题,同时,异常处理还可以避免一 ...

  5. github 创建网络仓库 ,使用git工具将本地文件上传/删除 --- 心得

    1.前言 使用  git做项目控制版本工具,当然,使用SVN也可以,但是,git让人感觉更先进一些,与GitHub结合,用起来很方便,服务端由官网控制. 而SVN分客户端和服务端,都是个人控制,因此, ...

  6. selenium+python自动化测试系列(二):AutoIt工具实现本地文件上传

    AutoIt使用简单说明 AutoIt的安装这里就不在啰嗦,可以参考AutoIt安装或者自行搜索解决. 第一步:定位上传文件路径的文本框 这里举例说明,如何定位?如图 这里我们看到上传文件的类型是bu ...

  7. 19、文件上传与下载/JavaMail邮件开发

    回顾: 一. 监听器 生命周期监听器 ServletRequestListener HttpSessionListener ServletContextListener 属性监听器 ServletRe ...

  8. 文件上传组件FileUpload 以及邮箱搭建JavaMail

     文件上传与下载 1.1 文件上传 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 文件上传,要点: 前台: 1 ...

  9. JavaWeb 后端 <十四> 文件上传下载

    1.文件上传与下载 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 1.1 文件上传 文件上传,要点: 前台: ...

随机推荐

  1. SpringBoot 配置 @ConfigurationProperties 与 @Value 区别

    一.SpringBoot 配置 @ConfigurationProperties 与 @Value 区别 配置文件 yml 还是 properties 他们都能获取到值: 如果说,我们只是在某个业务逻 ...

  2. LaTeX 加粗

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50997822 LaTeX中文本加粗的方 ...

  3. Unhandled 'error' event

    events.js: throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE at errnoException (net.js ...

  4. openfire 安装部署

    1. openfire安装和配置 本文介绍openfire 在linux上安装部署过程 linux上有两种安装方式,一个是RPM包方式.还有一个是tar.gz压缩包方式, 官方推荐採用RPM包方式,会 ...

  5. 深入浅出VGA和DVI接口

    由CrazyBingo修改…… 前言:目前显示器的主流接口是VGA.DVI以及HDMI,再加上一个比较少有的Displayport接口,一共可归为四类.相信不少消费者对这些接口认识并不多,特别是普通用 ...

  6. NSKeyedArchiver

    如果对象是NSString.NSDictionary.NSArray.NSData.NSNumber等类型,可以直接用NSKeyedArchiver进行归档和恢复 不是所有的对象都可以直接用这种方法进 ...

  7. 【刷题笔记】LeetCode 222. Count Complete Tree Nodes

    题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...

  8. luogu 1593 因子和

    因子和 题目描述 输入两个正整数a和b,求\(a^b\)的因子和.结果太大,只要输出它对9901的余数. 解法 基本算数定理,每一个数都可以被分解成一系列的素数的乘积,然后你可以分解出因数了. 如何求 ...

  9. Asp.net mvc中使用配置Unity

    第一步:添加unity.mvc 第二步:在添加之后会在app_start中生成UnityConfig.cs,UnityMvcActivator.cs 第三步:使用 第四步:效果展示

  10. 洛谷P1291 [SHOI2002]百事世界杯之旅(期望DP)

    题目描述 “……在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽奖活动,获得球星背包,随声听,更克赴日韩观看世界杯.还不赶 ...