FastDFS 工具类实现文件上传_02
一、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的更多相关文章
- java http工具类和HttpUrlConnection上传文件分析
利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...
- fastDFS与java整合文件上传下载
准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugins是必需要的,是maven用来编译的插件,第二个是maven打源码包的,可以不要. ...
- C# FTPClientHelper共公类 实现文件上传,目录操作,下载等动作
文档说明 本文档使用Socket通信方式来实现ftp文件的上传下载等命令的执行 1.基本介绍 由于最近的项目是客户端的程序,需要将客户端的图片文件[切图]-[打包]-[ftp上传],现在就差最后一步了 ...
- PHP异常处理类(文件上传提示)
知识点: 大部分时候我们的代码总有各种各样的bug,新手程序员(比如我)最经常的工作就是不停的报错和echo变量,一个好的异常处理类可以帮我们更快+更容易理解报错代码的问题,同时,异常处理还可以避免一 ...
- github 创建网络仓库 ,使用git工具将本地文件上传/删除 --- 心得
1.前言 使用 git做项目控制版本工具,当然,使用SVN也可以,但是,git让人感觉更先进一些,与GitHub结合,用起来很方便,服务端由官网控制. 而SVN分客户端和服务端,都是个人控制,因此, ...
- selenium+python自动化测试系列(二):AutoIt工具实现本地文件上传
AutoIt使用简单说明 AutoIt的安装这里就不在啰嗦,可以参考AutoIt安装或者自行搜索解决. 第一步:定位上传文件路径的文本框 这里举例说明,如何定位?如图 这里我们看到上传文件的类型是bu ...
- 19、文件上传与下载/JavaMail邮件开发
回顾: 一. 监听器 生命周期监听器 ServletRequestListener HttpSessionListener ServletContextListener 属性监听器 ServletRe ...
- 文件上传组件FileUpload 以及邮箱搭建JavaMail
文件上传与下载 1.1 文件上传 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 文件上传,要点: 前台: 1 ...
- JavaWeb 后端 <十四> 文件上传下载
1.文件上传与下载 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 1.1 文件上传 文件上传,要点: 前台: ...
随机推荐
- EL表达式取整问题
一般来说我们是无法实现EL表达式取整的.对于EL表达式的除法而言,他的结果是浮点型. 如:${6/7},他的结果是:0.8571428571428571.对于这个我们是无法直接来实现取整的. 这时就可 ...
- 洛谷——P2440 木材加工
https://www.luogu.org/problem/show?pid=2440#sub 题目背景 要保护环境 题目描述 题目描述: 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木 ...
- 编程算法 - 求1+2+...+n(模板类) 代码(C++)
求1+2+...+n(模板类) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whil ...
- linux网络启动报错
报错信息: shutting down interface eth0: error:device "eth0" (/org/freedsktop/networkMaager/Dev ...
- poj--1985--Cow Marathon(树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4424 Accepted: 2214 Case ...
- Linux就该这么学 20181009(第十二章 SAMBA)
参考链接https://www.linuxprobe.com Samba 跨平台的文件共享 linux-linux linux-windows /etc/samba/smb.conf 里面 []这个名 ...
- linux下服务启动脚本
#!/usr/bin/env python# -*- coding: utf-8 -*-# @File : deployment.py# @Author: Anthony.waa# @Date : 2 ...
- 【转】JS回调函数--简单易懂有实例
JS回调函数--简单易懂有实例 初学js的时候,被回调函数搞得很晕,现在回过头来总结一下什么是回调函数. 我们先来看看回调的英文定义:A callback is a function that is ...
- 移动端 华为手机 input中placeholder垂直居中失效
为一个app写了一个嵌套的提现页面,效果如下图 input给定宽高,给了line-heigh,在浏览器查看效果正常,placeholder内容以及光标显示都是垂直居中的, IOS显示正常, Andro ...
- [转]opencv学习资料
转自:http://blog.csdn.net/poem_qianmo/article/details/20537737 1:Mat imread(const string& filename ...