一、背景

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

二、使用步骤

  1.开发环境

    spring+springmvc+maven

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

 <dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>5.0.4</version>
</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"; @Test
public void testUploadImg () {
try {
// 初始化全局配置。加载client配置文件
ClientGlobal.init(CONFIGLOCATION);
// 创建一个TrackerClient对象
TrackerClient trackerClient = new TrackerClient();
// 创建一个TrackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
// 声明一个StorageServer对象并初始为null
StorageServer 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();
}
} @Test
public 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 Web开发学习】Spring MVC文件上传

    [Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...

  2. java网络编程(7)——利用tcp实现文件上传

    其实客户端与服务端通讯的道理都是一样的,都是通过输入与输出这两个流,那么实现文件上传也就是同样的,客户端把文件读到文件流,服务端用文件流来接受,然后写到一个文件中,这样子就实现了文件上传,文件拷贝也是 ...

  3. springboot整合web开发(整合servlet、filter、listener、访问静态、文件上传)

    整合servlet 1.继承HttpServlet 2.添加@WebServlet注解 @WebServlet(name="FirstServlet",urlPatterns=&q ...

  4. [SAP ABAP开发技术总结]客户端文本文件、Excel文件上传下载

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. Java Web使用Html5 FormData实现多文件上传

    前一阵子,迭代一个线上的项目,其中有一个图片上传的功能,之前用的ajaxfileupload.js来实现上传的,不过由于ajaxfileupload.js,默认是单文件上传(虽然可以通过修改源码的方法 ...

  6. 从.Net到Java学习第十篇——Spring Boot文件上传和下载

    从.Net到Java学习系列目录 图片上传 Spring Boot中的文件上传就是Spring MVC中的文件上传,将其集成进来了. 在模板目录创建一个新的页面 profile/uploadPage. ...

  7. MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传

    前段时间做了几个关于图片.文件上传的Demo,使用客户端Query-File-Upload插件和服务端Badkload组件实现多文件异步上传,比如"MVC文件上传04-使用客户端jQuery ...

  8. asp.net core系列 69 Amazon S3 资源文件上传示例

    一.  上传示例 Install-Package AWSSDK.S3 -Version 3.3.104.10 using Amazon; using Amazon.Runtime; using Ama ...

  9. Java 利用Apache Commons Net 实现 FTP文件上传下载

    package woxingwosu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

随机推荐

  1. poj 2299 Ultra-QuickSort(归并排序,树状数组,线段树)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  2. Codeforces Round#416 Div.2

    A. Vladik and Courtesy 题面 At regular competition Vladik and Valera won a and b candies respectively. ...

  3. 工作中的小tips(持续更新)

    1.在工作的时候一定要留下痕迹,这样即使乙方抵赖,或者说领导认为你没干活的时候留下证据(电话没有微信,邮件之类的文字类有效果) 2.每天晚上下班之前将一天的工作总结一下,把第二天的工作给罗列出来,以方 ...

  4. AI模型训练/算法评估 测试员

  5. NoSQL数据库的分布式算法

    本文译自 Distributed Algorithms in NoSQL Databases 系统的可扩展性是推动NoSQL运动发展的的主要理由,包含了分布式系统协调,故障转移,资源管理和许多其他特性 ...

  6. 8.正则表达式和XPath

    1.使用正则表达式爬取内涵段子 import requests import re def loadPage(page): url = "http://www.neihan8.com/art ...

  7. Node.js之绝对选择(2018版)

    [这篇是很早期的文字,由于引用较广泛,担心误导,故按照现在的情形做一些修改] 几年前,完全放弃Asp.net,彻底脱离微软方向.Web开发,在公司团队中,一概使用Node.js.Mongodb.Git ...

  8. 彻底卸载Oracle database 12c教程

    1.WIN+R,然后输入regedit,回车:2.在注册表中,进入目录:\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services,删除所有以oracl ...

  9. 基于opencv3.0下的人脸检测和检测部分的高斯模糊处理

    如题 这里将任务分解为三大部分: 1.录播放视频 2.人脸检测 3.部分高斯模糊 其中重点放在人脸检测和部分高斯模糊上 1.录播放视频(以opencv中的VideoCapture类进行实现) 首先罗列 ...

  10. asp.net mvc 配置ckeditor4.x

    下载地址:https://ckeditor.com/ckeditor-4/download/ 一.使用方法: 1.在页面中引入ckeditor核心文件ckeditor.js 2.在使用编辑器的地方插入 ...