网站首页/ 信息中心/ 档案百科/

数字档案馆系统从零搭建:Java + FastDFS实战指南

发布时间:2026年08月18日 07:10:04 浏览量:0

一、环境与工具准备

操作系统:Ubuntu 20.04 LTS Server

核心软件清单:

1.1 基础环境部署

更新系统并安装编译工具:

``` sudo apt update sudo apt install -y gcc g++ make perl wget vim ```

安装JDK:

``` sudo apt install -y openjdk-8-jdk java -version 验证安装 ```

安装MySQL:

``` sudo apt install -y mysql-server sudo mysql_secure_installation 按提示设置root密码 ```

二、FastDFS分布式文件系统部署

2.1 安装libfastcommon

下载并编译安装:

``` wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz tar -zxvf V1.0.43.tar.gz cd libfastcommon-1.0.43/ sudo ./make.sh sudo ./make.sh install ```

2.2 安装FastDFS

下载FastDFS源码:

``` wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz tar -zxvf V6.06.tar.gz cd fastdfs-6.06/ sudo ./make.sh sudo ./make.sh install ```

2.3 配置Tracker服务器

创建配置文件目录:

``` sudo cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf sudo mkdir -p /opt/fastdfs/tracker ```

编辑tracker配置文件:

``` sudo vim /etc/fdfs/tracker.conf ```

修改以下配置项:

``` base_path=/opt/fastdfs/tracker bind_addr=0.0.0.0 port=22122 http.server_port=8080 ```

启动Tracker服务:

``` sudo fdfs_trackerd /etc/fdfs/tracker.conf start sudo netstat -tunlp | grep fdfs 验证端口22122已监听 ```

2.4 配置Storage服务器

创建存储目录:

``` sudo mkdir -p /opt/fastdfs/storage sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf ```

编辑storage配置文件:

``` sudo vim /etc/fdfs/storage.conf ```

修改以下配置项:

``` group_name=group1 base_path=/opt/fastdfs/storage store_path0=/opt/fastdfs/storage tracker_server=你的服务器IP:22122 http.server_port=8888 ```

启动Storage服务:

``` sudo fdfs_storaged /etc/fdfs/storage.conf start sudo fdfs_monitor /etc/fdfs/storage.conf 查看状态 ```

三、Nginx与FastDFS模块集成

3.1 安装Nginx

下载并安装Nginx:

``` wget http://nginx.org/download/nginx-1.18.0.tar.gz tar -zxvf nginx-1.18.0.tar.gz cd nginx-1.18.0/ sudo ./configure --prefix=/usr/local/nginx sudo make sudo make install ```

3.2 安装fastdfs-nginx-module

下载模块:

``` wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz tar -zxvf V1.22.tar.gz ```

重新编译Nginx:

``` cd nginx-1.18.0/ sudo ./configure --add-module=/path/to/fastdfs-nginx-module-1.22/src sudo make sudo make install ```

3.3 配置Nginx

复制FastDFS配置文件:

``` sudo cp /path/to/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/ sudo vim /etc/fdfs/mod_fastdfs.conf ```

修改配置:

``` tracker_server=你的服务器IP:22122 url_have_group_name=true store_path0=/opt/fastdfs/storage ```

配置Nginx虚拟主机:

``` sudo vim /usr/local/nginx/conf/nginx.conf ```

数字档案馆系统从零搭建:Java + FastDFS实战指南

在http块内添加:

``` server { listen 80; server_name localhost; location ~ /group[0-9]/M00 { root /opt/fastdfs/storage/data; ngx_fastdfs_module; } location / { root html; index index.html index.htm; } } ```

启动Nginx:

``` sudo /usr/local/nginx/sbin/nginx ```

四、Java后端服务开发

4.1 创建Spring Boot项目

使用Maven创建项目:

``` mvn archetype:generate -DgroupId=com.digitalarchive -DartifactId=digital-archive -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ```

pom.xml依赖配置:

``` org.springframework.boot spring-boot-starter-web 2.5.4 org.springframework.boot spring-boot-starter-data-jpa 2.5.4 mysql mysql-connector-java 8.0.26 net.oschina.zcx7878 fastdfs-client-java 1.27.0.0 ```

4.2 数据库设计

创建数据库表:

