上篇文章介绍了如何使用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. feign微服务调用携带浏览器信息(header、cookie)

    import feign.RequestInterceptor; import feign.RequestTemplate; import org.apache.commons.collections ...

  2. Linux驱动实践:带你一步一步编译内核驱动程序

    作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...

  3. hover 背后的数学和图形学

    前端开发中,hover是最常见的鼠标操作行为之一,用起来也很方便,CSS直接提供:hover伪类,js可以通过mouseover+mouseout事件模拟,甚至一些第三方库/框架直接提供了 hover ...

  4. linux安装python3.6.1

    Linux下安装Python3.6和第三方库   如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!! ...

  5. 【Microsoft Azure 的1024种玩法】三.基于Azure云平台构建Discuz论坛

    [简介] Discuz!是一套通用社区论坛软件系统,用户在不需要任何编程的基础上,通过简单的设置和安装,在互联网上搭建起具备完善功能.很强负载能力和可高度定制的论坛服务. [前期文章] [操作步骤] ...

  6. [atARC112E]Rvom and Rsrev

    毒瘤分类讨论题 (注:以下情况都有"之前的情况都不满足的"前提条件,并用斜体表示一些说明) Case0:若$|s|\le 2$,直接输出即可,因此假设$|s|>3$ 首先,我 ...

  7. 【Java面试题】-- Java基本类型

    Java基本类型 2019-11-03  19:03:48  by冲冲 1.两个float型相减丢失精度,如何解决? 使用BigDemical装饰器模式 public class Test { pub ...

  8. git添加新工程

    git init git remote add origin 码云路径 git pull origin master 代码拉本地后 git add . git commit -m '新添加的文件内容描 ...

  9. mybatis-批量操作数据(list对象 )

    在实际工作中老是忘记 传入的参数和数据库参数名称要一致还是与实体类型一致导致很多笑话发生. 那我还是做个记录吧! dao层: int addRemark(@Param("list" ...

  10. Java 操作符小记

    "在最底层,Java中的数据是通过使用操作符来操作的" (Thinking in Java) 1 算术操作符 Java 中的基本算术操作符和其他大多数程序设计语言是相同的.其中包括 ...