上篇文章介绍了如何使用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. python Max retries exceeded with URL in requests

    使用requests进行重试 import requests from requests.adapters import HTTPAdapter from requests.packages.urll ...

  2. Python命令行参数及文件读出写入

    看完了柯老板的个人编程作业,虽然是评测组不用做此次作业,但还是想对本次作业涉及到利用Python命令行参数以及进行文件读出写入操作做一个简单的总结.(个人编程作业还是想自己能敲一敲,毕竟我的码力还是小 ...

  3. 快速搭建 kvm web 管理工具 WebVirtMgr

    作者:SRE运维博客 博客地址: https://www.cnsre.cn/ 文章地址:https://www.cnsre.cn/posts/211117937177/ 相关话题:https://ww ...

  4. halcon基础算子介绍(窗口创建,算子运行时长,是否启用更新函数)

    前言 halcon有有大约1500个算子,我总结一些简单大家用得到的算子,比如创建窗口的方式有3种,接下来结束这方式,及其异同点等! 1.窗口创建的三种方式 1.1使用dev_open_window算 ...

  5. Maven 依赖调解源码解析(三):传递依赖,路径最近者优先

    本文是系列文章<Maven 源码解析:依赖调解是如何实现的?>第三篇,主要介绍依赖调解的第一条原则:传递依赖,路径最近者优先.本篇内容较多,也是开始源码分析的第一篇,请务必仔细阅读,否则后 ...

  6. [noi37]列队

    直接统计答案,令dp[i][j]表示前i个数最长的颜色各不相同后缀长度为j的方案数,如果一直令j<m,那么就相当于统计了方案数. 如何推出dp[i][j]呢?考虑i-1的最长前缀是多少:当小于j ...

  7. 第04章_MySQL运算符详解

    第04章_运算符 1. 算术运算符 算术运算符主要用于数学运算,其可以连接运算符前后的两个数值或表达式,对数值或表达式进行加(+).减(-).乘(*).除(/)和取模(%)运算. 1.加法与减法运算符 ...

  8. Jmeter——变量嵌套函数使用(__V)案例分析

    jmeter版本:5.3 __V官方函数解释: (https://jmeter.apache.org/usermanual/functions.html#__V) 图1-1 解决问题:实现字符串拼接 ...

  9. CF1354F Summoning Minions

    考虑我们一定是先放我们选定了\(m\)个数,一定是先放了\(m-1\)个数上去,然后让放上一个不打算选的然后拿下来,白嫖\(b * (m-1)\)的贡献,最后放上一个打算放的. 考虑我们一定是按\(b ...

  10. Codeforces 429E - Points and Segments(欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 果然我不具备融会贯通的能力/ll 看到这样的设问我们可以很自然地联想到这道题,具体来说我们可以通过某种方式建出一张图,然后根据" ...