1.拉取鏡像
docker pull morunchang/fastdfs
2.啟動tracker
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
3.啟動storage
docker run -d --name storage --net=host -e TRACKER_SERVER=111.229.229.183:22122 -e TRACKER_IP=111.229.229.183:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
4.放開端口22122 和23000
firewall-cmd --query-port=22122/tcp
firewall-cmd --permanent --add-port=22122/tcp
firewall-cmd --reload
5.修改storage配置資訊
docker exec -it storage /bin/bash
cd /etc/nginx/conf
vi nginx.conf
nginx加上 user root;配置
cd /etc/fdfs/
-
storage.conf
group_name=group1 #組名是不是跟你通路路徑中的組名一緻?????
base_path=/fastdfs/storage #這個檔案路徑存不存在????
store_path0=/fastdfs/storage #這個檔案路徑存不存在????
tracker_server=192.168.59.131:22122 #ip對嗎???
http.server_port=80 #跟你在nginx.conf配置的艦艇端口号保持一緻,并且為了可以省略不寫端口号,建議改成80
-
tracker.conf:
base_path=/fastdfs/tracker #路徑存在嗎????
client.conf:
base_path=/fastdfs/tracker #這個檔案路徑存不存在????
tracker_server=192.168.59.131:22122 #ip正确嗎????
http.tracker_server_port=80 #端口号是否跟之前配置的一緻,統一起來
8.檢查mod_fastdfs.conf
tracker_server=192.168.59.131:22122 #ip?
url_have_group_name = true #這個是true???
store_path0=/fastdfs/storage #路徑存在嗎????
9.java代碼
(1).fastdfs.conf
connect_timeout=60
network_timeout=60
charset=UTF-8
http.tracker_http_port=80
tracker_server=外網ip:22122
(2).fastdfsFile 實體類封裝
public class FastDFSFile implements Serializable{
//名字
private String name;
//内容
private byte[] content;
//字尾
private String ext;
private String md5;
private String author;
public FastDFSFile(String name, byte[] content, String ext) {
this.name = name;
this.content = content;
this.ext = ext;
}
public FastDFSFile(String name, byte[] content, String ext, String md5, String author) {
this.name = name;
this.content = content;
this.ext = ext;
this.md5 = md5;
this.author = author;
}
省略get,set
(3).fastdfsUtil
import com.example.service_hi.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
public class FastDFSUtil {
static {
try {
//擷取fastdfs配置資訊
String fileName = new ClassPathResource(“fdfs_client.conf”).getPath();
//初始化用戶端
ClientGlobal.init(fileName);
}catch (Exception e){
e.printStackTrace();
}
}
public static String[] upload(FastDFSFile fastDFSFile) throws Exception {
//添加額外資訊
NameValuePair[] metaList = new NameValuePair[1];
metaList[0] = new NameValuePair("拍攝位址","天津");
//建立tracker用戶端
TrackerClient trackerClient = new TrackerClient();
//連接配接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//建立storage用戶端
StorageClient storageClient = new StorageClient(trackerServer,null);
//創傳檔案 參數一檔案内容 參數二檔案字尾 參數三額外資訊
String[] uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), metaList);
return uploads;
}
}
(4).controller
@RestController
@RequestMapping("/upload")
@CrossOrigin
public class FileUploadController {
@PostMapping
public Map<String,Object> upload(@RequestParam(value = “file”)MultipartFile file) throws Exception {
Map<String,Object> map = new HashMap<>();
//封裝檔案對象 參數一擷取檔案名稱,參數二檔案内容,參數三截取檔案字尾名
FastDFSFile fastDFSFile = new FastDFSFile(file.getOriginalFilename(),file.getBytes(), StringUtils.getFilenameExtension(file.getOriginalFilename()));
//上傳檔案
String[] uploads = FastDFSUtil.upload(fastDFSFile);
//拼接伺服器傳回的檔案位址
String url = "http://111.229.229.183/"+uploads[0]+"/"+uploads[1];
map.put("ok","ok");
map.put("data",url);
return map;
}
}