Example of fastdfs procedure code integrated with springboot

Time:2021-5-10

This article mainly introduces the spring boot integration fastdfs process code example, the article through the example code introduction is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

Pom.xml introduces dependency


<dependency>
  <groupId>com.github.tobato</groupId>
  <artifactId>fastdfs-client</artifactId>
  <version>1.26.1-RELEASE</version>
</dependency>

Application.properties configuration

#Fastdfs configuration
fdfs.connect-timeout=600
fdfs.so-timeout=1500
fdfs.trackerList=192.168.1.207:22122
fdfs.thumbImage.height=150
fdfs.thumbImage.width=150
spring.jmx.enabled=false
fdfs.pool.max-total=200
storage.resHost=http://192.168.1.207/
storage.resPort=8888

Automatic injection of dfsautoconfig.java


@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class DfsAutoConfig {
}

Dfsresconfig configuration mapping relationship

@Data
@Component
@ConfigurationProperties("storage")
public class DfsResConfig {
 
  private String resHost;
  private String resPort;
}F
<em>Fastdfsclientutil tool class < br > 0</em>
@Slf4j
@Component
public class FastDfsClientUtil {
 
  @Autowired
  private FastFileStorageClient storageClient;
 
  /**
   * @Author AlanMa
   *@ description multipartfile type file upload ַ
   * @Date 2019/11/12
   * @Param [file]
   * @return com.hiynn.data.visual.file.vo.ResultData<java.lang.String>
   */
  public ResultData<String> uploadFile(MultipartFile file){
 
    try{
      StorePath path = storageClient.uploadFile(file.getInputStream(), file.getSize(),
          FilenameUtils.getExtension(file.getOriginalFilename()), null);
      return ResultDataUtil.setSuccessResult(path.getFullPath());
    }catch (Exception e){
      e.printStackTrace();
      return ResultDataUtil.setFailedResult();
    }
 
  }
 
  /**
   * @Author AlanMa
   *@ description ordinary file upload
   * @Date 2019/11/12
   * @Param [file]
   * @return com.hiynn.data.visual.file.vo.ResultData<java.lang.String>
   */
  public ResultData<String> uploadFile(File file){
 
    try{
      FileInputStream inputStream = new FileInputStream(file);
      StorePath path = storageClient.uploadFile(inputStream, file.length(),
          FilenameUtils.getExtension(file.getName()), null);
      return ResultDataUtil.setSuccessResult(path.getFullPath());
    }catch (Exception e){
      e.printStackTrace();
      return ResultDataUtil.setFailedResult();
    }
  }
 
  /**
   * @Author AlanMa
   *@ description file upload with input stream
   * @Date 2019/11/12
   * @Param [is, size, fileName]
   * @return com.hiynn.data.visual.file.vo.ResultData<java.lang.String>
   */
  public ResultData<String> uploadFileStream(InputStream is, long size, String fileName) {
 
    StorePath path = storageClient.uploadFile(is, size, fileName, null);
    return ResultDataUtil.setSuccessResult(path.getFullPath());
  }
 
  /**
   * @Author AlanMa
   *@ description write a text file to the server of fastdfs
   * @Date 2019/11/12
   * @Param [content, fileExtension]
   * @return java.lang.String
   */
  public String uploadFile(String content, String fileExtension) {
    byte[] buff = content.getBytes(Charset.forName("UTF-8"));
    ByteArrayInputStream stream = new ByteArrayInputStream(buff);
    StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
    return path.getFullPath();
  }
 
  /**
   * @Author AlanMa
   *@ description delete file
   * @Date 2019/11/12
   * @Param [fileUrl]
   * @return com.hiynn.data.visual.file.vo.ResultData
   */
  public ResultData deleteFile(String fileUrl) {
 
    if (StringUtils.isEmpty(fileUrl)) {
      return ResultDataUtil.setFailedResult();
    }
    try {
      StorePath storePath = StorePath.praseFromUrl(fileUrl);
      storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
      return ResultDataUtil.setSuccessResult();
    } catch (FdfsUnsupportStorePathException e) {
      e.printStackTrace();
      log.warn(e.getMessage());
      return ResultDataUtil.setFailedResult();
    }
  }
//
//  /**
//   * @Author AlanMa
//* @ description upload file image
//   * @Date 2019/11/12
//   * @Param [is, size, fileExtName, metaData]
//   * @return java.lang.String
//   */
//  public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
//    StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
//    return path.getFullPath();
//  }
}

test


@Slf4j
@RestController
@RequestMapping("/dfs")
public class FileDfsController extends BaseController {
 
  @Autowired
  private FastDfsClientUtil fastDfsClientUtil;
 
  @Autowired
  private DfsResConfig dfsResConfig;
 
  @PostMapping("/single")
  public ResultData singleUpload(@RequestParam("file") MultipartFile file){
    ResultData<String> resultData = fastDfsClientUtil.uploadFile(file);
    if (Objects.equals(ResultEnum.SUCCESS.getCode(), resultData.getCode())) {
      String url = String.format("%s:%s/%s",dfsResConfig.getResHost(),dfsResConfig.getResPort(),resultData.getData());
      return ResultDataUtil.setSuccessResult(url);
    }
    return resultData;
 
  }
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.