Create a SpringBoot project
Project depend on
<dependencies> <! -- fastDFS-client --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> The < version > 1.26.4 < / version > < / dependency > <! -- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>Copy the code
The configuration file
FDFS: so timeout: 3000 connect-timeout: 1000 Thumb-image: width: 60 height: 60 tracker-list: -121.199.58.42:22122Copy the code
File service configuration class
package zw.springboot.config; import com.github.tobato.fastdfs.FdfsClientConfig; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableMBeanExport; import org.springframework.context.annotation.Import; import org.springframework.jmx.support.RegistrationPolicy; /** * @className FastDFSClientConfig * @description File server Configuration class * @author Zhou Wei@date 2020-08-01-21:43 */ @configuration @Import(FdfsClientConfig.class) @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastDFSClientConfig { }Copy the code
File upload Test
package zw.springboot; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.apache.commons.io.FilenameUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.*; @SpringBootTest class SpringbootApplicationTest { @Autowired private FastFileStorageClient storageClient; @test public void uploadTest() {InputStream is = null; Try {// Get the File source File source = new File("D:\ util\ fstdfs.jpg "); Is = new FileInputStream(source); StorePath StorePath = storageclient.uploadFile (is, source.length(), FilenameUtils.getExtension(source.getName()), null); String fullPath = storePath.getFullPath(); String fullPath = storepath.getFullPath (); Println ("fullPath = "+ fullPath); // Print the access address system.out. println("fullPath =" + fullPath); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if(is ! = null) {// Close the stream resource is.close(); } } catch (IOException e) { e.printStackTrace(); }}}}Copy the code
File upload operation
- Console print file access path: fullPath = group1 / M00/00/00 / eccnMl9PT9uAUK9VAAB1IQneShA844. JPG
- Browser: IP + port + fullPath
File download test
package zw.springboot; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.apache.commons.io.FilenameUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.*; @SpringBootTest class SpringbootApplicationTest { @Autowired private FastFileStorageClient storageClient; /** * File download */ @test public void downloadTest() {// File access address String fullPath = "group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg"; String group = fullPath.subString (0, fullPath.indexof ("/")); String Path = fullPath.subString (fullPath.indexof ("/") + 1); / / file download byte [] buffer. = storageClient downloadFile (group, path, new DownloadByteArray ()); File target = new File("D://util", "target" + fullPath.substring(fullPath.indexof ("."))); OutputStream os = null; OS = new FileOutputStream(target); // Write the contents of the byte array to the file source os.write(buffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Close the stream resource if (OS! = null) { os.close(); } } catch (IOException e) { e.printStackTrace(); }}}}Copy the code
File download run
File Deletion Test
package zw.springboot; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.apache.commons.io.FilenameUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.*; @SpringBootTest class SpringbootApplicationTest { @Autowired private FastFileStorageClient storageClient; /** * File deletion */ @test public void deleteTest() {// File access address String fullPath = "group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg"; String group = fullPath.subString (0, fullPath.indexof ("/")); String Path = fullPath.subString (fullPath.indexof ("/") + 1); Storageclient.deletefile (group, path); }}Copy the code
File deletion run
- Browser: IP + port + fullPath
The last
Thank you for reading here, the article has any shortcomings please correct, feel the article is helpful to you remember to give me a thumbs up, every day will share Java related technical articles or industry information, welcome to pay attention to and forward the article!