基于fastDFS做了一个简单的文件上传案例(贼简陋),文件上传成功后将文件信息保存到MySQL数据库中
pom.xml
12 3 6org.springframework.boot 4spring-boot-starter-web 57 11 12org.mybatis.spring.boot 8mybatis-spring-boot-starter 92.0.1 1013 16mysql 14mysql-connector-java 1517 20net.oschina.zcx7878 18fastdfs-client-java 1921 25org.springframework.boot 22spring-boot-starter-test 23test 2426 29org.apache.commons 27commons-io 28
springboot启动类
1 @SpringBootApplication 2 public class FastDFSApplication { 3 4 public static void main(String[] args) { 5 SpringApplication.run(FastDFSApplication.class, args); 6 } 7 } 8
Controller
1 import com.xuecheng.fastdfs.api.UserApi; 2 import com.xuecheng.fastdfs.service.UserService; 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.PostMapping; 5 import org.springframework.web.bind.annotation.RequestParam; 6 import org.springframework.web.bind.annotation.RestController; 7 import org.springframework.web.multipart.MultipartFile; 9 import java.util.Map; 10 11 @RestController 12 public class UserController implements UserApi { 13 14 @Autowired 15 UserService userService; 16 17 @Override 18 @PostMapping("/upload") 19 public Map uploadPic(@RequestParam("file") MultipartFile file, String username) { 20 return userService.uploadPic(file, username); 21 22 } 23 24 25 } 26
文件上传代码实现部分
这里只写了文件上传的实现,文件的的删除等功能可以通过 storageClient1调用方法实现
实际开发中,根据需要返回的参数和需要存入数据库的参数可通过封装实体类传递信息
service
1 import com.xuecheng.fastdfs.mapper.UserMapper; 2 import org.csource.fastdfs.*; 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 import org.springframework.web.multipart.MultipartFile; 6 7 import java.io.IOException; 8 import java.util.HashMap; 9 import java.util.Map; 10 11 @Service 12 public class UserService { 13 14 @Autowired 15 UserMapper userMapper; 16 17 public Map uploadPic(MultipartFile file,String username) { 18 Map map = new HashMap(); 19 try { 20 //文件id 21 String fileId = fdfs_upload(file); 22 //文件直接存储的url 23 String fileUrl = queryFileUrl(fileId); 24 //将文件的fileId和fileUrl存入数据库,可供后面使用 25 userMapper.saveFileId(username, fileId,fileUrl); 26 map.put(true, fileId); 27 return map; 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 map.put(true, "上传失败"); 32 return map; 33 34 } 35 36 37 38 public String queryFileUrl(String fileId) { 39 StorageClient1 storageClient1 = getstorageClient1(); 40 try { 41 FileInfo fileInfo = storageClient1.query_file_info1(fileId); 42 String sourceIpAddr = fileInfo.getSourceIpAddr(); 43 return sourceIpAddr + "/" + fileId; 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } 47 return null; 48 } 49 50 //上传文件到fdfs,返回文件id 这里将文件上传的过程抽取了 51 public String fdfs_upload(MultipartFile file) { 52 try { 53 54 StorageClient1 storageClient1 = getstorageClient1(); 55 //上传文件 56 //文件字节 57 byte[] bytes = file.getBytes(); 58 //文件原始名称 59 String originalFilename = file.getOriginalFilename(); 60 //文件扩展名 61 String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); 62 //上传文件返回文件id 63 String file1 = storageClient1.upload_file1(bytes, extName, null); 64 //返回文件id 65 return file1; 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 return null; 70 } 71 72 73 //获取文件上传对象storageClient1 这里单独将此段代码抽取出来复用 74 public StorageClient1 getstorageClient1() { 75 try { 76 try { 77 //加载fdfs的配置 78 ClientGlobal.initByProperties("fastdfs-client.properties"); 79 } catch (IOException e) { 80 throw new RuntimeException("初始化配置文件出错"); 81 } 82 //创建tracker client 83 TrackerClient trackerClient = new TrackerClient(); 84 //获取trackerServer 85 TrackerServer trackerServer = trackerClient.getConnection(); 86 //获取storage 87 StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer); 88 //创建storage client 89 StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage); 90 return storageClient1; 91 } catch (Exception e) { 92 throw new RuntimeException("初始化StorageClient1出错"); 93 } 94 } 95 } 96
上面加载的文件上传的配置文件 fastdfs-client.properties
1 fastdfs.connect_timeout_in_seconds = 5 2 fastdfs.network_timeout_in_seconds = 30 3 fastdfs.charset = UTF-8 4 fastdfs.tracker_servers = 192.168.25.133:22122 5 6 #fastdfs.connect_timeout_in_seconds: http连接超时时间 7 #fastdfs.network_timeout_in_seconds: tracker与storage网络通信超时时间 8 #fastdfs.charset:字符编码 9 #fastdfs.tracker_servers:tracker服务器地址,多个地址中间用英文逗号分隔比如=192.168.25.133:22122,192.168.25.134:22122
持久层根据需要填写,这里做了简单的保存
配置文件application.yml
1 spring: 2 datasource: 3 driver-class-name: com.mysql.jdbc.Driver 4 url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC 5 username: root 6 password: dacian821 7 8 9 mybatis: 10 type-aliases-package: com.xuecheng.fastdfs.domain 11 mapper-locations: classpath:mapper/*Mapper.xml
usermaper接口
1 @Mapper 2 public interface UserMapper { 3 4 void saveFileId(String username,String fileId,String fileUrl); 5 6 }
usermaper.xml
1 2 4 56 7 8 129 update user set fileId=#{fileId},fileUrl=#{fileUrl} where username=#{username} 10 11