``` CREATE DATABASE digital_archive DEFAULT CHARACTER SET utf8mb4; USE digital_archive; CREATE TABLE archive_file ( id BIGINT PRIMARY KEY AUTO_INCREMENT, file_name VARCHAR(255) NOT NULL, file_size BIGINT NOT NULL, file_type VARCHAR(50), fastdfs_path VARCHAR(500) NOT NULL, upload_time DATETIME DEFAULT CURRENT_TIMESTAMP, upload_user VARCHAR(100), description TEXT, INDEX idx_upload_time (upload_time), INDEX idx_file_type (file_type) ); ```

4.3 FastDFS客户端配置

创建配置文件fdfs_client.conf:

``` connect_timeout = 30 network_timeout = 60 charset = UTF-8 http.tracker_http_port = 8080 tracker_server = 你的服务器IP:22122 ```

4.4 核心上传代码实现

FileService.java:

``` @Service public class FileService { @Value("${fdfs.config.path}") private String fdfsConfigPath; public String uploadFile(MultipartFile file) throws Exception { // 初始化客户端 ClientGlobal.init(fdfsConfigPath); TrackerClient trackerClient = new TrackerClient(); TrackerServer trackerServer = trackerClient.getConnection(); StorageServer storageServer = null; StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer); // 获取文件扩展名 String originalFilename = file.getOriginalFilename(); String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); // 上传文件 String fileId = storageClient.upload_file1(file.getBytes(), extName, null); // 构建访问URL String fileUrl = "http://你的服务器IP/group1/" + fileId; // 保存文件元数据到数据库 saveFileMetadata(originalFilename, file.getSize(), extName, fileId, fileUrl); return fileUrl; } private void saveFileMetadata(String fileName, long fileSize, String fileType, String fastdfsPath, String url) { // 数据库保存逻辑 } } ```

4.5 文件下载接口

FileController.java:

``` @RestController @RequestMapping("/api/file") public class FileController { @Autowired private FileService fileService; @PostMapping("/upload") public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { try { String fileUrl = fileService.uploadFile(file); return ResponseEntity.ok(fileUrl); } catch (Exception e) { return ResponseEntity.status(500).body("上传失败: " + e.getMessage()); } } @GetMapping("/download/{fileId}") public void downloadFile(@PathVariable String fileId, HttpServletResponse response) { try { byte[] fileData = fileService.downloadFile(fileId); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileId, "UTF-8") + "\""); response.getOutputStream().write(fileData); response.getOutputStream().flush(); } catch (Exception e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } } ```

五、系统部署与验证

5.1 应用打包部署

打包Spring Boot应用:

``` mvn clean package -DskipTests ```

启动应用:

``` java -jar target/digital-archive-1.0.0.jar --server.port=8081 ```

5.2 功能验证测试

使用curl测试文件上传:

``` curl -X POST -F "file=@/path/to/test.pdf" http://localhost:8081/api/file/upload ```

预期返回:

``` http://你的服务器IP/group1/M00/00/00/test.pdf ```

浏览器访问返回的URL验证文件可正常下载。

5.3 监控与维护

查看FastDFS状态:

``` sudo fdfs_monitor /etc/fdfs/storage.conf ```

检查存储空间使用情况:

``` df -h /opt/fastdfs/storage ```

查看Nginx访问日志:

``` tail -f /usr/local/nginx/logs/access.log ```

六、关键问题排查

6.1 文件上传失败排查

检查Tracker服务状态:

``` sudo systemctl status fdfs_trackerd ```

检查Storage服务状态:

``` sudo systemctl status fdfs_storaged ```

检查防火墙设置:

``` sudo ufw status sudo ufw allow 22122/tcp 开放Tracker端口 sudo ufw allow 23000/tcp 开放Storage端口 sudo ufw allow 80/tcp 开放HTTP端口 ```

6.2 文件无法访问排查

检查Nginx配置:

``` sudo /usr/local/nginx/sbin/nginx -t ```

检查FastDFS模块配置:

``` cat /etc/fdfs/mod_fastdfs.conf | grep tracker_server ```

验证文件是否存在:

``` ls -la /opt/fastdfs/storage/data/00/00/ ```
太原档案数字化服务流程是怎样的?大概需要多少钱?
太原档案数字化服务流程是怎样的?大概需要多少钱?
太原档案数字化服务通常包含需求分析、方案制定、现场整理、扫描加工、数据挂接、验收交付等核心流程,费用根据档案数量、纸张状况、数字化标准等因素综合计算,2026年市场价格范围大致在每页0.5元至2元之间...
2026年08月18日 07:10:04
微信咨询
电话联系
QQ客服
微信咨询一对一服务
服务热线: 028-8744 4417
QQ客服: 2305721818