一、背景

  上篇博客我介绍了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. Mongoose轻松搞定MongoDB,不要回调!

    MEAN开发栈中使用MongoDB的时候,与之配对的ORM最好的选择就是Mongoose了.本文就和大家一起探讨一下如何使用Mongoose来实现MongoDB的增删改查. 为了能使文中的例子更加生动 ...

  2. python(1)在windows8.1下搭建python27和python36环境

    去Python官网下载需要的Python版本 https://www.python.org/ 我下载的是下面这两个版本: Python 2.7.13 Python 3.6.1 安装Python27时, ...

  3. 20145232 《Java程序设计》课程总结

    学期总结 实验报告链接汇总 实验一 Java开发环境的熟悉 实验二 Java面向对象程序设计 实验三 敏捷开发与XP实践 实验四 Andoid开发基础 实验五 Java网络编程 代码托管链接 :Jav ...

  4. concurrent.future

    concurrent.future module provides a high-level interface for asynchronously executing callables. Bas ...

  5. noip第17课作业

    1.  召见骑士 [问题描述] 某王国有5位骑士,每位骑士都有自己的编号,且这个王国的编号都为奇数,分别为1,3,5,7,9,在国王召见他们之前他们都必须经过只能从一边进出的长廊,长廊的宽度只能坐一个 ...

  6. 修改linux swap空间的swappiness,降低对硬盘的缓存

    linux 会使用硬盘的一部分做为SWAP分区,用来进行进程调度--进程是正在运行的程序--把当前不用的进程调成‘等待(standby)‘,甚至‘睡眠(sleep)’,一旦要用,再调成‘活动(acti ...

  7. Spring 使用javaconfig配置

    除了使用xml,spring提供javaconfig配置,下面是简单的例子: 1.声明接口 /** * */ package com.junge.demo.spring.service; /** * ...

  8. MISL Learning

    https://www.codeguru.com/csharp/.net/net_general/il/article.php/c4635/MSIL-Tutorial.htm http://etuto ...

  9. C#通过字符串名称来调用对应字符串名称的方法

    前段时间在一个项目中,在一个地方要将函数所在类中的方法都调用一遍,但是否调用要通过配置文件中的内容决定.所以为了减少代码量,在网上查了相关信息,终于成功的将其应用到了项目中,我在这里将我做的一个简单例 ...

  10. Python入门 ---基础知识

    Python入门不知道这些你还是承早放弃吧!真的 Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言 ...