二、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调用你的文件上传接口,根据返回的路径在浏览器上访问,即可成功访问到你上传的文件。
二、SpringBoot实现上传文件到fastDFS文件服务器的更多相关文章
- SpringBoot初探(上传文件)
学了Spring,SpringMVC,Mybatis这一套再来看SpringBoot,心里只有一句握草,好方便 这里对今天做的东西做个总结,然后在这之间先安利一个热部署的工具,叫spring-DevT ...
- Hessian学习总结(二)——使用hessian上传文件
hessian较早版本通过 byte[] 进行文件传输:4.0之后支持 InputStream 作为参数或返回值进行传输. 注意:hessian会读取整个文件,如果文件过大,会导致JVM内存溢出.可以 ...
- katalon系列十二:自动化上传文件、下载文件
一.下载文件1.下载文件时,需要先设置好Chrome/Firefox下载路径.不弹出下载框等,大家先学习下在selenium下如何设置:https://www.cnblogs.com/fnng/p/7 ...
- Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法
记录瞬间 近段时间使用Springboot实现了文件的上传服务,但是在使用python的requests进行post上传时,总是报错. 比如: 1.Current request is not a m ...
- Python脚本控制的WebDriver 常用操作 <二十六> 上传文件
测试用例场景 上传文件的方法是找到上传文件的对象,通常是的对象.然后直接往这个对象send_keys,传入需要上传文件的正确路径.绝对路径和相对路径都可以,但是上传的文件必须存在,否则会报错. Pyt ...
- SpringMVC上传文件后返回文件服务器地址路径
先写一个表单: <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...
- springboot项目上传文件出现临时文件目录为空
最近写文件上传到服务器读取的代码,前端使用FormData上传,服务端用MultipartFile接收,自己测试了下MultipartFile对象有什么东西,结果一般属性都能出来,测试getInput ...
- springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux
//我的会员中心 头像上传接口 /*windows 调试*/ @Value("${appImg.location}") private String winPathPic; /*l ...
- Yii2 使用十二 配合ajaxFileUpload 上传文件
1.js $("input#upload").change(function () { $.ajaxFileUpload({ url: '/members/web-members- ...
随机推荐
- 六问六答理解ForkJoin原理
摘要:ForkJoin线程池是将任务分割为子任务,有可能子任务还是很大,还需要进一步拆解,最终得到足够小的任务. 本文分享自华为云社区<ForkJoin线程池的学习和思考>,作者:brea ...
- HCNP Routing&Switching之BGP路由过滤和AS-Path-Filter
前文我们聊了下通过修改BGP路由属性来影响路由,从而达到控制BGP路由的目的:回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15495585.html:今天我们 ...
- Qt概要
Qt是面向对象的框架,使用特殊的代码生成扩展以及一些宏.是一个跨平台的C++图形用户界面.它是一款可以轻松的帮我们做界面的软件. Qt是1991年奇趣科技公司(2008 年 6 月被诺基亚收购)开发的 ...
- Django 小实例S1 简易学生选课管理系统 2 新建项目(project)并进行设置
Django 小实例S1 简易学生选课管理系统 第2节--新建项目(project)并进行设置 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 0 ...
- 浏览器 Proxy SwitchyOmega 插件设置代理访问内网服务器
使用Proxy SwitchyOmega 插件通过代理 直接访问到内网网站 一.使用场景 如下图所示,如果在电脑的网络设置中开启代理,每次更换代理就需要进入这里设置改变代理.且我们可能回需求到两个网页 ...
- [luogu5464]缩小社交圈
不难证明合法当且仅当满足一下两个条件: 1.每一个位置最多被覆盖两次(无环) 2.将选择的区间按左端点从小到大排序,对于每一个左端点,其之前的区间的最大右端点不小于其(连通) (关于第一个的充分性证明 ...
- [atAGC051C]Flipper
对于这一个平面用$a_{x,y}$来表示,即$(x,y)$为黑色则$a_{x,y}=1$,否则$a_{x,y}=0$,之后定义$a$能生成$b$当且仅当$a$能够通过若干次操作后得到$b$ 令$p_{ ...
- 【Tool】IntelliJ 搭建Node.js环境
IntelliJ IDEA 开发 Node.js 2019-07-29 14:12:34 by冲冲 1. 配置插件 在IDEA的 file -> setting -> Plugins, ...
- Stupid && 祖传Fortran代码救赎之路(编译Dll)
Stupid && 祖传Fortran代码救赎之路(编译Dll) gfortran编译动态库 在Windows平台下,Intel Fortran安装过于庞大且费事(现在集成到OneAP ...
- CF Edu Round 71
CF Edu Round 71 A There Are Two Types Of Burgers 贪心随便模拟一下 #include<iostream> #include<algor ...