二、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- ...
随机推荐
- Express 的基本使用(创建一个简单的服务器)
Express 的基本使用(创建一个简单的服务器) const express = require('express') // 创建服务器应用程序 // 相当于 http.creatServer co ...
- Spring Boot 面试总结
1.使用 Spring Boot 前景? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用 ...
- Swift-技巧(五)设置圆角的代码
摘要 实现控件圆角的代码时,会不假思索的写 cornerRadius 和 masksToBounds,因为搜索得到的设置圆角的代码就是这样.今天突发奇想,为什么要写 masksToBounds? 打个 ...
- myeclipse激活、破解教程
myeclipse安装注意事项 首先要下载jdk 配置jdk的环境变量 如果1和2 都打不开说明没有下载jdk文件,点击下载就可以了,点击1的时候会出现要求下载的界面,直接下载就可以了...下载完成之 ...
- 藏书馆App基于Rainbond实现云原生DevOps的实践
我们需要的不是精通Kubernetes的工程师,我们需要一款小白都能用好的管理工具. -- 厦门正观易知科技有限公司运维负责人 郭传壕 大家好,我是厦门正观易知科技有限公司运维负责人郭传壕. 藏书馆是 ...
- PHP 日期详细介绍
简介 你可以使用这些函数获取运行 PHP 的服务器的日期和时间, 也可以使用这些函数把日期和时间 格式化成不同格式的字符串. 日期和时间信息在 PHP 内部是以 64 位数字存储的, 它可以覆盖当前时 ...
- c语言实参与形参的区别
1 #include<stdio.h> 2 #include<math.h> 3 4 /** 5 * 形参和实参的功能是作数据传送. 6 * 函数调用中发生的数据传送是单向的. ...
- Codeforces 997D - Cycles in product(换根 dp)
Codeforces 题面传送门 & 洛谷题面传送门 一种换根 dp 的做法. 首先碰到这类题目,我们很明显不能真的把图 \(G\) 建出来,因此我们需要观察一下图 \(G\) 有哪些性质.很 ...
- Codeforces 772D - Varying Kibibits(高维差分+二项式定理维护 k 次方和)
Codeforces 题目传送门 & 洛谷题目传送门 首先很容易注意到一件事,那就是对于所有 \(f(S)\) 可能成为 \(x\) 的集合 \(S\),必定有 \(\forall y\in ...
- 51nod 1355 - 斐波那契的最小公倍数(Min-Max 容斥+莫比乌斯反演)
vjudge 题面传送门 首先我们知道斐波那契数列的 lcm 是不太容易计算的,但是它们的 gcd 非常容易计算--\(\gcd(f_x,f_y)=f_{\gcd(x,y)}\),该性质已在我的这篇博 ...