二、SpringBoot实现上传文件到fastDFS文件服务器
上篇文章介绍了如何使用docker安装fastDFS文件服务器,这一篇就介绍整合springBoot实现文件上传到fastDFS文件服务器
1.pom.xml文件添加依赖
<!-- 连接fastdfs文件系统 --> <dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
2.在resource包下创建配置文件fdfs_client.conf
tracker_server的值ip为你文件服务器的ip
connect_timeout=30 network_timeout=60 charset = UTF-8 http.tracker_http_port = 8888 http.anti_steal_token = no http.secret_key = tracker_server=ip:22122
3.创建FastDFSConfig.java加载fdfs_client.conf配置文件
import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.TrackerClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; /** * fastDFS文件上传的配置 */ @Configuration public class FastDFSConfig { private final Logger log = LoggerFactory.getLogger(this.getClass()); @Value("classpath:fdfs_client.conf") private Resource ccs; @Bean public TrackerClient initClient(){ try{ ClientGlobal.init(ccs.getFilename()); return new TrackerClient(); }catch (Exception e){ log.info("FastDFS创建客户端失败"); return null; } } }
4.创建文件上传的Cotroller,返回的访问路径中ip是你文件服务器的ip
import org.csource.common.NameValuePair; import org.csource.fastdfs.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; 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; @RestController @RequestMapping("/file") public class FileController { private Logger log = LoggerFactory.getLogger(FileController.class); @Autowired private TrackerClient trackerClient; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception{ if(file == null){ throw new RuntimeException("文件不能为空"); } //1.获取文件的完整名称 String filename = file.getOriginalFilename(); if(StringUtils.isEmpty(filename)){ throw new RuntimeException("文件不存在"); } //2.获取文件的扩展名称 String extName = filename.substring(filename.lastIndexOf(".") + 1); log.info("文件的全名:"+filename+" 文件的扩展名:"+extName); NameValuePair[] metaList = new NameValuePair[1]; metaList[0] = new NameValuePair("fileName", filename); //3.创建trackerServer TrackerServer trackerServer = trackerClient.getConnection(); // 4、创建一个 StorageServer 的引用,值为 null StorageServer storageServer = null; // 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用 StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 6、使用 StorageClient 对象上传图片。 String[] strings = storageClient.upload_file(file.getBytes(), extName, metaList); return "http://ip:8888/"+strings[0]+"/"+strings[1]; }
5.此时用postman调用你的文件上传接口,根据返回的路径在浏览器上访问,即可成功访问到你上传的文件。
相关推荐
zhangskd 2020-06-21
我爱熊猫 2020-01-28
Hhaile 2019-03-13
单调的低调 2019-09-09
ROES 2020-06-11
MingoJiang 2020-05-27
笑面依旧 2020-04-26
cmsmdn 2020-03-06
书弋江山 2020-04-26
LandryBean 2020-04-06
leodengzx 2020-03-04
thlm0 2019-11-08
beiya 2008-02-25
whechuan00 2007-10-12
lucklytd 2007-08-08
MedeUz 2010-08-03
linuxprobe0 2008-09-29
小小狐狸 2019-09-29