Mr. Yuqing provides a Java client, but as a C programmer, to write java code can be imagined. And it has been for a long time is not maintained.
It is recommended that an open source FastDFS client, support for the latest SpringBoot2.0.
Configuration is very simple to use, supports connection pooling, support for automatic generation of thumbnails, mad pull cool hanging fried God, there are wood.
Address: tobato / FastDFS_client
Next, we will use FastDFS transformation ~ ~ ~ -upload project.
1.1.1 The introduction of dependence
In the parent project, we have managed dependence, version:
<fastDFS.client.version>1.26.2</fastDFS.client.version>
So here we are directly introduced to coordinate the project in taotao-upload the pom.xml:
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> </dependency>
1.1.2. Introductory configuration class
Pure java configuration: @Configuration @Import (. FdfsClientConfig class ) // solve jmx repeated registration of the bean @EnableMBeanExport (Registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }
1.1.3. Writing FastDFS property
Append the following in application.yml configuration file:
FDFS: SO -timeout: 1501 # timeout Connect -timeout: 601 # connection timeout Thumb - Image: # thumbnail width: 60 height: 60 Tracker -list: # Tracker Address: Your virtual machine server address + port (default is 22122) - 192.168.0.211:22122
1.1.4. Configuring hosts
The future through the domain name:. Image ~~~ .com domain name to access resources on the pictures fastDFS server. Therefore, it requires a proxy address to a virtual machine:
Configure the hosts file, so that image. ~~~ .com server can access fastDFS
1.1.5. Test
Create a test class:
The following copy into it:
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 = newFile ( "C: \\ joedy the Users \\ \\ \\ xbx1.jpg Pictures" ); 15 // uploading and storing the image, the parameter: Suffix 1- size uploaded file stream file 2- 3- 4- file Whether he can 16 StorePath = storepath the this .storageClient.uploadFile ( . 17 new new the FileInputStream (File), file.length (), "JPG", null ); 18 is // path with the packet . 19 System.out.println (storePath.getFullPath ()); 20 // path without packet 21 is System.out.println (storePath.getPath ()); 22 is } 23 is 24 @Test 25 public void testUploadAndCreateThumb () throws a FileNotFoundException { 26 is File File = new new File ( "C: \\ joedy the Users \\ \\ \\ xbx1.jpg Pictures" ); 27 // upload and generate a thumbnail 28 StorePath = storepath the this .storageClient.uploadImageAndCrtThumbImage ( 29 new new the FileInputStream (File), file.length (), "PNG", null ); 30 // path with the packet 31 is System.out.println (storePath.getFullPath ()); 32 // path without packet 33 is the System. Out.println (storePath.getPath ()); 34 is // Get the thumbnail path 35 String path = thumbImageConfig.getThumbImagePath(storePath.getPath()); 36 System.out.println(path); 37 } 38 }
result:
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
Access to the second set of the first path:
Access the last path (path thumbnail), pay attention to add the group name (group1) :
Upload the transformation logic in service
Red marked as new entrants
@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)) { // file type is not valid, the direct return null logger.info ( "file type is not valid: {}" , OriginalFilename); return null ; } the try { // the contents of the verification document BufferedImage bufferedImage = ImageIO.read (file.getInputStream ()); IF (BufferedImage == null ) { logger.info ( "invalid file content: {}" , OriginalFilename); return null ; } // saved to the server // 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; } }