分布式存储FastDFS
- 加入依赖
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
- yml中配置
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
- fdfs_clients.conf
connect_timeout = 60network_timeout = 60charset = UTF-8http.tracker_http_port = 8080tracker_server = 192.168.200.128:22122
- 测试文件
try{ //判断文件是否存在 if (file == null){ throw new RuntimeException("文件不存在"); } //获取文件的完整名称 String originalFilename = file.getOriginalFilename(); if (StringUtils.isEmpty(originalFilename)){ throw new RuntimeException("文件不存在"); } //获取文件的扩展名称 abc.jpg jpg String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); //获取文件内容 byte[] content = file.getBytes(); //创建文件上传的封装实体类 FastDFSFile fastDFSFile = new FastDFSFile(originalFilename,content,extName); //基于工具类进行文件上传,并接受返回参数 String[] String[] uploadResult = FastDFSClient.upload(fastDFSFile); //封装返回结果 String url = FastDFSClient.getTrackerUrl()+uploadResult[0]+"/"+uploadResult[1]; return new Result(true,StatusCode.OK,"文件上传成功",url); }catch (Exception e){ e.printStackTrace(); } return new Result(false, StatusCode.ERROR,"文件上传失败");}
相关推荐
chensen 2020-11-14
lwnylslwnyls 2020-11-06
ATenhong 2020-10-15
yanzhelee 2020-10-13
佛系程序员J 2020-10-10
guojin0 2020-10-08
佛系程序员J 2020-10-08
bluewelkin 2020-09-16
wwzaqw 2020-09-04
zhongdaowendao 2020-09-02
favouriter 2020-08-18
奎因amp华洛 2020-08-15
一青年 2020-08-13
千锋 2020-08-10
nangongyanya 2020-08-09
dongxurr 2020-08-08
明天你好 2020-08-03
kyelu 2020-08-03
Ashes 2020-08-03