Springboot工程引入淘宝开源插件FastDFS_client启动报错:
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [FdfsConnectionPool [maxTotal=50, blockWhenExhausted=true, maxWaitMillis=100, lifo=true, fairness=false, testOnCreate=false, testOnBorrow=false, testOnReturn=false, testWhileIdle=true, timeBetweenEvictionRunsMillis=60000, numTestsPerEvictionRun=-1, minEvictableIdleTimeMillis=180000, softMinEvictableIdleTimeMillis=-1, evictionPolicy=org.apache.commons.pool2.impl.DefaultEvictionPolicy@1a05ff8e, closeLock=java.lang.Object@251e2f4a, closed=false, evictionLock=java.lang.Object@1abea1ed, evictor=org.apache.commons.pool2.impl.BaseGenericObjectPool$Evictor@6f5288c5, evictionIterator=null, factoryClassLoader=java.lang.ref.WeakReference@5a9ef32e, oname=org.apache.commons.pool2:type=GenericKeyedObjectPool,name=pool, creationStackTrace=java.lang.Exception
at org.apache.commons.pool2.impl.BaseGenericObjectPool.<init>(BaseGenericObjectPool.java:142)
at org.apache.commons.pool2.impl.GenericKeyedObjectPool.<init>(GenericKeyedObjectPool.java:105)
at com.github.tobato.fastdfs.conn.FdfsConnectionPool.<init>(FdfsConnectionPool.java:33)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
解决这个问题需要在启动类上加一个注解 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING):
@SpringBootApplication
@EnableSwagger2
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@ComponentScan("com")
public class LearningApplication {
public static void main(String[] args) {
SpringApplication.run(LearningApplication.class, args);
}
}
@EnableMBeanExport解决JMX重复注册问题,不要再配置 spring.jmx.enabled=false,以免影响SpringBoot默认的JMX监控。
application.yml参数配置:
# ===================================================================
# 分布式文件系统FDFS配置
# ===================================================================
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList参数,支持多个
- 192.168.1.1:22122
上传下载:
import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
@RequestMapping("/file")
public class FileController {
@Resource
private FastFileStorageClient fastFileStorageClient;
@PostMapping("/upload")
public String save(MultipartFile multipartFile) throws IOException {
byte[] bytes = multipartFile.getBytes();
InputStream inputStream = new ByteArrayInputStream(bytes);
String extName = FileUtil.extName(multipartFile.getOriginalFilename());
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, bytes.length, extName, null);
return storePath.getFullPath();
}
@PostMapping("/del")
public String del(@RequestParam String uri) {
StorePath storePath = fromUri(uri);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
return "删除成功";
}
/**
* 从uri生成StorePath对象.
*/
private StorePath fromUri(String uri) {
String group = uri.replaceFirst("/.*$", "");
String path = uri.replaceFirst("^.*?/", "");
return new StorePath(group, path);
}
//下载
public byte[] getBin(String fullPath) {
try {
StorePath storePath = fromUri(fullPath);
String group = storePath.getGroup();
String path = storePath.getPath();
return fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}