首先要转一个FastDFS,这个很难装。一般由运维人员安装。

git项目fastdfs-client-java由happy fish开发的,down下来后import到项目中maven install到本地仓库即可

package com.common.utils;

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);
}
} /**
* 测试
*/
public class FastDFSTest {
@Test
public void testFileUpload() throws Exception {
// 1、加载配置文件,配置文件中的内容就是tracker服务的地址。
ClientGlobal.init("D:\\Programmer_QY\\eclipse-jee-mars-2-win32-x86_64\\workspace\\e3-manager-web\\src\\main\\resources\\conf\\client.conf");
// 2、创建一个TrackerClient对象。直接new一个。
TrackerClient trackerClient = new TrackerClient();
// 3、使用TrackerClient对象创建连接,获得一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 4、创建一个StorageServer的引用,值为null
StorageServer storageServer = null;
// 5、创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 6、使用StorageClient对象上传图片。
// 扩展名不带“.”
String[] strings = storageClient.upload_file("G:\\秋雨\\相机\\小猪\\QQ图片20160501233001.jpg", "jpg",
null);
// 7、返回数组。包含组名和图片的路径。
for (String string : strings) {
System.out.println(string);
}
}
@Test
public void textFastDfsClient() throws Exception{
FastDFSClient fastDFSClient = new FastDFSClient("D:\\Programmer_QY\\eclipse-jee-mars-2-win32-x86_64\\workspace\\e3-manager-web\\src\\main\\resources\\conf\\client.conf");
String result = fastDFSClient.uploadFile("G:\\秋雨\\相机\\头像\\3226-150PR30508-52.jpg");
System.out.println(result);
}
}

客户端返回

浏览器访问

FastDFS图片服务器的更多相关文章

  1. java连接Fastdfs图片服务器上传失败的解决方法

    照着视频上做,但是却连接不了虚拟机linux上的图片服务器,估计是linux防火墙的问题(这个实在是神烦,前面有好几次连接不了都是因为linux防火墙),果不其然,关闭即可. Linux关闭防火墙的命 ...

  2. FastDFS图片服务器搭建

    *FastDFS图片服务器搭建准备:1.需要libfastcommon安装包 选择最新稳定版(libfastcommon-1.0.36.tar.gz)2.需要FastDFS安装包 选择最新稳定版(fa ...

  3. FastDFS图片服务器(分布式文件系统)学习。

    参考:https://blog.csdn.net/hiqingtian/article/details/79413471 https://blog.csdn.net/sinat_40399893/ar ...

  4. [转帖]FastDFS图片服务器单机安装步骤

    FastDFS图片服务器单机安装步骤 https://www.cnblogs.com/yuesf/p/11847103.html 前面已经讲 一张图秒懂微服务的网络架构,通过此文章可以了解FastDF ...

  5. fastdfs 图片服务器 使用java端作为客户端上传图片

    之前有说道搭建fastdfs作为图片服务器,但是没有说明如何真正在代码里调用,那么今天大致讲一下,如何使用java客户端进行上传 首先你得要有一个客户端,导入到eclipse中即可 git地址如下: ...

  6. FastDFS图片服务器java后台的简单调用

    工具类: package com.liveyc.common.fdfs; import org.apache.commons.io.FilenameUtils; import org.csource. ...

  7. 分享知识-快乐自己:FastDFS 图片服务器的搭建

    使用一台虚拟机来模拟,只有一个Tracker.一个Storage服务,配置nginx访问图片. 1):安装依赖包 yum -y install zlib zlib-devel pcre pcre-de ...

  8. FastDFS图片服务器单机安装步骤

    前面已经讲 一张图秒懂微服务的网络架构,通过此文章可以了解FastDFS组件中单机安装流程. 单机版架构图 以下为单机FastDFS安装步骤 一.环境准备 CentOS 7.X libfastcomm ...

  9. FastDFS图片服务器单机安装步骤(修订版)

    前面已经讲 一张图秒懂微服务的网络架构,通过此文章可以了解FastDFS组件中单机安装流程. 单机版架构图 以下为单机FastDFS安装步骤 一.环境准备 CentOS 7.X libfastcomm ...

随机推荐

  1. 学习Zookeeper需要了解的专业名词

    一.Zookeeper的集群角色 Leader:该角色是整个zookeeper集群工作机制中的核心 Follower:该角色是zookeeper集群状态的跟随者 Observer:在集群中充当观察者的 ...

  2. 闪回事务(Flashback Transaction)

    到目前为止,介绍的所有功能均不会直接将数据恢复为“以前”的样子.闪回查询只是查看,闪回数据归档只是延伸了闪回查询的时间窗口,闪回事务查询虽然提供了撤销SQL,但是否执行及如何执行还需要管理员进一步手动 ...

  3. python .bat

    传值给.bat os.system('%s %s %s %s %s' % ('image_dispose.bat', change_photo,dic['width'], '-resize', cha ...

  4. php 通过http user-agent判断是否为手机浏览器

    <?php/*** 判断是否是通过手机访问* @return bool 是否是移动设备    */public function isMobile() {  //判断手机发送的客户端标志  if ...

  5. Delphi锁定鼠标 模拟左右键 静止一会自动隐藏鼠标

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. Zen Cart屏蔽中文语言浏览器

    在根目录下的index.php前面加上以下这段代码即可: if(preg_match("/zh-cn/is",$_SERVER['HTTP_ACCEPT_LANGUAGE'])){ ...

  7. 读取、设置 php.ini配置文件(复制)

    1.ini_get()获取配置参数,ini_set()设置配置参数 复制代码 代码如下: <?phpecho ini_get('display_errors'); //1//动态修改php.in ...

  8. idea新建的项目,文件夹右键不能新建class

    一般情况下,新建的mave项目,通常没有XXX\src\main\java这个目录,如果手动创建,则又不能右键build与java相关的,强行建立的话,也不会被idea所识别,更不会被虚拟机编译执行. ...

  9. 仿netty线程池简化版本

    package com.hcxy.car.threadpools; import java.io.IOException; import java.nio.channels.Selector; imp ...

  10. 为什么iterator,foreach遍历时不能进行remove操作?除了一种情况可以这样(特殊情况)?

    Exception in thread "main" java.util.ConcurrentModificationException 并发修改异常引发的思考! 1 foreac ...