余慶先生提供了一個Java客戶端,但是作為一個C程序員,寫的java代碼可想而知。而且已經很久不維護了。
這里推薦一個開源的FastDFS客戶端,支持最新的SpringBoot2.0。
配置使用極為簡單,支持連接池,支持自動生成縮略圖,狂拽酷炫吊炸天啊,有木有。

接下來,我們就用FastDFS改造~~~-upload工程。
1.1.1.引入依賴
在父工程中,我們已經管理了依賴,版本為:
<fastDFS.client.version>1.26.2</fastDFS.client.version>
因此,這里我們直接在taotao-upload工程的pom.xml中引入坐標即可:
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency>
1.1.2.引入配置類

純java配置:
@Configuration
@Import(FdfsClientConfig.class) // 解決jmx重復注冊bean的問題 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }
1.1.3.編寫FastDFS屬性
在application.yml配置文件中追加如下內容:
fdfs:
so-timeout: 1501 # 超時時間 connect-timeout: 601 # 連接超時時間 thumb-image: # 縮略圖 width: 60 height: 60 tracker-list: # tracker地址:你的虛擬機服務器地址+端口(默認是22122) - 192.168.0.211:22122
1.1.4.配置hosts
將來通過域名:image.~~~.com這個域名訪問fastDFS服務器上的圖片資源。所以,需要代理到虛擬機地址:
配置hosts文件,使image.~~~.com可以訪問fastDFS服務器

1.1.5.測試
創建測試類:

把以下內容copy進去:
1 @SpringBootTest
2 @RunWith(SpringRunner.class) 3 public class FastDFSTest { 4 5 @Autowired 6 private FastFileStorageClient storageClient; 7 8 @Autowired 9 private ThumbImageConfig thumbImageConfig; 10 11 @Test 12 public void testUpload() throws FileNotFoundException { 13 // 要上傳的文件 14 File file = new File("C:\\Users\\joedy\\Pictures\\xbx1.jpg"); 15 // 上傳並保存圖片,參數:1-上傳的文件流 2-文件的大小 3-文件的后綴 4-可以不管他 16 StorePath storePath = this.storageClient.uploadFile( 17 new FileInputStream(file), file.length(), "jpg", null); 18 // 帶分組的路徑 19 System.out.println(storePath.getFullPath()); 20 // 不帶分組的路徑 21 System.out.println(storePath.getPath()); 22 } 23 24 @Test 25 public void testUploadAndCreateThumb() throws FileNotFoundException { 26 File file = new File("C:\\Users\\joedy\\Pictures\\xbx1.jpg"); 27 // 上傳並且生成縮略圖 28 StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( 29 new FileInputStream(file), file.length(), "png", null); 30 // 帶分組的路徑 31 System.out.println(storePath.getFullPath()); 32 // 不帶分組的路徑 33 System.out.println(storePath.getPath()); 34 // 獲取縮略圖路徑 35 String path = thumbImageConfig.getThumbImagePath(storePath.getPath()); 36 System.out.println(path); 37 } 38 }
結果:
1 group1/M00/00/00/wKg4ZVsWl5eAdLNZAABAhya2V0c424.jpg
2 M00/00/00/wKg4ZVsWl5eAdLNZAABAhya2V0c424.jpg 3 group1/M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772.png 4 M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772.png 5 M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772_60x60.png
訪問第二組第一個路徑:

訪問最后一個路徑(縮略圖路徑),注意加組名(group1):

在service中改造上傳邏輯
紅色標記為新加入的
@Service
public class UploadService { @Autowired private FastFileStorageClient storageClient; private static final List<String> CONTENT_TYPES = Arrays.asList("image/jpeg", "image/gif"); private static final Logger LOGGER = LoggerFactory.getLogger(UploadService.class); public String upload(MultipartFile file) { String originalFilename = file.getOriginalFilename(); // 校驗文件的類型 String contentType = file.getContentType(); if (!CONTENT_TYPES.contains(contentType)){ // 文件類型不合法,直接返回null LOGGER.info("文件類型不合法:{}", originalFilename); return null; } try { // 校驗文件的內容 BufferedImage bufferedImage = ImageIO.read(file.getInputStream()); if (bufferedImage == null){ LOGGER.info("文件內容不合法:{}", originalFilename); return null; } // 保存到服務器 // file.transferTo(new File("C:\\~~~\\images\\" + originalFilename)); String ext = StringUtils.substringAfterLast(originalFilename, "."); StorePath storePath = this.storageClient.uploadFile(file.getInputStream(), file.getSize(), ext, null); // 生成url地址,返回 return "http://image.~~~.com/" + storePath.getFullPath(); } catch (IOException e) { LOGGER.info("服務器內部錯誤:{}", originalFilename); e.printStackTrace(); } return null; } }
