基於fastDFS做了一個簡單的文件上傳案例(賊簡陋),文件上傳成功后將文件信息保存到MySQL數據庫中
pom.xml
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.mybatis.spring.boot</groupId> 8 <artifactId>mybatis-spring-boot-starter</artifactId> 9 <version>2.0.1</version> 10 </dependency> 11 <!-- MySQL連接驅動 --> 12 <dependency> 13 <groupId>mysql</groupId> 14 <artifactId>mysql-connector-java</artifactId> 15 </dependency> 16 <dependency> 17 <groupId>net.oschina.zcx7878</groupId> 18 <artifactId>fastdfs-client-java</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-test</artifactId> 23 <scope>test</scope> 24 </dependency> 25 <dependency> 26 <groupId>org.apache.commons</groupId> 27 <artifactId>commons-io</artifactId> 28 </dependency> 29
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 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 4 5 <mapper namespace="com.xuecheng.fastdfs.mapper.UserMapper"> 6 7 8 <update id="saveFileId"> 9 update user set fileId=#{fileId},fileUrl=#{fileUrl} where username=#{username} 10 </update> 11 </mapper> 12