赞
踩
<dependencies>
<!-- fastDFS-client -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.4</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
fdfs:
so-timeout: 3000
connect-timeout: 1000
thumb-image:
width: 60
height: 60
tracker-list:
- 121.199.58.42:22122
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 文件服务器配置类 * @Author 周威 * @Date 2020-08-01 - 21:43 */ @Configuration @Import(FdfsClientConfig.class) @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastDFSClientConfig { }
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 { // 获取文件源 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(); // 打印访问地址 System.out.println("fullPath = " + fullPath); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if(is != null) { // 关闭流资源 is.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
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; /** * 文件下载 */ @Test public void downloadTest() { // 文件访问地址 String fullPath = "group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg"; // 分离文件分组 String group = fullPath.substring(0, fullPath.indexOf("/")); // 分离文件路径 String path = fullPath.substring(fullPath.indexOf("/") + 1); // 进行文件下载 byte[] buffer = storageClient.downloadFile(group, path, new DownloadByteArray()); // 创建输出文件源 File target = new File("D://util", "target" + fullPath.substring(fullPath.indexOf("."))); OutputStream os = null; try { // 获取文件输出字节流 os = new FileOutputStream(target); // 将字节数组内容写入文件源 os.write(buffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 关闭流资源 if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
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; /** * 文件删除 */ @Test public void deleteTest() { // 文件访问地址 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); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。