FastDFS图片服务器java客户端的使用

简介: FastDFS图片服务器java客户端的使用

FastDFS图片服务器的使用

一般使用java客户端进行上传图片

使用图片服务器需要相关的依赖,企业中有Maven私服,我们可以把FastDFS的java客户端依赖手动添加到Maven私服的第三方库。我们这里没有Maven私服,需要手动添加依赖,安装到本地仓库。

下载依赖工程项目:

链接:https://pan.baidu.com/s/1G2DGnrA1Wa1m2vTMFHsnvg

提取码:1ts6

20200401134307494.png

添加依赖

将下载好的工程导入eclipse以后,使用make install将项目安装到本地仓库

20200401134307494.png

创建itcast-fastdfs工程进行测试

20200401134307494.png

然后就可以在自己的项目POM中引入依赖。fastdfs_client工程的坐标,就是我们需要的依赖坐标

  <dependency>
      <groupId>fastdfs_client</groupId>
      <artifactId>fastdfs_client</artifactId>
      <version>1.25</version>
    </dependency>

java客户端上传步骤

1、加载配置文件,配置文件中的内容就是tracker服务的地址。

配置文件内容:tracker_server=192.168.37.161:22122 (这是FastDFS服务器部署的ip、端口)

2、创建一个TrackerClient对象。直接new一个。

3、使用TrackerClient对象创建连接,获得一个TrackerServer对象。

4、创建一个StorageServer的引用,值为null

5、创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用

6、使用StorageClient对象上传图片。

7、返回数组。包含组名和图片的路径。

创建tracker.conf

20200401134307494.png

内容为:

tracker_server=ip+端口

这是FastDFS服务器部署的ip、端口

20200401134307494.png

编写测试代码

package com.taotao.fastdfs;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;
public class TestFastDFD {
  @Test
  public void voloadFile() throws Exception{
    // TODO Auto-generated method stub
    //1.添加jar包
    //2.创建一个配置文件。配置tracker服务器地址
    //3.加载配置文件
    //4.创建一个trackerClient对象
    ClientGlobal.init("D:/eclipse-workspace/taotao-manager-web/src/main/resources/resource/client.conf");
    TrackerClient trackerClient=new TrackerClient();
    //5.创建一个TranClient对象获取trackServer对象
    TrackerServer trackerServer=trackerClient.getConnection();
    //6.创建一个storageServer的引用 null就可以
    StorageServer storageServer = null;
    //7.创建一个storageClient对象。trackerserver、StorageServer两个参数
    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
    //8.使用storageClient上传文件
    String[] result = storageClient.upload_file("C:/Users/Administrator/Desktop/a4.jpg", "jpg", null);
    // 7. 打印返回的结果,String数组
    for (String s : result) {
      System.out.println(s);
    }
  }
}

控制台的输出路径为:

20200401134307494.png

这是存入fastDFS后返回的存储路径信息,之后我们将通过这个信息去查看图片。

之前的测试代码写法比较繁琐,这里提供一个FastDFS的客户端工具类:

package com.taotao.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;
  /**
   * 
   * @Title:  FastDFSClient   
   * @Description:    TODO  
   * @param:  @param conf fastDFS配置文件路径
   * @param:  @throws Exception  
   * @throws
   */
  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;
  }
  /**
   * 上传文件名
   * @Title: uploadFile   
   * @Description: TODO
   * @param: @param fileName
   * @param: @return
   * @param: @throws Exception      
   * @return: String      
   * @throws
   */
  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 test() throws Exception {
    //传入配置文件名
    FastDFSClient client=new FastDFSClient("D:/eclipse-workspace/taotao-manager-web/src/main/resources/resource/client.conf"); 
    System.out.println(client.uploadFile("C:/Users/Administrator/Desktop/a5.jpg")); //传入所要上传的文件,输出返回的路径信息
  }

控制台打印:

20200401134307494.png




