Springboot統合fastDFS添付ファイルのアップロードと削除機能を実現
1.pom.xml参照jarパッケージ
2.application.yml fastDFSノード情報の構成
3.DfsConfigを作成する.java
4.FileDfsUtilを作成する.java
5.テストコントローラの作成
com.github.tobato
fastdfs-client
1.26.5
2.application.yml fastDFSノード情報の構成
fdfs:
#
connect-timeout: 120
#
so-timeout: 120
#
thumb-image:
width: 150
height: 150
tracker-list: 172.16.1.83:22122,172.16.1.74:22122
3.DfsConfigを作成する.java
package com.currency.tbyoung.fastdfs;
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;
@Configuration
@Import(FdfsClientConfig.class)
// Jmx bean
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class DfsConfig {
}
4.FileDfsUtilを作成する.java
package com.currency.tbyoung.fastdfs;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@Component
public class FileDfsUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);
@Resource
private FastFileStorageClient storageClient ;
/**
*
*/
public String upload(MultipartFile multipartFile) throws Exception{
String originalFilename = multipartFile.getOriginalFilename().
substring(multipartFile.getOriginalFilename().
lastIndexOf(".") + 1);
StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
multipartFile.getInputStream(),
multipartFile.getSize(),originalFilename , null);
return storePath.getFullPath() ;
}
/**
*
*/
public String deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return "fileUrl == >> ...";
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
return e.getMessage();
}
return "OK";
}
}
5.テストコントローラの作成
import com.currency.tbyoung.bean.ReturnValues;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource
@RestController
public class FileController {
@Resource
private FileDfsUtil fileDfsUtil ;
@RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
public ReturnValues uploadFile (@RequestParam("file") MultipartFile file){
String result ;
try{
String path = fileDfsUtil.upload(file) ;
if (!StringUtils.isEmpty(path)){
result = path ;
} else {
result = " " ;
}
} catch (Exception e){
e.printStackTrace() ;
result = " " ;
}
return new ReturnValues(ReturnValues.SUCCESS,result);
}
/**
*
*/
@RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
public ReturnValues deleteByPath (String filePathName){
String result = fileDfsUtil.deleteFile(filePathName);
if("OK".equals(result)){
return new ReturnValues(ReturnValues.SUCCESS," ");
}else{
return new ReturnValues(ReturnValues.ERROR,result);
}
}
}