mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
集成fastDfs
This commit is contained in:
parent
c62acc288e
commit
6e171d20ed
@ -51,6 +51,18 @@
|
||||
<version>1.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<!--FastDfs-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-mock</artifactId>
|
||||
<version>2.0.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
<version>1.26.1-RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.java2nb.novel;
|
||||
|
||||
import com.github.tobato.fastdfs.FdfsClientConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
@ -18,6 +20,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
@EnableScheduling
|
||||
@EnableCaching
|
||||
@MapperScan(basePackages = {"com.java2nb.novel.mapper"})
|
||||
@Import(FdfsClientConfig.class)
|
||||
public class FrontNovelApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.java2nb.novel.core.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableMBeanExport;
|
||||
import org.springframework.jmx.support.RegistrationPolicy;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Configuration
|
||||
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
|
||||
public class FdfsConfiguration {
|
||||
}
|
@ -1,20 +1,60 @@
|
||||
package com.java2nb.novel.service.impl;
|
||||
|
||||
import com.github.tobato.fastdfs.domain.StorePath;
|
||||
import com.github.tobato.fastdfs.service.FastFileStorageClient;
|
||||
import com.java2nb.novel.core.utils.Constants;
|
||||
import com.java2nb.novel.core.utils.FileUtil;
|
||||
import com.java2nb.novel.service.FileService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "fastDfs")
|
||||
public class FastDfsFileServiceImpl implements FileService {
|
||||
|
||||
private final FastFileStorageClient storageClient;
|
||||
|
||||
@Value("${fdfs.webUrl}")
|
||||
private String webUrl;
|
||||
|
||||
@Override
|
||||
public String transFile(String picSrc, String picSavePath) {
|
||||
return null;
|
||||
|
||||
File file;
|
||||
String filePath = FileUtil.network2Local(picSrc, picSavePath, Constants.LOCAL_PIC_PREFIX);
|
||||
if (filePath.contains(Constants.LOCAL_PIC_PREFIX)) {
|
||||
file = new File(picSavePath + filePath);
|
||||
} else {
|
||||
//默认图片不存储
|
||||
return filePath;
|
||||
}
|
||||
|
||||
try {
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
StorePath storePath = storageClient.uploadFile(inputStream, file.length(),
|
||||
FilenameUtils.getExtension(file.getName()), null);
|
||||
//这里额外加上LOCAL_PIC_PREFIX路径,表明该图片是个人资源,而不是爬虫爬取的网络资源,不需要再次进行转换,
|
||||
// 实际访问时,再通过nginx的rewite指令来重写路径,去掉LOCAL_PIC_PREFIX
|
||||
return webUrl+Constants.LOCAL_PIC_PREFIX+storePath.getFullPath();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
//删除
|
||||
file.delete();
|
||||
}
|
||||
|
||||
return "/images/default.gif";
|
||||
}
|
||||
}
|
||||
|
16
novel-front/src/main/resources/application-fastdfs.yml
Normal file
16
novel-front/src/main/resources/application-fastdfs.yml
Normal file
@ -0,0 +1,16 @@
|
||||
#FastDfs的配置 ====================================
|
||||
#读取inputsream阻塞时间
|
||||
fdfs:
|
||||
connect-timeout: 600
|
||||
so-timeout: 1500
|
||||
#tracker地址
|
||||
trackerList: 127.0.0.1:22122
|
||||
#缩略图配置
|
||||
thumbImage:
|
||||
height: 150
|
||||
width: 150
|
||||
#通过nginx 访问地址
|
||||
webUrl: http://127.0.0.1/
|
||||
#获取连接池最大数量
|
||||
pool.max-total: 200
|
||||
|
@ -4,7 +4,7 @@ server:
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
include: alipay,oss
|
||||
include: alipay,oss,fastdfs
|
||||
|
||||
|
||||
rabbitmq:
|
||||
|
Loading…
x
Reference in New Issue
Block a user