mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs -Dversion=1.2 -Dpackaging=jar -Dfile=F:\fastdfs_client_v1.20.jar
<dependency>
<groupId>org.csource.fastdfsgroupId>
<artifactId>fastdfsartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.2version>
dependency>
# connect timeout in seconds
# default value is 30s
connect_timeout=30
# network timeout in seconds
# default value is 30s
network_timeout=60
# the base path to store log files
base_path=/opt/module/FastDFS/repo/tracker
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=127.0.0.1:22122
#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info
# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false
# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600
# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false
# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false
# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf
#HTTP settings
http.tracker_server_port=80
#use "#include" directive to include HTTP other settiongs
##include http.conf
#nginx文件下载地址
FILE_SERVER_URL=http://127.0.0.1:8888/
<context:property-placeholder location="classpath:config/application.properties"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="52428800"/>
bean>
package com.hf.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 FastDFSClientUtils {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
public FastDFSClientUtils(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);
}
/**
* 功能描述:
* 〈
* fastDFS文件上传方法,直接返回文件在FastDFS上的路径(group1/M00/00/00/rBFs1lxlDwGAWLN7AFBHTyw8uXg903.xx)
* 〉
*
* @className: FastDFSClientUtils
* @author: hf
* @version: 1.0.0
* @date: 2019/2/13 17:55
* @param: [fileName 文件全路径 , extName 文件扩展名(不包含.), metas 文件扩展信息]
* @return: java.lang.String , java.lang.Integer>
*
* History:
*
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
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);
}
/**
* 功能描述:
* 〈
* fastDFS文件上传方法,直接返回文件在FastDFS上的路径(group1/M00/00/00/rBFs1lxlDwGAWLN7AFBHTyw8uXg903.xx)
* 〉
*
* @className: FastDFSClientUtils
* @author: hf
* @version: 1.0.0
* @date: 2019/2/13 17:56
* @param: [fileContent 文件的内容(字节数组), extName 文件扩展名, metas 文件扩展信息]
* @return: java.lang.String , java.lang.Integer>
*
* History:
*
*/
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);
}
}
package com.hf.fastdfs.controller;
import com.hf.utils.FastDFSClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class UploadController {
@Value("${FILE_SERVER_URL}")
private String file_server_url;
/**
* 功能描述:
* 〈
* 文件上传方法
* 〉
*
* @className: UploadController
* @author: hf
* @version: 1.0.0
* @date: 2019/2/13 18:21
* @param: [file 要上传的文件]
* @return: java.lang.Object,java.lang.Integer>
*
* History:
*
*/
@PostMapping("/upload")
public Object upload(MultipartFile file) {
//获取文件的全名称
String originalFilename = file.getOriginalFilename();
//获取文件拓展名
String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
try {
//获得文件上传客户端对象
FastDFSClientUtils clientUtils = new FastDFSClientUtils("classpath:config/fdfs_client.conf");
//上传文件至fastDFS服务器
String fileId = clientUtils.uploadFile(file.getBytes(), extName);
//获取已上传文件的全路径
String url = file_server_url + fileId;
return MsgUtils.success().add("url", url);
} catch (Exception e) {
e.printStackTrace();
return MsgUtils.fail();
}
}
}
//文件上传服务
app.service('uploadService', function ($http) {
//上传文件
this.uploadFile = function () {
let formData = new FormData();
//规定文件上传的name属性值必须是file
formData.append('file', file.files[0]);
return $http({
url: '/upload',
method: 'POST',
data: formData,
headers: {'Content-Type': undefined},
transformRequest: angular.identity
});
};
});
//文件上传前端控制器
app.controller('uploadController', ['$scope', '$http','uploadService', function ($scope, $http, uploadService) {
//文件上传
$scope.uploadFile = function () {
uploadService.uploadFile()
.then(result => {
//输出返回结果
console.log(result);
}, (result) => {
console.log(result);
});
};
}]);
<form role="form" enctype="multipart/form-data">
<div class="form-group">
<label>选择文件label>
<input type="file" class="form-control" name="file">
div>
<button class="btn btn-primary" type="button" ng-click="uploadFile()">开始上传button>
form>