准备环境:
- windows、java环境、数据库
- linux、centosOS系统、nginx环境
开始~~~~~开车:
1、linux环境配置
首先需要安装一些插件:
(1)下载安装:libfastcommon
cd libfastcommon
./make.sh && ./make.sh install
(2)下载fastdfs
cd fastdfs
./make.sh && ./make.sh install
(3)下载fastdfs-nginx-module
2、复制fastdfs->config文件下的
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/
cp mod_fastdfs.conf /etc/fdfs/
3、下载nginx
cd nginx
./cconfigure --add-module=/usr/local/soft/fastdfs-nginx-module/src
make && make install
修改配置文件:
tracker和storage配置描述https://blog.csdn.net/qq_41337192/article/details/122944934
修改完后启动 nginx
启动tracker
/usr/bin/ fdfs_trackerd /etc/fdfs/tracker.conf
启动storage
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
查看是否启动成功:
ps -ef|grep fastdfs
上传测试
rz //上传图片到虚拟机上
pwd //查询上传后的文件地址,下面一步会使用到
上传实例:
/user/bin/fdfs_upload_file /etc/fdfs/client.conf /root/Pictures/girl_sakura-wallpaper-1920x1080.jpg ?
上传成功后可以看到返回一行地址说明服务器已经安装配置完成
group1/M00/00/00/wKgBamKyk7KASj1wAAXCriL_btk052.jpg
Java代码准备:
导入相关依赖:
<!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
上才艺!!!(部分代码)
yml配置文件:
server:
port: 8081
servlet:
session:
timeout: 15000m
spring:
jackson:
date-format: yyyy-MM-dd HH-mm-ss
datasource:
url: jdbc:mysql://localhost:3306/wcss?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/com/springprogram/demo/mapper/mapper/*Mapper.xml
global-config:
db-config:
logic-delete-value: 1 #逻辑删除的值
logic-not-delete-value: 0 #逻辑删除的未删除字段值
fdfs:
so-timeout: 1500 #读取时间
connect-timeout: 600 #连接时间
thumb-image: #缩略图
height: 150
width: 150
tracker-list: #TrackerList参数,支持多个
- 192.168.0.101:22122
pool:
# 从线程池中获取最大对象数据(-1表示不限)
max-total: -1
#获取线程池的最大等待毫秒时间
max-wait-millis: 50
#每个key的最大连接数
max-total-per-key: 50
# 每个线程池的最大空闲连接数
max-idle-per-key: 10
min-idle-per-key: 5
upload:
base-url: http://192.168.0.101:8888/
controller层:
package com.springprogram.demo.controller;
import com.springprogram.demo.entity.WcssFile;
import com.springprogram.demo.service.FileService;
import com.springprogram.demo.service.UploadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* @ClassName: FileController
* @Description:
* @Author: TXW
* @Date: 2022/7/1
*/
@Controller
public class FileController {
@Value("${upload.base-url}")
private String basePath;
@Resource
private FileService fileService;
@Resource
private UploadService uploadService;
@RequestMapping("/")
public String filePage(){
return "fileStore";
}
@RequestMapping("/fileUpload")
public String fileUpload(MultipartFile file, Model model){
System.out.println("获取配置文件端口"+basePath);
WcssFile wcssFile = new WcssFile();
wcssFile.setFileName(file.getOriginalFilename());
wcssFile.setFileType(file.getContentType());
wcssFile.setIsEnable(1);
wcssFile.setCreateTime(new Date());
//保存到fastdfs先
String filePath = null;
try {
filePath = uploadService.uploadFile(file, wcssFile.getFileName());
} catch (IOException e) {
System.err.println("dfs保存文件时出错了"+e);
}
boolean isSuccess = false;
if (filePath != null){
// 保存到数据库
wcssFile.setFileUrl(filePath);
isSuccess = fileService.saveOrUpdate(wcssFile);
model.addAttribute("success",isSuccess);
}else {
model.addAttribute("error",isSuccess);
}
List<WcssFile> list = fileService.getList();
for (WcssFile f :list) {
f.setFileUrl(basePath+f.getFileUrl());
}
model.addAttribute("list",list);
return "fileList";
}
@RequestMapping("/fileList")
public String fileListPage(Model model){
List<WcssFile> list = fileService.getList();
for (WcssFile f :list) {
f.setFileUrl(basePath+f.getFileUrl());
}
model.addAttribute("list",list);
return "fileList";
}
}
中间的什么service啊mapper啊都是调用的mybatisplus的接口爆红直接在idea中创建就行了!!
Impl层
package com.springprogram.demo.service.fileServiceImp;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.springprogram.demo.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @ClassName: UploadServiceImp
* @Description:
* @Author: TXW
* @Date: 2022/6/26
*/
@Service
public class UploadServiceImp implements UploadService {
// 自动注入
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Override
public String uploadFile(MultipartFile file, String fileExtName) throws IOException {
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(),fileExtName,null);
return storePath.getFullPath();
}
@Override
public void download(HttpServletResponse response) {
byte[] bytes = fastFileStorageClient.downloadFile("group1", "数据库中查出来的地址", new DownloadByteArray());
try {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void saveFilePath(String path) {
System.out.println("文件保存成功" + path);
}
}
最后会将数据展示到前端,然后会有个拼接地址使用a标签进行接收,点击后会下载,下载后的文件没有后缀,喜欢探索的小伙伴可以自行去探索,而且文件也不能太大,不然会报错。这个也需要去探索结局上传文件太大的问题
你可能会用到(linux)防火墙相关命令
切记打开一切你项目中使用到的的端口!!!!!!
查看防火墙端口是否开启:firewall-cmd --query-port=8081/tcp
添加防火墙开启端口:firewall-cmd --permanent --add-port=8081/tcp
重启防火墙:firewall-cmd --reload