041-云E办_后端用FASTDFS实现--更新头像
一、前提准备:
1.确保服务已经开启:
2.添加依赖
pom.xml
<!--FastDFS依赖-->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.29-SNAPSHOT</version>
</dependency>
3.在resources目录下添加配置文件
fdfs_client.conf
注1:tracker_server指向您自己IP地址和端口,1-n个
注2:除了tracker_server,其它配置项都是可选的
connect_timeout = 2
#网络超时
network_timeout = 30
#编码格式
charset = UTF-8
#tracker端口号
http.tracker_http_port = 8080
#防盗链功能
http.anti_steal_token = no
#秘钥
http.secret_key = FastDFS1234567890
#tracker ip:端口号
tracker_server = 192.168.服务器的IP地址.100:22122
#连接池配置
connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
二、如下代码:
1、编写FastDFS工具类
在工具类里面,进行相应的操作。最后通过工具类操作文件上传。
FastDFSUtils.java
package com.xxxx.server.utils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* FastDFSUtils工具类:
*/
public class FastDFSUtils {
private static Logger logger = LoggerFactory.getLogger(FastDFSUtils.class);
/**
* 1. 初始化客户端
* ClientGlobal.init 方法会读取配置文件,并初始化对应的属性。
*/
static {
try {
String filePath = new
ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
ClientGlobal.init(filePath);
} catch (Exception e) {
logger.error("FastDFS Client Init Fail!", e);
}
}
/**
* 3.生成Storage客户端
* @return
* @tadminows IOException
*/
private static StorageClient getStorageClient() throws IOException {
TrackerServer trackerServer = getTrackerServer();
StorageClient storageClient = new StorageClient(trackerServer, null);
return storageClient;
}
/**
* 2.生成Tracker服务器端
* @return
* @tadminows IOException
*/
private static TrackerServer getTrackerServer() throws IOException {
//跟踪器 客户
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
return trackerServer;
}
/**
* 上传文件
*
* @param file
* @return
*/
public static String[] upload(MultipartFile file) {
String name = file.getOriginalFilename();
logger.info("File Name: " + name);
long startTime = System.currentTimeMillis();
String[] uploadResults = null;
StorageClient storageClient = null;
try {
//获取storage客户端
storageClient = getStorageClient();
//上传
uploadResults = storageClient.upload_file(file.getBytes(),
name.substring(name.lastIndexOf(".") + 1),
null);
} catch (IOException e) {
logger.error("IO Exception when uploadind the file:" + name, e);
} catch (Exception e) {
logger.error("Non IO Exception when uploadind the file:" + name, e);
}
logger.info("upload_file time used:" + (System.currentTimeMillis() -
startTime) + " ms");
//验证上传结果
if (uploadResults == null && storageClient != null) {
logger.error("upload file fail, error code:" +
storageClient.getErrorCode());
}
//上传文件成功会返回 groupName。
logger.info("upload file successfully!!!" + "group_name:" +
uploadResults[0] + ", remoteFileName:" + " " + uploadResults[1]);
return uploadResults;
}
/**
* 获取文件信息
*
* @param groupName
* @param remoteFileName
* @return
*/
public static FileInfo getFile(String groupName, String remoteFileName) {
try {
StorageClient storageClient = getStorageClient();
return storageClient.get_file_info(groupName, remoteFileName);
} catch (IOException e) {
logger.error("IO Exception: Get File from Fast DFS failed", e);
} catch (Exception e) {
logger.error("Non IO Exception: Get File from Fast DFS failed", e);
}
return null;
}
/**
* 下载文件
*
* @param groupName
* @param remoteFileName
* @return
* 流上传文件
*/
public static InputStream downFile(String groupName, String remoteFileName)
{
try {
StorageClient storageClient = getStorageClient();
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
//返回的是数组,转换为流形式
InputStream ins = new ByteArrayInputStream(fileByte);
return ins;
} catch (IOException e) {
logger.error("IO Exception: Get File from Fast DFS failed", e);
} catch (Exception e) {
logger.error("Non IO Exception: Get File from Fast DFS failed", e);
}
return null;
}
/**
* 删除文件
*
* @param groupName
* @param remoteFileName
* @tadminows Exception
*/
public static void deleteFile(String groupName, String remoteFileName)
throws Exception {
StorageClient storageClient = getStorageClient();
int i = storageClient.delete_file(groupName, remoteFileName);
logger.info("delete file successfully!!!" + i);
}
/**
* 获取文件路径
*
* @return
*/
public static String getTrackerUrl() {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = null;
StorageServer storeStorage = null;
try {
trackerServer = trackerClient.getTrackerServer();
storeStorage = trackerClient.getStoreStorage(trackerServer);
} catch (Exception e) {
e.printStackTrace();
}
return "http://" + storeStorage.getInetSocketAddress().getHostString() + ":8888/";
}
}
2、controller
AdminInfoController
package com.xxxx.server.controller;
@ApiOperation(value = "更新用户头像")
@ApiImplicitParams({@ApiImplicitParam(name = "file", value = "头像", dataType = "MultipartFile")})
@PostMapping("/admin/userface")
public RespBean updateHrUserFace(MultipartFile file, Integer id, Authentication authentication) {
//获取上传文件地址
String[] fileAbsolutePath = FastDFSUtils.upload(file);
String url = FastDFSUtils.getTrackerUrl() + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
return adminService.updateAdminUserFace(url, id, authentication);
}
3、service
AdminServiceImpl
package com.xxxx.server.service.impl;
/**
* 更新用户头像
*
* @param url
* @param id
* @param authentication
* @return
*/
@Override
public RespBean updateAdminUserFace(String url, Integer id, Authentication authentication) {
Admin admin = adminMapper.selectById(id);
admin.setUserFace(url);
int result = adminMapper.updateById(admin);
if (1==result){
Admin principal = (Admin) authentication.getPrincipal();
principal.setUserFace(url);
//更新Authentication
SecurityContextHolder.getContext().setAuthentication(new
UsernamePasswordAuthenticationToken(admin,
authentication.getCredentials(),authentication.getAuthorities()));
return RespBean.success("更新成功!",url);
}
return RespBean.error("更新失败!");
}
三、测试: