分布式檔案服務器FastDFS
什么是FastDFS
- 描述:是一個分布式檔案上傳工具(檔案資料庫 SQL)
- 作用:將檔案上傳到服務器

怎么用
理論概念
運行流程
1)存盤服務器定時上傳狀態到調度服務器
2)客戶端訪問調度器獲取可以使用的存盤服務器地址
3)客戶端將檔案上傳至指定存盤服務器
4)存盤生成fileID,并將檔案存入磁盤
5)回傳fileId給客戶端
fileID的結構
組名/磁盤路徑/兩級資料路徑/檔案名
撰寫步驟
a、導包fastdfs-client
<!--檔案服務器客戶端-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
</dependency>
b、撰寫核心組態檔,詳見application.yml
server:
port: 8081
fdfs:
connect-timeout: 600
so-timeout: 15000
tracker-list: 192.168.25.133:22122
thumb-image:
height: 150
width: 150
pool:
max-total: 50
#自定義屬性
fastLocalhost: http://192.168.25.133/
fastHost: 80
spring:
freemarker:
template-loader-path: /templaters/
suffix: .ftl
c、匯入配置類
CommonFileUtil.java
import com.github.tobato.fastdfs.domain.MateData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Set;
// 操作檔案的工具類
@Component
public class CommonFileUtil {
private final Logger logger = LoggerFactory.getLogger(CommonFileUtil.class);
@Autowired
private FastFileStorageClient storageClient;
/**
* MultipartFile型別的檔案上傳?
* @param file
* @return
* @throws IOException
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return getResAccessUrl(storePath);
}
/**
* 普通的檔案上傳
*
* @param file
* @return
* @throws IOException
*/
public String uploadFile(File file) throws IOException {
FileInputStream inputStream = new FileInputStream(file);
StorePath path = storageClient.uploadFile(inputStream, file.length(),
FilenameUtils.getExtension(file.getName()), null);
return getResAccessUrl(path);
}
/**
* 帶輸入流形式的檔案上傳
*
* @param is
* @param size
* @param fileName
* @return
*/
public String uploadFileStream(InputStream is, long size, String fileName) {
StorePath path = storageClient.uploadFile(is, size, fileName, null);
return getResAccessUrl(path);
}
/**
* 將一段文本檔案寫到fastdfs的服務器上
*
* @param content
* @param fileExtension
* @return
*/
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return getResAccessUrl(path);
}
/**
* 回傳檔案上傳成功后的地址名稱?
* @param storePath
* @return
*/
private String getResAccessUrl(StorePath storePath) {
String fileUrl = storePath.getFullPath();
return fileUrl;
}
/**
* 洗掉檔案
* @param fileUrl
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
logger.warn(e.getMessage());
}
}
public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
return getResAccessUrl(path);
}
}
fdfsConfig.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class FdfsConfig {
@Value("${fdfs.fastLocalhost}")
private String resHost;
@Value("${fdfs.fastHost}")
private String storagePort;
public String getResHost() {
return resHost;
}
public void setResHost(String resHost) {
this.resHost = resHost;
}
public String getStoragePort() {
return storagePort;
}
public void setStoragePort(String storagePort) {
this.storagePort = storagePort;
}
}
FdfsConfiguration.java
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
@Configuration
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfiguration {
}
d、修改啟動類
@Import(FdfsClientConfig.class)
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.rmi.registry.Registry;
@SpringBootApplication
@Import(FdfsClientConfig.class)
public class FastDFSApplication {
public static void main(String[] args) {
SpringApplication.run(FastDFSApplication.class,args);
}
}
e、撰寫檔案上傳代碼
前端:
檔案上傳兩要素:
提交方式:POST
配置enctype屬性:enctype=multiparty/form-data
后臺:
依賴注入(commonFileUtil,FdfsConfig)
撰寫處理請求的方法:
入參:MultiPartFile型別的引數,引數名=控制元件name屬性
處理邏輯:
1)判斷引數是否為空
2)上傳檔案,獲取fileID
3)拼接url = FdfsConfig.redHost+fileID
4)回傳界面,并顯示
@Controller
public class FileUploadController {
@Autowired
private CommonFileUtil commonFileUtil;
@Autowired
private FdfsConfig config;
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public String upload(MultipartFile files,Model model){
//判斷檔案是否為空
String url = null;
if(!files.isEmpty()){
try {
//進行檔案上傳,獲取fileId
String fileId = commonFileUtil.uploadFile(files);
//拼接圖片訪問路徑
url = config.getResHost()+fileId;
model.addAttribute("url", url);
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
檔案上傳需要安裝虛擬機,上傳到服務器,以下是教程
1.安裝VMware-workstation-full-14.1.2-8497320
安裝完成后激活,激活好后打開,主頁是這樣的:

2.還需要pinyougou-image-server檔案夾

3.在VMware Workstation中匯入虛擬機
點擊檔案>打開,

匯入此虛擬機

這里要修改以下設定,最好關閉防火墻

運行虛擬機,用戶名root,密碼itcast
注意密碼是不能顯示的,只能盲打
登錄成功,在cmd視窗能正確顯示這些,基本就能完成檔案上傳了,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264475.html
標籤:java
