FastFDS集成
This commit is contained in:
parent
a4d7fec604
commit
e8e5909abf
@ -49,10 +49,21 @@
|
||||
<!-- <version>1.27-SNAPSHOT</version>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>net.oschina.zcx7878</groupId>-->
|
||||
<!-- <artifactId>fastdfs-client-java</artifactId>-->
|
||||
<!-- <version>1.27.0.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>net.oschina.zcx7878</groupId>
|
||||
<artifactId>fastdfs-client-java</artifactId>
|
||||
<version>1.27.0.0</version>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
<version>1.26.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>3.0.1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
@ -278,9 +278,9 @@ public class FileOperation {
|
||||
}
|
||||
}
|
||||
for (String zipPath : fileEntryNameList) {
|
||||
if (FileUtil.isImageFile(FileUtil.getFileType(zipPath))) {
|
||||
if (FileUtil.isImageFile(FileUtil.getFileExtendName(zipPath))) {
|
||||
File file = new File(destDirPath + zipPath);
|
||||
File minFile = new File(destDirPath + FileUtil.getFileNameNotExtend(zipPath) + "_min." + FileUtil.getFileType(zipPath));
|
||||
File minFile = new File(destDirPath + FileUtil.getFileNameNotExtend(zipPath) + "_min." + FileUtil.getFileExtendName(zipPath));
|
||||
try {
|
||||
ImageOperation.thumbnailsImage(file, minFile, 300);
|
||||
} catch (IOException e) {
|
||||
@ -361,7 +361,7 @@ public class FileOperation {
|
||||
|
||||
public static long deleteFileFromDisk(String fileurl) {
|
||||
String fileUrl = PathUtil.getStaticPath() + fileurl;
|
||||
String extendName = FileUtil.getFileType(fileUrl);
|
||||
String extendName = FileUtil.getFileExtendName(fileUrl);
|
||||
String minFileUrl = fileUrl.replace("." + extendName, "_min." + extendName);
|
||||
long filesize = getFileSize(fileUrl);
|
||||
|
||||
|
@ -4,8 +4,6 @@ import com.qiwenshare.common.domain.UploadFile;
|
||||
import com.qiwenshare.common.util.PathUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -29,7 +27,26 @@ public abstract class Uploader {
|
||||
protected StandardMultipartHttpServletRequest request = null;
|
||||
|
||||
public abstract List<UploadFile> upload(HttpServletRequest request);
|
||||
|
||||
//
|
||||
//
|
||||
// protected static TrackerServer trackerServer = null;
|
||||
//
|
||||
// protected static StorageServer storageServer = null;
|
||||
//
|
||||
// static {
|
||||
// try {
|
||||
// //String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;
|
||||
// Properties properties = new Properties();
|
||||
// properties.setProperty("tracker_server ", "121.89.222.103");
|
||||
// properties.setProperty("http.tracker_http_port", "8090");
|
||||
// ClientGlobal.initByProperties(properties);
|
||||
// TrackerClient trackerClient = new TrackerClient();
|
||||
// trackerServer = trackerClient.getConnection();
|
||||
// storageServer = trackerClient.getStoreStorage(trackerServer);
|
||||
// } catch (Exception e) {
|
||||
// log.error("FastDFS Client Init Fail!",e);
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* 根据字符串创建本地目录 并按照日期建立子目录返回
|
||||
*
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.qiwenshare.common.upload.factory;
|
||||
|
||||
import com.qiwenshare.common.domain.UploadFile;
|
||||
import com.qiwenshare.common.upload.Uploader;
|
||||
import com.qiwenshare.common.upload.product.FastDFSUploader;
|
||||
|
||||
public class FastDFSUploaderFactory implements UploaderFactory {
|
||||
@Override
|
||||
public Uploader getUploader() {
|
||||
return new FastDFSUploader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uploader getUploader(UploadFile uploadFile) {
|
||||
return new FastDFSUploader(uploadFile);
|
||||
}
|
||||
}
|
@ -12,7 +12,6 @@ import com.qiwenshare.common.util.PathUtil;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -23,10 +22,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class AliyunOSSUploader extends Uploader {
|
||||
private static final Logger logger = LoggerFactory.getLogger(AliyunOSSUploader.class);
|
||||
@ -111,7 +107,7 @@ public class AliyunOSSUploader extends Uploader {
|
||||
|
||||
String fileName = getFileName(originalName);
|
||||
|
||||
String fileType = FileUtil.getFileType(originalName);
|
||||
String fileType = FileUtil.getFileExtendName(originalName);
|
||||
uploadFile.setFileName(fileName);
|
||||
uploadFile.setFileType(fileType);
|
||||
uploadFile.setTimeStampName(timeStampName);
|
||||
|
@ -9,7 +9,6 @@ import com.qiwenshare.common.util.PathUtil;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -93,7 +92,7 @@ public class ChunkUploader extends Uploader {
|
||||
String originalName = multipartfile.getOriginalFilename();
|
||||
|
||||
String fileName = getFileName(originalName);
|
||||
String fileType = FileUtil.getFileType(originalName);
|
||||
String fileType = FileUtil.getFileExtendName(originalName);
|
||||
uploadFile.setFileName(fileName);
|
||||
uploadFile.setFileType(fileType);
|
||||
uploadFile.setTimeStampName(timeStampName);
|
||||
|
@ -0,0 +1,240 @@
|
||||
package com.qiwenshare.common.upload.product;
|
||||
|
||||
import com.github.tobato.fastdfs.domain.StorePath;
|
||||
import com.github.tobato.fastdfs.service.AppendFileStorageClient;
|
||||
import com.qiwenshare.common.domain.UploadFile;
|
||||
import com.qiwenshare.common.upload.Uploader;
|
||||
import com.qiwenshare.common.util.FileUtil;
|
||||
import com.qiwenshare.common.util.PathUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class FastDFSUploader extends Uploader {
|
||||
@Resource
|
||||
AppendFileStorageClient defaultAppendFileStorageClient;
|
||||
|
||||
UploadFile uploadFile;
|
||||
|
||||
private static Map<String, Integer> CURRENT_UPLOAD_CHUNK_NUMBER = new HashMap<>();
|
||||
private static Map<String, Long> UPLOADED_SIZE = new HashMap<>();
|
||||
private static Map<String, String> STORE_PATH = new HashMap<>();
|
||||
private static Map<String, Boolean> LOCK_MAP = new HashMap<>();
|
||||
|
||||
public FastDFSUploader() {
|
||||
|
||||
}
|
||||
|
||||
public FastDFSUploader(UploadFile uploadFile) {
|
||||
this.uploadFile = uploadFile;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<UploadFile> upload(HttpServletRequest request) {
|
||||
log.info("开始上传upload");
|
||||
|
||||
List<UploadFile> saveUploadFileList = new ArrayList<>();
|
||||
this.request = (StandardMultipartHttpServletRequest) request;
|
||||
|
||||
boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
|
||||
if (!isMultipart) {
|
||||
UploadFile uploadFile = new UploadFile();
|
||||
uploadFile.setSuccess(0);
|
||||
uploadFile.setMessage("未包含文件上传域");
|
||||
saveUploadFileList.add(uploadFile);
|
||||
return saveUploadFileList;
|
||||
}
|
||||
DiskFileItemFactory dff = new DiskFileItemFactory();//1、创建工厂
|
||||
String savePath = getSaveFilePath();
|
||||
dff.setRepository(new File(savePath));
|
||||
|
||||
try {
|
||||
ServletFileUpload sfu = new ServletFileUpload(dff);//2、创建文件上传解析器
|
||||
sfu.setSizeMax(this.maxSize * 1024L);
|
||||
sfu.setHeaderEncoding("utf-8");//3、解决文件名的中文乱码
|
||||
Iterator<String> iter = this.request.getFileNames();
|
||||
while (iter.hasNext()) {
|
||||
|
||||
saveUploadFileList = doUpload(savePath, iter);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
UploadFile uploadFile = new UploadFile();
|
||||
uploadFile.setSuccess(1);
|
||||
uploadFile.setMessage("未知错误");
|
||||
saveUploadFileList.add(uploadFile);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
log.info("结束上传");
|
||||
return saveUploadFileList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private List<UploadFile> doUpload(String savePath, Iterator<String> iter) throws IOException{
|
||||
|
||||
List<UploadFile> saveUploadFileList = new ArrayList<>();
|
||||
|
||||
try {
|
||||
MultipartFile multipartfile = this.request.getFile(iter.next());
|
||||
boolean uploadResult = uploadFileChunk(multipartfile);
|
||||
if (!uploadResult) {
|
||||
|
||||
}
|
||||
String timeStampName = getTimeStampName();
|
||||
String originalName = multipartfile.getOriginalFilename();
|
||||
|
||||
String fileName = getFileName(originalName);
|
||||
|
||||
String fileType = FileUtil.getFileExtendName(originalName);
|
||||
uploadFile.setFileName(fileName);
|
||||
uploadFile.setFileType(fileType);
|
||||
uploadFile.setTimeStampName(timeStampName);
|
||||
|
||||
|
||||
String confFilePath = savePath + FILE_SEPARATOR + uploadFile.getIdentifier() + "." + "conf";
|
||||
File confFile = new File(PathUtil.getStaticPath() + FILE_SEPARATOR + confFilePath);
|
||||
|
||||
|
||||
|
||||
boolean isComplete = checkUploadStatus(uploadFile, confFile);
|
||||
if (isComplete) {
|
||||
log.info("分片上传完成");
|
||||
|
||||
uploadFile.setUrl(STORE_PATH.get(uploadFile.getIdentifier()));
|
||||
uploadFile.setSuccess(1);
|
||||
uploadFile.setMessage("上传成功");
|
||||
} else {
|
||||
uploadFile.setSuccess(0);
|
||||
uploadFile.setMessage("未完成");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("上传出错:" + e);
|
||||
}
|
||||
|
||||
uploadFile.setIsOSS(1);
|
||||
|
||||
uploadFile.setFileSize(uploadFile.getTotalSize());
|
||||
saveUploadFileList.add(uploadFile);
|
||||
return saveUploadFileList;
|
||||
}
|
||||
|
||||
public boolean uploadFileChunk(MultipartFile multipartFile) {
|
||||
|
||||
// 存储在fastdfs不带组的路径
|
||||
// String noGroupPath = "";
|
||||
log.info("当前文件的Md5:{}", uploadFile.getIdentifier());
|
||||
|
||||
// 真正的拥有者
|
||||
boolean currOwner = false;
|
||||
|
||||
try {
|
||||
|
||||
Boolean lock = LOCK_MAP.get(uploadFile.getIdentifier());
|
||||
//
|
||||
// if (lock == null){
|
||||
// log.info("请求块锁失败");
|
||||
// return false;
|
||||
// }
|
||||
if (lock != null && lock) {
|
||||
return false;
|
||||
}
|
||||
LOCK_MAP.put(uploadFile.getIdentifier(), true);
|
||||
// 写入锁的当前拥有者
|
||||
currOwner = true;
|
||||
|
||||
// redis中记录当前应该传第几块(从0开始)
|
||||
Integer currentChunkInRedis = CURRENT_UPLOAD_CHUNK_NUMBER.get(uploadFile.getIdentifier());
|
||||
|
||||
log.info("当前块的大小:{}", uploadFile.getCurrentChunkSize());
|
||||
if (currentChunkInRedis == null) {
|
||||
currentChunkInRedis = 1;
|
||||
}
|
||||
|
||||
//此段代码保证顺序,如果满足条件则返回失败
|
||||
if (uploadFile.getChunkNumber() < currentChunkInRedis) {
|
||||
log.info("当前文件块已上传");
|
||||
return false;
|
||||
} else if (uploadFile.getChunkNumber() > currentChunkInRedis) {
|
||||
log.info("当前文件块需要等待上传,稍后请重试");
|
||||
return false;
|
||||
}
|
||||
|
||||
log.info("***********开始上传第{}块**********", uploadFile.getChunkNumber());
|
||||
StorePath storePath = null;
|
||||
|
||||
try {
|
||||
|
||||
if (uploadFile.getChunkNumber() <= 1) {
|
||||
log.info("上传第一块");
|
||||
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber() + 1);
|
||||
try {
|
||||
storePath = defaultAppendFileStorageClient.uploadAppenderFile("default_group", multipartFile.getInputStream(),
|
||||
multipartFile.getSize(), FileUtil.getFileExtendName(multipartFile.getOriginalFilename()));
|
||||
// 记录第一个分片上传的大小
|
||||
UPLOADED_SIZE.put(uploadFile.getIdentifier(), uploadFile.getCurrentChunkSize());
|
||||
log.info("第一块上传完成");
|
||||
if (storePath == null) {
|
||||
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
|
||||
log.info("获取远程文件路径出错");
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
|
||||
log.error("初次上传远程文件出错", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
STORE_PATH.put(uploadFile.getIdentifier(), storePath.getPath());
|
||||
log.info("上传文件 result = {}", storePath.getPath());
|
||||
} else {
|
||||
log.info("上传第{}块:" + uploadFile.getChunkNumber());
|
||||
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber() + 1);
|
||||
String path = STORE_PATH.get(uploadFile.getIdentifier());
|
||||
if (path == null) {
|
||||
log.error("无法获取已上传服务器文件地址");
|
||||
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Long alreadySize = UPLOADED_SIZE.get(uploadFile.getIdentifier());
|
||||
// 追加方式实际实用如果中途出错多次,可能会出现重复追加情况,这里改成修改模式,即时多次传来重复文件块,依然可以保证文件拼接正确
|
||||
defaultAppendFileStorageClient.modifyFile("default_group", path, multipartFile.getInputStream(),
|
||||
multipartFile.getSize(), alreadySize);
|
||||
// 记录分片上传的大小
|
||||
UPLOADED_SIZE.put(uploadFile.getIdentifier(), alreadySize + multipartFile.getSize());
|
||||
log.info("第{}块更新完成", uploadFile.getChunkNumber());
|
||||
} catch (Exception e) {
|
||||
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
|
||||
log.error("更新远程文件出错", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("上传文件错误", e);
|
||||
return false;
|
||||
}
|
||||
} finally {
|
||||
// 锁的当前拥有者才能释放块上传锁
|
||||
if (currOwner) {
|
||||
LOCK_MAP.put(uploadFile.getIdentifier(), false);
|
||||
//JedisConfig.setString(chunkLockName, "0");
|
||||
}
|
||||
}
|
||||
log.info("***********第{}块上传成功**********", uploadFile.getChunkNumber());
|
||||
return true;
|
||||
}
|
||||
}
|
@ -71,7 +71,7 @@ public class NormalUploader extends Uploader {
|
||||
|
||||
String fileName = getFileName(originalName);
|
||||
|
||||
String fileType = FileUtil.getFileType(originalName);
|
||||
String fileType = FileUtil.getFileExtendName(originalName);
|
||||
uploadFile.setFileName(fileName);
|
||||
uploadFile.setFileType(fileType);
|
||||
uploadFile.setTimeStampName(timeStampName);
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.qiwenshare.common.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -74,7 +73,7 @@ public class FileUtil {
|
||||
* @param fileName 文件名
|
||||
* @return 文件扩展名
|
||||
*/
|
||||
public static String getFileType(String fileName) {
|
||||
public static String getFileExtendName(String fileName) {
|
||||
if (fileName.lastIndexOf(".") == -1) {
|
||||
return "";
|
||||
//这里暂时用jpg,后续应该去获取真实的文件类型
|
||||
@ -89,7 +88,7 @@ public class FileUtil {
|
||||
* @return 文件名(不带扩展名)
|
||||
*/
|
||||
public static String getFileNameNotExtend(String fileName) {
|
||||
String fileType = getFileType(fileName);
|
||||
String fileType = getFileExtendName(fileName);
|
||||
return fileName.replace("." + fileType, "");
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,403 @@
|
||||
//package com.qiwenshare.common.util;
|
||||
//
|
||||
//import cn.hutool.core.util.StrUtil;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//import redis.clients.jedis.Jedis;
|
||||
//import redis.clients.jedis.JedisPool;
|
||||
//import redis.clients.jedis.JedisPoolConfig;
|
||||
//
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * @Description: JedisConfig
|
||||
// * @ClassName: JedisConfig
|
||||
// * @Author: xxx
|
||||
// * @Date: 2019/12/31 16:23
|
||||
// * @Version: 1.0
|
||||
// */
|
||||
//public class JedisConfig {
|
||||
//
|
||||
// private static Logger logger = LoggerFactory.getLogger(JedisConfig.class);
|
||||
//
|
||||
// protected static final ThreadLocal<Jedis> threadLocalJedis = new ThreadLocal<>();
|
||||
// private static JedisPool jedisPool;
|
||||
// /**
|
||||
// * Redis服务器IP
|
||||
// */
|
||||
// private static String ADDR_ARRAY = "192.168.1.122";
|
||||
//
|
||||
// /**
|
||||
// * Redis的端口号
|
||||
// */
|
||||
// private static int PORT = 6379;
|
||||
//
|
||||
// /**
|
||||
// * 访问密码
|
||||
// */
|
||||
// private static String AUTH = "123456";
|
||||
//
|
||||
// /**
|
||||
// * 可用连接实例的最大数目,默认值为8
|
||||
// * 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
|
||||
// */
|
||||
// private static int MAX_ACTIVE = -1;
|
||||
//
|
||||
// /**
|
||||
// * 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
|
||||
// */
|
||||
// private static int MAX_IDLE = 16;
|
||||
//
|
||||
// /**
|
||||
// * 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
|
||||
// */
|
||||
// private static int MAX_WAIT = 1000 * 5;
|
||||
//
|
||||
// // 超时时间
|
||||
// private static int TIMEOUT = 1000 * 5;
|
||||
//
|
||||
// /**
|
||||
// * 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
|
||||
// */
|
||||
// private static boolean TEST_ON_BORROW = true;
|
||||
//
|
||||
// /**
|
||||
// * redis过期时间,以秒为单位
|
||||
// */
|
||||
// /**
|
||||
// * 一小时
|
||||
// */
|
||||
// public final static int EXRP_HOUR = 60 * 60;
|
||||
// /**
|
||||
// * 一天
|
||||
// */
|
||||
// public final static int EXRP_DAY = 60 * 60 * 24;
|
||||
// /**
|
||||
// * 一个月
|
||||
// */
|
||||
// public final static int EXRP_MONTH = 60 * 60 * 24 * 30;
|
||||
//
|
||||
// public JedisConfig() {
|
||||
// }
|
||||
//
|
||||
// static {
|
||||
// initialPool();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 初始化Redis连接池,注意一定要在使用前初始化一次,一般在项目启动时初始化就行了
|
||||
// */
|
||||
// public static JedisPool initialPool() {
|
||||
// JedisPool jp = null;
|
||||
// try {
|
||||
// JedisPoolConfig config = new JedisPoolConfig();
|
||||
// config.setMaxTotal(MAX_ACTIVE);
|
||||
// config.setMaxIdle(MAX_IDLE);
|
||||
// config.setMaxWaitMillis(MAX_WAIT);
|
||||
// config.setTestOnBorrow(TEST_ON_BORROW);
|
||||
// config.setTestOnCreate(true);
|
||||
// config.setTestWhileIdle(true);
|
||||
// config.setTestOnReturn(true);
|
||||
// config.setNumTestsPerEvictionRun(-1);
|
||||
// jp = new JedisPool(config, ADDR_ARRAY, PORT, TIMEOUT, AUTH);
|
||||
// jedisPool = jp;
|
||||
// threadLocalJedis.set(getJedis());
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// }
|
||||
// return jp;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取Jedis实例,一定先初始化
|
||||
// * @return Jedis
|
||||
// */
|
||||
// public static Jedis getJedis() {
|
||||
// boolean success = false;
|
||||
// Jedis jedis = null;
|
||||
// int i=0;
|
||||
// while (!success) {
|
||||
// i++;
|
||||
// try {
|
||||
// if (jedisPool != null) {
|
||||
// jedis = threadLocalJedis.get();
|
||||
// if (jedis == null){
|
||||
// jedis = jedisPool.getResource();
|
||||
// }else {
|
||||
// if(!jedis.isConnected() && !jedis.getClient().isBroken()){
|
||||
// threadLocalJedis.set(null);
|
||||
// jedis = jedisPool.getResource();
|
||||
// }
|
||||
// return jedis;
|
||||
// }
|
||||
// }else {
|
||||
// throw new RuntimeException("redis连接池初始化失败");
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// System.out.println(Thread.currentThread().getName()+":第"+i+"次获取失败!!!");
|
||||
// success = false;
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// }
|
||||
// if (jedis != null){
|
||||
// success = true;
|
||||
// }
|
||||
// if (i >= 10 && i < 20){
|
||||
// try {
|
||||
// Thread.sleep(1000);
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// if (i >= 20 && i < 30){
|
||||
// try {
|
||||
// Thread.sleep(2000);
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// if (i >= 30 && i < 40){
|
||||
// try {
|
||||
// Thread.sleep(3000);
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// if (i >= 40){
|
||||
// System.out.println("redis彻底连不上了~~~~(>_<)~~~~");
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
// if (threadLocalJedis.get() == null) {
|
||||
// threadLocalJedis.set(jedis);
|
||||
// }
|
||||
// return jedis;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 设置 String
|
||||
// * @param key
|
||||
// * @param value
|
||||
// */
|
||||
// public static void setString(String key, String value) {
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// value = StrUtil.isBlank(value) ? "" : value;
|
||||
// jo = getJedis();
|
||||
// jo.set(key, value);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis服务器异常");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 设置 过期时间
|
||||
// * @param key
|
||||
// * @param seconds 以秒为单位
|
||||
// * @param value
|
||||
// */
|
||||
// public static void setString(String key, int seconds, String value) {
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// value = StrUtil.isBlank(value) ? "" : value;
|
||||
// jo = getJedis();
|
||||
// jo.setex(key, seconds, value);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis服务器异常");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取String值
|
||||
// * @param key
|
||||
// * @return value
|
||||
// */
|
||||
// public static String getString(String key) {
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// if (jo == null || !jo.exists(key)) {
|
||||
// return null;
|
||||
// }
|
||||
// return jo.get(key);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static long incrBy(String key, long integer) {
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// return jo.incrBy(key, integer);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static long decrBy(String key, long integer) {
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// return jo.decrBy(key, integer);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 删除多个key
|
||||
// */
|
||||
// public static long delKeys(String [] keys){
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// return jo.del(keys);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 删除单个key
|
||||
// */
|
||||
// public static long delKey(String key){
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// return jo.del(key);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 添加到队列尾
|
||||
// */
|
||||
// public static long rpush(String key,String node){
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// return jo.rpush(key,node);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 删除list元素
|
||||
// */
|
||||
// public static long delListNode(String key,int count,String value){
|
||||
// Jedis jo = null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// return jo.lrem(key,count,value);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取所有list
|
||||
// */
|
||||
// public static List getListAll(String key){
|
||||
// Jedis jo = null;
|
||||
// List list=null;
|
||||
// try {
|
||||
// jo = getJedis();
|
||||
// list= jo.lrange(key,0,-1);
|
||||
// } catch (Exception e) {
|
||||
// threadLocalJedis.set(null);
|
||||
// e.printStackTrace();
|
||||
// logger.error("redis服务器异常",e);
|
||||
// throw new RuntimeException("redis操作错误");
|
||||
// } finally {
|
||||
// if (jo != null) {
|
||||
// close(jo);
|
||||
// }
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 清理缓存redis
|
||||
// */
|
||||
// public void cleanLoacl(Jedis jo){
|
||||
// threadLocalJedis.set(null);
|
||||
// close(jo);
|
||||
// }
|
||||
//
|
||||
// public static void close(Jedis jedis) {
|
||||
// if (threadLocalJedis.get() == null && jedis != null){
|
||||
// jedis.close();
|
||||
// }
|
||||
// }
|
||||
//}
|
@ -0,0 +1,14 @@
|
||||
package com.qiwenshare.file.config;
|
||||
|
||||
import com.github.tobato.fastdfs.FdfsClientConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableMBeanExport;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.jmx.support.RegistrationPolicy;
|
||||
|
||||
@Configuration
|
||||
@Import(FdfsClientConfig.class)
|
||||
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
|
||||
public class FdfsConfig {
|
||||
|
||||
}
|
@ -12,6 +12,8 @@ public class QiwenFileConfig {
|
||||
|
||||
private boolean shareMode;
|
||||
|
||||
private String storageType;
|
||||
|
||||
private AliyunConfig aliyun = new AliyunConfig();
|
||||
|
||||
}
|
||||
|
@ -309,7 +309,7 @@ public class FileController {
|
||||
}else{
|
||||
|
||||
userFile.setIsDir(0);
|
||||
userFile.setExtendName(FileUtil.getFileType(totalFileUrl));
|
||||
userFile.setExtendName(FileUtil.getFileExtendName(totalFileUrl));
|
||||
userFile.setFileName(FileUtil.getFileNameNotExtend(currentFile.getName()));
|
||||
tempFileBean.setFileSize(currentFile.length());
|
||||
tempFileBean.setTimeStampName(FileUtil.getFileNameNotExtend(currentFile.getName()));
|
||||
|
@ -76,7 +76,7 @@ public class FiletransferController {
|
||||
userFile.setFilePath(uploadFileDto.getFilePath());
|
||||
String fileName = uploadFileDto.getFilename();
|
||||
userFile.setFileName(fileName.substring(0, fileName.lastIndexOf(".")));
|
||||
userFile.setExtendName(FileUtil.getFileType(fileName));
|
||||
userFile.setExtendName(FileUtil.getFileExtendName(fileName));
|
||||
userFile.setDeleteFlag(0);
|
||||
userFile.setIsDir(0);
|
||||
userFile.setUploadTime(DateUtil.getCurrentTime());
|
||||
|
@ -87,7 +87,7 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
|
||||
AliyunOSSDelete.deleteObject(qiwenFileConfig.getAliyun().getOss(), fileBean.getFileUrl().substring(1));
|
||||
} else {
|
||||
FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl());
|
||||
if (FileUtil.isImageFile(FileUtil.getFileType(fileBean.getFileUrl()))) {
|
||||
if (FileUtil.isImageFile(FileUtil.getFileExtendName(fileBean.getFileUrl()))) {
|
||||
FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl().replace(fileBean.getTimeStampName(), fileBean.getTimeStampName() + "_min"));
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import com.qiwenshare.common.domain.UploadFile;
|
||||
import com.qiwenshare.common.upload.factory.AliyunOSSUploaderFactory;
|
||||
import com.qiwenshare.common.upload.factory.ChunkUploaderFactory;
|
||||
import com.qiwenshare.common.upload.Uploader;
|
||||
import com.qiwenshare.common.upload.factory.FastDFSUploaderFactory;
|
||||
import com.qiwenshare.file.api.IFiletransferService;
|
||||
|
||||
import com.qiwenshare.common.domain.AliyunOSS;
|
||||
@ -45,6 +46,7 @@ public class FiletransferService implements IFiletransferService {
|
||||
@Override
|
||||
public void uploadFile(HttpServletRequest request, UploadFileDTO UploadFileDto, Long userId) {
|
||||
AliyunOSS oss = qiwenFileConfig.getAliyun().getOss();
|
||||
String storyType = qiwenFileConfig.getStorageType();
|
||||
request.setAttribute("oss", oss);
|
||||
Uploader uploader;
|
||||
UploadFile uploadFile = new UploadFile();
|
||||
@ -56,6 +58,8 @@ public class FiletransferService implements IFiletransferService {
|
||||
uploadFile.setCurrentChunkSize(UploadFileDto.getCurrentChunkSize());
|
||||
if (oss.isEnabled()) {
|
||||
uploader = new AliyunOSSUploaderFactory().getUploader(uploadFile);
|
||||
} else if ("FastFDS".equals(storyType)) {
|
||||
uploader = new FastDFSUploaderFactory().getUploader(uploadFile);
|
||||
} else {
|
||||
uploader = new ChunkUploaderFactory().getUploader(uploadFile);
|
||||
}
|
||||
|
@ -51,6 +51,8 @@ mybatis-plus.global-config.banner=false
|
||||
|
||||
qiwen-file.remote-login=false
|
||||
qiwen-file.share-mode=false
|
||||
#FastFDS
|
||||
qiwen-file.storage-type=FastFDS
|
||||
|
||||
#是否启用阿里云oss
|
||||
qiwen-file.aliyun.oss.enabled=false
|
||||
@ -62,3 +64,10 @@ qiwen-file.aliyun.oss.bucket-name=
|
||||
#阿里云oss绑定域名
|
||||
qiwen-file.aliyun.oss.domain=
|
||||
|
||||
|
||||
fdfs.so-timeout=1501
|
||||
fdfs.connect-timeout=601
|
||||
fdfs.thumb-image.width=150
|
||||
fdfs.thumb-image.height=150
|
||||
fdfs.tracker-list=121.89.222.103:22122
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user