上篇文章介绍了如何使用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调用你的文件上传接口,根据返回的路径在浏览器上访问,即可成功访问到你上传的文件。

二、SpringBoot实现上传文件到fastDFS文件服务器的更多相关文章

  1. SpringBoot初探(上传文件)

    学了Spring,SpringMVC,Mybatis这一套再来看SpringBoot,心里只有一句握草,好方便 这里对今天做的东西做个总结,然后在这之间先安利一个热部署的工具,叫spring-DevT ...

  2. Hessian学习总结(二)——使用hessian上传文件

    hessian较早版本通过 byte[] 进行文件传输:4.0之后支持 InputStream 作为参数或返回值进行传输. 注意:hessian会读取整个文件,如果文件过大,会导致JVM内存溢出.可以 ...

  3. katalon系列十二:自动化上传文件、下载文件

    一.下载文件1.下载文件时,需要先设置好Chrome/Firefox下载路径.不弹出下载框等,大家先学习下在selenium下如何设置:https://www.cnblogs.com/fnng/p/7 ...

  4. Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法

    记录瞬间 近段时间使用Springboot实现了文件的上传服务,但是在使用python的requests进行post上传时,总是报错. 比如: 1.Current request is not a m ...

  5. Python脚本控制的WebDriver 常用操作 <二十六> 上传文件

    测试用例场景 上传文件的方法是找到上传文件的对象,通常是的对象.然后直接往这个对象send_keys,传入需要上传文件的正确路径.绝对路径和相对路径都可以,但是上传的文件必须存在,否则会报错. Pyt ...

  6. SpringMVC上传文件后返回文件服务器地址路径

    先写一个表单: <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...

  7. springboot项目上传文件出现临时文件目录为空

    最近写文件上传到服务器读取的代码,前端使用FormData上传,服务端用MultipartFile接收,自己测试了下MultipartFile对象有什么东西,结果一般属性都能出来,测试getInput ...

  8. springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux

    //我的会员中心 头像上传接口 /*windows 调试*/ @Value("${appImg.location}") private String winPathPic; /*l ...

  9. Yii2 使用十二 配合ajaxFileUpload 上传文件

    1.js $("input#upload").change(function () { $.ajaxFileUpload({ url: '/members/web-members- ...

随机推荐

  1. DASCTF 安恒七月赛wp

    web Ezfileinclude 首页一张图片,看src就可以看出文件包含 验证了时间戳 尝试用php://filter 读源码读不到,以为只能读.jpg,然后用../路径穿越有waf 最后居然一直 ...

  2. 用C++实现俄罗斯方块(Tetris)游戏

    我是一个C++初学者,控制台实现了一个俄罗斯方块游戏. 代码如下: //"俄罗斯方块"V1.0 //李国良于2017年1月20日编写完成 #include <iostream ...

  3. 菜鸡的Java笔记 第二十九 - java 单例设计模式

    SingleCase 单例设计模式        1.单例设计模式的特点        2.多例设计模式的特点    内容    单例设计模式        现在如果说有这么一个程序类 class S ...

  4. R数据分析:跟随top期刊手把手教你做一个临床预测模型

    临床预测模型也是大家比较感兴趣的,今天就带着大家看一篇临床预测模型的文章,并且用一个例子给大家过一遍做法. 这篇文章来自护理领域顶级期刊的文章,文章名在下面 Ballesta-Castillejos ...

  5. Serverless 下的微服务实践

    作者:弈川 审核&校对:筱姜.潇航 编辑&排版:雯燕 微服务架构介绍 微服务架构诞生背景 在互联网早期即 Web 1.0 的时代,当时流行的是单体应用,研发团队比较小,主要是外部网页, ...

  6. [luogu4331]数字序列

    令$a'_{i}=a_{i}+n-i$.$b'_{i}=b_{i}+n-i$,代价仍然是$\sum_{i=1}^{n}|a'_{i}-b'_{i}|$,但条件变为了$b'_{i}\le b'_{i+1 ...

  7. [atARC096F]Sweet Alchemy

    给定一棵有根树,记$f_{i}$表示$i$的父亲,每一个点有一个代价$c_{i}$ 给定常数$D$和$X$,再给每个点赋一个权值$v_{i}$($v_{i}\ge 0$),满足以下条件下最大化$\su ...

  8. 力扣 - 剑指 Offer 27. 二叉树的镜像

    题目 剑指 Offer 27. 二叉树的镜像 思路1(递归) 我们可以使用深度优先搜索,先递归到链表的末尾,然后从末尾开始两两交换.就相当于后续遍历而已 记得要先保存下来node.right节点,因为 ...

  9. 『与善仁』Appium基础 — 10、Appium基本原理

    目录 1.Appium自动化测试架构 2.Appium架构图 3.Session说明 4.Desired Capabilities说明 5.Appium Server说明 6.Appium Clien ...

  10. jenkins cron

    1. Jenkins cron syntax Jenkins Cron 语法遵循Cron实用程序的语法(略有不同)具体来说,每行包含由TAB或SPACE分隔的5个字段(分时日月周): 分钟(Minut ...