相关文章
|
4月前
|
Java
Java开发实现图片URL地址检验,如何编码?
【10月更文挑战第14天】Java开发实现图片URL地址检验,如何编码?
142 4
|
3月前
|
Java
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
116 9
|
1月前
|
存储 开发工具 git
[Git] 深入理解 Git 的客户端与服务器角色
Git 的核心设计理念是分布式,每个仓库既可以是客户端也可以是服务器。通过 GitHub 远程仓库和本地仓库的协作,Git 实现了高效的版本管理和代码协作。GitHub 作为远程裸仓库,存储项目的完整版本历史并支持多客户端协作;本地仓库则通过 `.git` 文件夹独立管理版本历史,可在离线状态下进行提交、回滚等操作,并通过 `git pull` 和 `git push` 与远程仓库同步。这种分布式特性使得 Git 在代码协作中具备强大的灵活性和可靠性。
65 18
[Git] 深入理解 Git 的客户端与服务器角色
|
4月前
|
Java
Java开发实现图片地址检验,如果无法找到资源则使用默认图片,如何编码?
【10月更文挑战第14天】Java开发实现图片地址检验,如果无法找到资源则使用默认图片,如何编码?
104 2
|
2月前
|
存储 人工智能 自然语言处理
ChatMCP:基于 MCP 协议开发的 AI 聊天客户端,支持多语言和自动化安装 MCP 服务器
ChatMCP 是一款基于模型上下文协议(MCP)的 AI 聊天客户端,支持多语言和自动化安装。它能够与多种大型语言模型(LLM)如 OpenAI、Claude 和 OLLama 等进行交互,具备自动化安装 MCP 服务器、SSE 传输支持、自动选择服务器、聊天记录管理等功能。
331 15
ChatMCP:基于 MCP 协议开发的 AI 聊天客户端,支持多语言和自动化安装 MCP 服务器
|
3月前
|
开发框架 .NET C#
在 ASP.NET Core 中创建 gRPC 客户端和服务器
本文介绍了如何使用 gRPC 框架搭建一个简单的“Hello World”示例。首先创建了一个名为 GrpcDemo 的解决方案,其中包含一个 gRPC 服务端项目 GrpcServer 和一个客户端项目 GrpcClient。服务端通过定义 `greeter.proto` 文件中的服务和消息类型,实现了一个简单的问候服务 `GreeterService`。客户端则通过 gRPC 客户端库连接到服务端并调用其 `SayHello` 方法,展示了 gRPC 在 C# 中的基本使用方法。
70 5
在 ASP.NET Core 中创建 gRPC 客户端和服务器
|
3月前
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
197 4
|
4月前
|
Java 数据安全/隐私保护
Java ffmpeg 实现视频加文字/图片水印功能
【10月更文挑战第22天】在 Java 中使用 FFmpeg 实现视频加文字或图片水印功能,需先安装 FFmpeg 并添加依赖(如 JavaCV)。通过构建 FFmpeg 命令行参数,使用 `drawtext` 滤镜添加文字水印,或使用 `overlay` 滤镜添加图片水印。示例代码展示了如何使用 JavaCV 实现文字水印。
337 1
|
4月前
|
网络协议 Unix Linux
一个.NET开源、快速、低延迟的异步套接字服务器和客户端库
一个.NET开源、快速、低延迟的异步套接字服务器和客户端库
129 4
|
4月前
|
运维 Java Linux
【运维基础知识】Linux服务器下手写启停Java程序脚本start.sh stop.sh及详细说明
### 启动Java程序脚本 `start.sh` 此脚本用于启动一个Java程序,设置JVM字符集为GBK,最大堆内存为3000M,并将程序的日志输出到`output.log`文件中,同时在后台运行。 ### 停止Java程序脚本 `stop.sh` 此脚本用于停止指定名称的服务(如`QuoteServer`),通过查找并终止该服务的Java进程,输出操作结果以确认是否成功。
188 1

热门文章

最新文章