使用fastDFS客户端改造文件上传
java客戶端
余慶先生提供了一個(gè)Java客戶端,但是作為一個(gè)C程序員,寫的java代碼可想而知。而且已經(jīng)很久不維護(hù)了。
這里推薦一個(gè)開源的FastDFS客戶端,支持最新的SpringBoot2.0。
配置使用極為簡(jiǎn)單,支持連接池,支持自動(dòng)生成縮略圖,狂拽酷炫吊炸天啊,有木有。
地址:tobato/FastDFS_client
接下來(lái),我們就用FastDFS改造learn-upload工程。
引入依賴
在父工程中,我們已經(jīng)管理了依賴,版本為:
<fastDFS.client.version>1.26.2</fastDFS.client.version>因此,這里我們直接在taotao-upload工程的pom.xml中引入坐標(biāo)即可:
<dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId> </dependency>引入配置類
純java配置:
@Configuration @Import(FdfsClientConfig.class) // 解決jmx重復(fù)注冊(cè)bean的問(wèn)題 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter {}編寫FastDFS屬性
在application.yml配置文件中追加如下內(nèi)容:
fdfs:so-timeout: 1501 # 超時(shí)時(shí)間connect-timeout: 601 # 連接超時(shí)時(shí)間thumb-image: # 縮略圖width: 60height: 60tracker-list: # tracker地址:你的虛擬機(jī)服務(wù)器地址+端口(默認(rèn)是22122)- 192.168.56.101:22122配置hosts
將來(lái)通過(guò)域名:image.learn.com這個(gè)域名訪問(wèn)fastDFS服務(wù)器上的圖片資源。所以,需要代理到虛擬機(jī)地址:
配置hosts文件,使image.learn.com可以訪問(wèn)fastDFS服務(wù)器
測(cè)試
創(chuàng)建測(cè)試類:
把以下內(nèi)容copy進(jìn)去:
@SpringBootTest @RunWith(SpringRunner.class) public class FastDFSTest {@Autowiredprivate FastFileStorageClient storageClient;@Autowiredprivate ThumbImageConfig thumbImageConfig;@Testpublic void testUpload() throws FileNotFoundException {// 要上傳的文件File file = new File("C:\\Users\\leon\\Pictures\\xbx1.jpg");// 上傳并保存圖片,參數(shù):1-上傳的文件流 2-文件的大小 3-文件的后綴 4-可以不管他StorePath storePath = this.storageClient.uploadFile(new FileInputStream(file), file.length(), "jpg", null);// 帶分組的路徑System.out.println(storePath.getFullPath());// 不帶分組的路徑System.out.println(storePath.getPath());}@Testpublic void testUploadAndCreateThumb() throws FileNotFoundException {File file = new File("C:\\Users\\leon\\Pictures\\xbx1.jpg");// 上傳并且生成縮略圖StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(new FileInputStream(file), file.length(), "png", null);// 帶分組的路徑System.out.println(storePath.getFullPath());// 不帶分組的路徑System.out.println(storePath.getPath());// 獲取縮略圖路徑String path = thumbImageConfig.getThumbImagePath(storePath.getPath());System.out.println(path);} }結(jié)果:
group1/M00/00/00/wKg4ZVsWl5eAdLNZAABAhya2V0c424.jpg M00/00/00/wKg4ZVsWl5eAdLNZAABAhya2V0c424.jpg group1/M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772.png M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772.png M00/00/00/wKg4ZVsWmD-ARnWiAABAhya2V0c772_60x60.png訪問(wèn)第二組第一個(gè)路徑:
訪問(wèn)最后一個(gè)路徑(縮略圖路徑),注意加組名(group1):
改造上傳邏輯
@Service public class UploadService {@Autowiredprivate 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();// 校驗(yàn)文件的類型String contentType = file.getContentType();if (!CONTENT_TYPES.contains(contentType)){// 文件類型不合法,直接返回nullLOGGER.info("文件類型不合法:{}", originalFilename);return null;}try {// 校驗(yàn)文件的內(nèi)容BufferedImage bufferedImage = ImageIO.read(file.getInputStream());if (bufferedImage == null){LOGGER.info("文件內(nèi)容不合法:{}", originalFilename);return null;}// 保存到服務(wù)器// file.transferTo(new File("C:\\learn\\images\\" + originalFilename));String ext = StringUtils.substringAfterLast(originalFilename, ".");StorePath storePath = this.storageClient.uploadFile(file.getInputStream(), file.getSize(), ext, null);// 生成url地址,返回return "http://image.learn.com/" + storePath.getFullPath();} catch (IOException e) {LOGGER.info("服務(wù)器內(nèi)部錯(cuò)誤:{}", originalFilename);e.printStackTrace();}return null;} }只需要把原來(lái)保存文件的邏輯去掉,然后上傳到FastDFS即可。
測(cè)試
通過(guò)RestClient測(cè)試:
頁(yè)面測(cè)試上傳
發(fā)現(xiàn)上傳成功:
?
總結(jié)
以上是生活随笔為你收集整理的使用fastDFS客户端改造文件上传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: FastDFS的安装
- 下一篇: 规格参数表结构