前言
上一篇文章分布式文件管理系统FastDFS搭建教程(FastDFS+nginx+fastdfs-nginx-module)已经讲解了环境的搭建,接下来这个,是讲FASTDFS JAVA API的使用,解决JAVA端的调用问题。
MAVEN管理
- 自己构建的话,官方项目地址是
https://github.com/happyfish100/fastdfs-client-java - 直接下载Jar包的话,Maven仓库地址是
https://mvnrepository.com/artifact/org.csource/fastdfs-client-java/1.27-RELEASE
可以看到几个大仓库官方是没有这个jar包的,用友云居然有,可是不能直接用到,还是乖乖下载并安装jar包到maven库吧,1.27是最新版本。
这是一个bat安装脚本,保存为"install jar fastdfs.bat"即可,或者执行红色部分命令行导入jar包到本地maven库。
@echo off
echo blog.csdn.net/moshowgame
mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=1.27-RELEASE -Dpackaging=jar -Dfile=fastdfs-client-java-1.27-RELEASE.jar
pause
然后再项目中引入依赖
<!-- https://mvnrepository.com/artifact/org.csource/fastdfs-client-java -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-RELEASE</version>
</dependency>
conf文件
maven工程再项目的\src\main\resources
目录下新建一个fdfs_client.conf
内容如下
#配置文件
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port =8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server =192.168.103.118:22122
注:这里确实很奇葩,他就是要求这种配置,然后就ClientGlobal.init()
来初始化,对于我们整合配置进yml里面等等的,支持不算特别友好,有待改进。
controller调用
- 里面的ApiReturnUtil.success/error为自己封装的类,根据业务需求调整即可。
- conf_filenames是根据项目的
\src\main\resources
目录自动获取的。 - 只需要配置
tracker_server
即可。那些http.secret_key
什么的可以暂时不管。 this.getClass().getClassLoader().getResource("fdfs_client.conf").getPath()
可以直接获得sources下的conf文件,再加.replaceAll("%20"," ")
可以支持目录有空格的情况,默认是显示%20
@RestController
@RequestMapping("/upload")
@Log4j
public class UploadController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public Object upload(@RequestParam MultipartFile file) {
try {
if(file.isEmpty()){
return ApiReturnUtil.error("空上传");
}else{
//blog.csdn.net/moshowgame
String conf_filename = this.getClass().getClassLoader().getResource("fdfs_client.conf").getPath().replaceAll("%20"," ");
System.out.println(conf_filename);
String tempFileName = file.getOriginalFilename();
//fastDFS方式
ClientGlobal.init(conf_filename);
byte[] fileBuff = file.getBytes();
String fileId = "";
String fileExtName = tempFileName.substring(tempFileName.lastIndexOf("."));
//建立连接
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = tracker.getStoreStorage(trackerServer);
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
//设置元信息
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName", tempFileName);
metaList[1] = new NameValuePair("fileExtName", fileExtName);
metaList[2] = new NameValuePair("fileLength", String.valueOf(file.getSize()));
//上传文件,获得fileId
fileId = client.upload_file1(fileBuff, fileExtName, metaList);
//TODO 这里可以追加一些业务代码,例如上传成功后保存到upload_file表,统一进行上传文件管理之类
return ApiReturnUtil.success("文件上传成功",fileId);
}
} catch (Exception e) {
e.printStackTrace();
return ApiReturnUtil.error("上传错误:"+e.getMessage());
}
}
}
html网页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog.Csdn.Net/MoshowGame</title>
<meta charset="utf-8">
</head>
<form id="formData" enctype="multipart/form-data" method="post" action="http://localhost:8085/managesys/upload/upload">
<input type="id" name="file" type="mul" /><br/>
<input type="file" name="file" value="" /><br/>
<a class="weui-btn-area">
<input class="weui-btn weui-btn_primary" type="submit" value="去支付"/>
</a>
</form>