- SpringBoot整合FastDFS很简单,这里使用的组件是:
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
import com.github.tobato.fastdfs.FdfsClientConfig;
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
public class FastDFSConfiguration {
}
- application.properties配置文件中添加FastDFS的相关配置:
fdfs.trackerList=[ip]:[port]
fdfs.connect-timeout=5000
fdfs.so-timeout=3000
fdfs.pool.max-total=200
fdfs.pool.max-total-per-key=20
fdfs.pool.max-wait-millis=25000
fdfs.thumbImage.height=150
fdfs.thumbImage.width=150
- 把上面配置中的[ip]和[port]替换成自己FastDFS的tracker地址就整合完成了,然后添加文件上传、下载、删除方法的包装类:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
@Component
public class FastDFSClientWrapper {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return storePath.getFullPath();
}
public String uploadFile(byte[] bytes, String format) {
StorePath storePath = storageClient.uploadFile(new ByteArrayInputStream(bytes), bytes.length, format, null);
return storePath.getFullPath();
}
public String uploadFile(File file) throws IOException {
StorePath storePath = storageClient.uploadFile(FileUtils.openInputStream(file), file.length(),
FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
}
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return storePath.getFullPath();
}
public String uploadImageAndCrtThumbImage(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return storePath.getFullPath();
}
public String getThumbImagePath(String filePath) {
return thumbImageConfig.getThumbImagePath(filePath);
}
public byte[] downFile(String filePath) throws IOException {
StorePath storePath = StorePath.praseFromUrl(filePath);
return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadCallback<byte[]>() {
@Override
public byte[] recv(InputStream ins) throws IOException {
return org.apache.commons.io.IOUtils.toByteArray(ins);
}
});
}
public void deleteFile(String filePath) {
StorePath storePath = StorePath.praseFromUrl(filePath);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
}
}