一、背景

  上篇博客我介绍了FastDFS的概念、原理以及安装步骤,这篇文章我们来聊一聊如何在java中使用FastDFSClient进行静态资源的上传。

二、使用步骤

  1.开发环境

    spring+springmvc+maven

  2.首先在maven的pom.xml中引入依赖fastdfs-client的依赖

1 <dependency>
2    <groupId>org.csource</groupId>
3    <artifactId>fastdfs-client-java</artifactId>
4    <version>5.0.4</version>
5 </dependency>

  3.接着我们来指定一个fastdfs-client.conf配置文件,里面内容如下:

    tracker_server=host:port(这里指trackerServer服务器的ip和端口)

  4.然后写一个单元测试类来测试服务

package com.hafiz.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;import com.taotao.common.utils.FastDFSClient;public class FastdfsTest {private static final String CONFIGLOCATION = "D:\\fastdfs_client.conf";@Testpublic void testUploadImg () {try {// 初始化全局配置。加载client配置文件
            ClientGlobal.init(CONFIGLOCATION);// 创建一个TrackerClient对象TrackerClient trackerClient = new TrackerClient();// 创建一个TrackerServer对象TrackerServer trackerServer = trackerClient.getConnection();// 声明一个StorageServer对象并初始为nullStorageServer storageServer = null;// 获得StorageClient对象StorageClient storageClient = new StorageClient(trackerServer, storageServer);// 直接调用StorageClient对象方法上传文件即可String[] result = storageClient.upload_file("D:\\Documents\\Downloads\\高圆圆2.jpg", "jpg", null);for(String item : result) {System.out.println(item);}trackerServer.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (MyException e) {e.printStackTrace();}}@Testpublic void fastDfsClientTest() {try {FastDFSClient client = new FastDFSClient(CONFIGLOCATION);String imgUrl = client.uploadFile("D:\\Documents\\Downloads\\高圆圆1.jpg", "jpg", null);System.out.println(imgUrl);} catch (Exception e) {e.printStackTrace();}}
}

  5.为了以后在项目中使用方便,我们不能每次都写这么一大串东西,所以我们来对该客户端进行以下封装:

package com.hafiz.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:")) {String url = this.getClass().getResource("/").getPath();url = url.substring(1);conf = conf.replace("classpath:", url);}ClientGlobal.init(conf);trackerClient = new TrackerClient();trackerServer = trackerClient.getConnection();storageServer = null;storageClient = new StorageClient1(trackerServer, storageServer);}public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {return storageClient.upload_file1(fileName, extName, metas);}public String uploadFile(String fileName, String extName) throws Exception {return storageClient.upload_file1(fileName, extName, null);}public String uploadFile(String fileName) throws Exception {return storageClient.upload_file1(fileName, null, null);}public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {return storageClient.upload_file1(fileContent, extName, metas);}public String uploadFile(byte[] fileContent, String extName) throws Exception {return storageClient.upload_file1(fileContent, extName, null);}public String uploadFile(byte[] fileContent) throws Exception {return storageClient.upload_file1(fileContent, null, null);}}

三、总结

  通过以上的步骤,我们就完成在java中使用fastdfs客户端进行静态资源上传的功能,这里面我们得到一个最重要的思想就是:DRY(Don't Repeat Yourself!),要有封装的思想。

使用java调用fastDFS客户端进行静态资源文件上传相关推荐

  1. 基于Java实现企业微信群聊机器人-文件上传+消息推送(保证可用)

    基于Java实现企业微信群聊机器人-文件上传+消息推送 1.pom.xml(注意版本尽量保持一致) 2.文件工具类 3.企业微信请求类 4.企业微信响应类 5.发送方法 6.企业微信机器人创建和配置 ...

  2. Java调用极光IM API注册用户并上传头像

    梗概:根据极光文档,java注册用户到极光IM分为两个步骤,第一步,调用文件上传接口,上传头像,获取返回的 media_id ,即资源文件id.第二步调动注册接口,带上头像数据即可.代码如下: 相关技 ...

  3. java web 上传图片漏洞_Web安全:文件上传漏洞

    原标题:Web安全:文件上传漏洞 一般将文件上传归类为直接文件上传与间接文件上传.直接文件上传就是服务器根本没有做任何安全过滤,导致攻击者可以直接上传小马文件及大马文件(如ASP.ASPX.PHP.J ...

  4. Java网络编程练习把本地的文件上传到服务器保存

    1)文件上传到客户端步骤: 1.创建客户端socket对象,指定要连接的服务器的ip地址和端口号 2.客户端socket对象调用getOutputStream方法,获取客户端的字节输出流对象 3.创建 ...

  5. java通过struts实现web中的文件上传

    单文件上传 fileupload.jsp <%@ page language="java" import="java.util.*" pageEncodi ...

  6. java ftp 下载慢_Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题

    废话不多说了,直接给大家贴代码了,具体代码如下所示: //文件上传 public static boolean uploadToFTP(String url,int port,String usern ...

  7. java ftp详解_Java实现FTP文件上传详解

    import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import java.io.Fi ...

  8. java代码实现多张图片/单张图片文件上传

    在项目中有这样的需求,有多个地方需要用到上传图片的功能,可以上传一张或多张照片.这时候自然想到了封装,来达到复用的目的.前后端分离开发,可以在上传文件后返回图片路径给前端,然后在最后整个需求完成后由前 ...

  9. 文件上传java前端怎么写_做一个文件上传,前端是ajax提交数据后台是java,这个错误怎么办...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 严重: Servlet.service() for servlet [dispatcherServlet] in context with path [/ ...

最新文章

  1. oracle统计信息收集的作用,oracle 11g 收集统计信息的新特点介绍
  2. python 回文链表
  3. 【职场】肺腑之言,给即将参加秋招的同学一点建议
  4. 鲁棒性、稳定性、非脆弱性
  5. js 利用事件委托解决mousedown中的click
  6. localhost 已拒绝连接_【Python】MongoDB数据库的连接和操作
  7. 第十七节(is-a 、is-like-a 、has-a,包和 import )
  8. Springboot整合一之Springboot整合RabbitMQ
  9. SLAM_怎么评价slam建图效果
  10. c语言习题答案解析,C语言习题及答案1
  11. Oracle函数写法和举例
  12. 2021年京东/淘宝/天猫/双十一红包最新优惠攻略,1111超级红包如何抢?
  13. 注塑过程中30个常见的问题及对策
  14. tf-encrypted介绍和代码示例
  15. 管理工具-生产管理系统Mes
  16. Cemu模拟铁拳TT2高清设置
  17. 宝讯网捷:拼多多主图怎么加文字?
  18. ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接
  19. Java调用opencv实现图片去水印
  20. 选择进入IT行业,会后悔吗?

热门文章

  1. 小学生 python教程-小学生都在学python
  2. python基础语法第10关作业-【python基础语法】第8天作业练习题
  3. python趣味编程10例-python趣味编程100例(99个)
  4. mybatis之ResultMap
  5. 测试时用存储过程来修改测试数据
  6. 10分钟内把永远跑不完的存储过程变为2秒跑完
  7. number to string - LeetCode【数字 = 字符串】
  8. Recover Binary Search Tree,恢复二叉排序树
  9. Java将小数转换为成百分比输出
  10. insert数据时,获取插入数据的id