使用FileListener回调

This commit is contained in:
昕有灵犀
2018-07-09 16:10:15 +08:00
parent 982c5831ec
commit 5ff703f21c
27 changed files with 1134 additions and 265 deletions

Binary file not shown.

View File

@@ -35,12 +35,12 @@ http://xyfs.mydoc.io/
#### 采用技术
- 1.Spring Boot、MVC、AOP、Actuator、Swagger
- 2.Dbcp2/Druid
- 2.Dbcp2Druid数据库连接池2种模式可选
- 3.JPA/Hibernate
- 4.Ehcache
- 5.HTML、Layer、Bootstrap
- 6.Tomcat
- 7.MySQL
- 7.MySQL、Oracle、SQLite3种数据库模式可选
- 8.Gradle
- 9.Nginx

View File

@@ -9,8 +9,9 @@ storage.location = uploadfiledir
# \u662f\u5426\u91cd\u65b0\u547d\u540d\u6587\u4ef6\u540d\uff0c\u5982\u679c\u662f\u4e2a\u4eba\u6587\u4ef6\u5219\u91cd\u547d\u540d\u4e3a\uff1ausername_ + \u539f\u6587\u4ef6\u540d\uff1b\u5982\u679c\u662f\u7fa4\u7ec4\u6587\u4ef6\u5219\u91cd\u547d\u540d\u4e3a\uff1agroupid_ + \u539f\u6587\u4ef6\u540d #
storage.rename = true
# \u5b58\u50a8\u6e90\uff0c\u7c7b\u578b\u6709\uff1axyfs\\Seafile\\FastDFS\\SeaweedFS\\MongoDB\\aliOSS\\qiniu\\CFS #
storage.todisk = true
# \u5b58\u50a8\u6e90.\u53ef\u591a\u9009.\u53ef\u4ee5\u540c\u65f6\u50a8\u5b58\u591a\u4e2a\u6570\u636e\u6e90,\u4fbf\u4e8e\u5907\u4efd #
# \u7c7b\u578b\u6709\uff1axyfs\\Seafile\\FastDFS\\SeaweedFS\\MongoDB\\aliOSS\\qiniu\\CFS #
storage.todisk = true
storage.toqiniu = true
storage.tofastdfs = false
storage.tomongodb = false
@@ -18,6 +19,11 @@ storage.toseaweedfs = false
storage.toalioss = false
storage.tocfs = false
# \u7528\u4e8e\u8bbe\u7f6e\u4e0b\u8f7d\u7684\u6e90 ,\u5355\u9009 #
# \u7c7b\u578b\u6709\uff1axyfs\\Seafile\\FastDFS\\SeaweedFS\\MongoDB\\aliOSS\\qiniu\\CFS #
storage.downloadfrom = xyfs
# xyfs\u5b58\u50a8\u914d\u7f6e #
storage.diskprefix = http://localhost:9091/files/
# \u4e03\u725b\u5b58\u50a8\u914d\u7f6e #
@@ -44,7 +50,14 @@ storage.fastdfstrackerserver = 192.168.17.112:22122
# SeaweedFS \u914d\u7f6e #
storage.seaweedfshost = localhost
storage.seaweedfsport = 9333
storage.seaweedfstimeout = 5000
storage.seaweedfstimeout = 10
# ali OSS \u914d\u7f6e #
storage.aliendpoint = ""
storage.aliaccesskeyid = ""
storage.aliaccesskeysecret = ""
storage.alibucketname = ""
storage.alidownloadkey = ""
# \u7b2c\u4e00\u6b21\u4f7f\u7528\u7684\u914d\u7f6e\uff1a\u81ea\u52a8\u521b\u5efa\u6570\u636e\u5e93\u8868\uff0c\u5982\u679c\u662f\u8fd0\u884cSQL\u811a\u672c\u5219\u65e0\u9700\u4f7f\u7528\u6b64\u914d\u7f6e #
# spring.jpa.hibernate.ddl-auto = create
@@ -62,6 +75,12 @@ spring.jpa.database-platform= xy.FileSystem.Dialect.SQLiteDialect
#spring.datasource.username=root
#spring.datasource.password=mysql
# oracle\u6570\u636e\u6e90 #
#spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:torcl
#spring.datasource.username=center
#spring.datasource.password=centerDB12345
# dbcp2\u8fde\u63a5\u6c60\u914d\u7f6e #
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.max-wait-millis=10000
@@ -86,7 +105,6 @@ spring.datasource.dbcp2.connection-properties=characterEncoding=utf8
#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#spring.datasource.filters=stat,wall,log4j
# spring jackson \u65f6\u95f4\u914d\u7f6e #
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.joda-date-time-format=yyyy-MM-dd HH:mm:ss
@@ -97,6 +115,5 @@ spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=1024MB
spring.servlet.multipart.max-request-size=1024MB
# oss \u65e5\u5fd7\u914d\u7f6e #
logging.config=classpath:logback.xml

View File

@@ -33,7 +33,7 @@ dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile 'mysql:mysql-connector-java'
compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.4.0'
compile group: 'com.alibaba', name: 'druid', version: '1.1.10'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.23.1'
@@ -49,6 +49,13 @@ dependencies {
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.5'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.5'
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.8.0'
compile group: 'org.lokra.seaweedfs', name: 'seaweedfs-client', version: '0.7.3.RELEASE'
compile group: 'com.github.tobato', name: 'fastdfs-client', version: '1.26.2'
compile group: 'com.aliyun.oss', name: 'aliyun-sdk-oss', version: '3.1.0'
compile group: 'org.jdom', name: 'jdom', version: '2.0.2'
compile group: 'com.aliyun', name: 'aliyun-java-sdk-ram', version: '3.0.0'
compile group: 'com.aliyun', name: 'aliyun-java-sdk-sts', version: '3.0.0'
compile group: 'com.aliyun', name: 'aliyun-java-sdk-ecs', version: '4.9.2'
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile('com.jayway.jsonpath:json-path')

View File

@@ -0,0 +1,10 @@
package xy.FileSystem.Cache;
import com.qiniu.util.Auth;
public class FileCache {
//缓存七牛认证信息
public static Auth qiniuAuth;
}

View File

@@ -0,0 +1,5 @@
package xy.FileSystem.Controller;
public class FileController {
}

View File

@@ -2,12 +2,12 @@ package xy.FileSystem.Controller;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
@@ -24,139 +24,158 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import xy.FileSystem.Cache.UsesCache;
import xy.FileSystem.Entity.Diskfile;
import xy.FileSystem.Entity.DiskfileRepository;
import xy.FileSystem.Exception.StorageFileNotFoundException;
import xy.FileSystem.File.FileListener;
import xy.FileSystem.File.StoreSource;
import xy.FileSystem.File.UploadFileExt;
import xy.FileSystem.Propert.StorageProperties;
import xy.FileSystem.Service.FileSystemStorageService;
import xy.FileSystem.Service.QiniuService;
@Controller
public class FileUploadController {
@Autowired
private FileSystemStorageService storageService;
@Autowired
private QiniuService qiniuService;
@Autowired
private StorageProperties prop;
@Autowired
private DiskfileRepository diskfileRepository;
// @Autowired
// public FileUploadController(FileSystemStorageService storageService) {
// this.storageService = storageService;
// }
@Autowired
private FileSystemStorageService storageService;
@Autowired
private StorageProperties prop;
@Autowired
private DiskfileRepository diskfileRepository;
ExecutorService executorService = Executors.newFixedThreadPool(5);
@GetMapping("/")
public String listUploadedFiles(Model model) throws IOException {
@GetMapping("/")
public String listUploadedFiles(Model model) throws IOException {
System.out.println("UsesCache.files:"+UsesCache.files);
System.out.println("UsesCache.usedspace:"+UsesCache.usedspace);
model.addAttribute("files", storageService.loadAll().map(
path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
"serveFile", path.getFileName().toString()).build().toString())
.collect(Collectors.toList()));
model.addAttribute("files",
storageService.loadAll()
.map(path -> MvcUriComponentsBuilder
.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
.build().toString())
.collect(Collectors.toList()));
return "uploadForm";
}
@GetMapping("/files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
Resource file = storageService.loadAsResource(filename);
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
@PostMapping("/")
public String handleFileUpload(MultipartHttpServletRequest request,
RedirectAttributes redirectAttributes,@RequestParam int appid,
@RequestParam String username,@RequestParam String groupid) {
Iterator<String> itr = request.getFileNames();
MultipartFile file = request.getFile(itr.next()); //只取一个文件,不取多个
String fileName = file.getOriginalFilename();
if (prop.isRename()){
fileName = username +"_"+ file.getOriginalFilename();
if (groupid!=null && !groupid.isEmpty()){
fileName = groupid +"_"+ file.getOriginalFilename();
}
}
storageService.store(file,fileName);
if (prop.isToqiniu()){
try {
qiniuService.store(file.getBytes(), fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
dbSave(appid,username,groupid,file,fileName);
redirectAttributes.addFlashAttribute("message",
"Successfully uploaded: " + file.getOriginalFilename());
return "redirect:/";
}
public void dbSave(int appid, String username,String groupid, MultipartFile file,String fileName) {
//数据库存储
Diskfile dbFile = new Diskfile();
String fileId = UUID.randomUUID().toString();
dbFile.setFileid(fileId);
dbFile.setAppid(appid);
dbFile.setFileExt(file.getContentType());
dbFile.setFileFlag("1");
dbFile.setFileName(fileName);
dbFile.setFileSize(BigInteger.valueOf(file.getSize()));
dbFile.setIspublic("1");
dbFile.setUploadDate(new Date());
dbFile.setUploadUser(username);
dbFile.setUrldisk(prop.getDiskprefix()+fileName);
//更新缓存
UsesCache.files++;
UsesCache.usedspace = UsesCache.usedspace + file.getSize();
if (prop.isToqiniu()){
dbFile.setUrlqiniu(prop.getQiniuprefix() + fileName);
}
diskfileRepository.save(dbFile);
return "uploadForm";
}
// public String handleFileUpload(@RequestParam("file") MultipartFile file,
// RedirectAttributes redirectAttributes) {
//
// storageService.store(file);
// redirectAttributes.addFlashAttribute("message",
// "Successfully uploaded: " + file.getOriginalFilename());
//
// return "redirect:/";
// }
@PostMapping("/upload")
public String upload(MultipartHttpServletRequest request,RedirectAttributes redirectAttributes) {
Iterator<String> itr = request.getFileNames();
MultipartFile mpf = request.getFile(itr.next());
@GetMapping("/files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
storageService.store(mpf);
redirectAttributes.addFlashAttribute("message",
"Successfully uploaded: " + mpf.getOriginalFilename());
Resource file = storageService.loadAsResource(filename);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
.body(file);
}
return "redirect:/";
@PostMapping("/")
public String handleFileUpload(MultipartHttpServletRequest request, RedirectAttributes redirectAttributes,
@RequestParam int appid, @RequestParam String username, @RequestParam String groupid) {
Iterator<String> itr = request.getFileNames();
MultipartFile file = request.getFile(itr.next()); // 只取一个文件,不取多个
String fileName = file.getOriginalFilename();
}
if (prop.isRename()) {
fileName = username + "_" + file.getOriginalFilename();
if (groupid != null && !groupid.isEmpty()) {
fileName = groupid + "_" + file.getOriginalFilename();
}
}
@ExceptionHandler(StorageFileNotFoundException.class)
public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc) {
return ResponseEntity.notFound().build();
}
final String finalFilename = fileName;
final String finalFilePath = "";
doUpload(file, finalFilename, finalFilePath);
dbSave(appid, username, groupid, file, fileName);
redirectAttributes.addFlashAttribute("message", "Successfully uploaded: " + file.getOriginalFilename());
return "redirect:/";
}
public void doUpload(MultipartFile file, final String finalFilename, final String filePath) {
// 磁盘存储
if (prop.isTodisk()) {
executorService.execute(new Runnable() {
@Override
public void run() {
storageService.store(file, finalFilename);
}
});
}
//第三方存储
UploadFileExt ufe;
try {
ufe = new UploadFileExt(finalFilename, file.getBytes(), file.getContentType(), file.getSize());
if (ufe != null) {
for (FileListener fl : StoreSource.getListensers()) {
executorService.execute(new Runnable() {
@Override
public void run() {
fl.Store(ufe);
}
});
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void dbSave(int appid, String username, String groupid, MultipartFile file, String fileName) {
// 数据库存储
Diskfile dbFile = new Diskfile();
String fileId = UUID.randomUUID().toString();
dbFile.setFileid(fileId);
dbFile.setAppid(appid);
dbFile.setFileExt(file.getContentType());
dbFile.setFileFlag("1");
dbFile.setFileName(fileName);
dbFile.setFileSize(BigInteger.valueOf(file.getSize()));
dbFile.setIspublic("1");
dbFile.setUploadDate(new Date());
dbFile.setUploadUser(username);
dbFile.setUrldisk(prop.getDiskprefix() + fileName);
// 更新缓存
UsesCache.files++;
UsesCache.usedspace = UsesCache.usedspace + file.getSize();
if (prop.isToqiniu()) {
dbFile.setUrlqiniu(prop.getQiniuprefix() + fileName);
}
diskfileRepository.save(dbFile);
}
@PostMapping("/upload")
public String upload(MultipartHttpServletRequest request, RedirectAttributes redirectAttributes) {
Iterator<String> itr = request.getFileNames();
MultipartFile mpf = request.getFile(itr.next());
storageService.store(mpf);
redirectAttributes.addFlashAttribute("message", "Successfully uploaded: " + mpf.getOriginalFilename());
return "redirect:/";
}
@ExceptionHandler(StorageFileNotFoundException.class)
public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc) {
return ResponseEntity.notFound().build();
}
}

View File

@@ -0,0 +1,8 @@
package xy.FileSystem.File;
public interface FileListener {
UploadResult Store(UploadFileExt ufe);
void Download(String fileKeyorName);
}

View File

@@ -2,6 +2,7 @@ package xy.FileSystem.File;
import java.io.Serializable;
//http回调信息
public class HttpResult implements Serializable{
private static final long serialVersionUID = 5976014738363051675L;

View File

@@ -0,0 +1,17 @@
package xy.FileSystem.File;
import java.util.ArrayList;
import java.util.List;
public class StoreSource {
static List<FileListener> listensers = new ArrayList<FileListener>();
public static List<FileListener> getListensers() {
return listensers;
}
public static void RegisterListensers(FileListener fl){
listensers.add(fl);
}
}

View File

@@ -0,0 +1,141 @@
package xy.FileSystem.File;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import xy.FileSystem.Utils.FilePathUtil;
import xy.FileSystem.Utils.MimeTypeUtil;
//上传文件扩展模型
public class UploadFileExt {
private String fileName;
private String mimeType;
private String catalog;
private String url;
private byte[] bytes;
private File file;
private InputStream inputStream;
private Map<String, Object> metadata = new HashMap<String, Object>();
private long size;
public UploadFileExt(String filePath) {
if (filePath.startsWith(FilePathUtil.HTTP_PREFIX) || filePath.startsWith(FilePathUtil.HTTPS_PREFIX)) {
this.url = filePath;
this.fileName = FilePathUtil.parseFileName(this.url);
} else {
this.file = new File(filePath);
this.fileName = file.getName();
}
}
public UploadFileExt(File file) {
this.fileName = file.getName();
this.file = file;
}
public UploadFileExt(String fileName, File file) {
this.fileName = fileName;
this.file = file;
}
public UploadFileExt(String fileName, InputStream inputStream, String mimeType) {
this.fileName = fileName;
this.inputStream = inputStream;
this.mimeType = mimeType;
}
public UploadFileExt(String fileName, byte[] bytes, String mimeType,long size) {
this.fileName = fileName;
this.bytes = bytes;
this.mimeType = mimeType;
this.size = size;
}
// public UploadFileExt(String fileName, byte[] bytes) {
// this.fileName = fileName;
// this.bytes = bytes;
// this.mimeType = perseMimeType(bytes);
// }
public String getFileName() {
if (fileName.isEmpty()) {
fileName = UUID.randomUUID().toString().replaceAll("\\-", "");
}
if (mimeType != null && !fileName.contains(".")) {
String fileExtension = MimeTypeUtil.getFileExtension(mimeType);
if(fileExtension != null)fileName = fileName + fileExtension;
}
return fileName;
}
public String getUrl() {
return url;
}
public byte[] getBytes() {
return bytes;
}
public File getFile() {
return file;
}
public InputStream getInputStream() {
return inputStream;
}
public Map<String, Object> getMetadata() {
return metadata;
}
public void setString(String mimeType) {
this.mimeType = mimeType;
}
public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}
public UploadFileExt addMetaData(String key, Object value) {
metadata.put(key, value);
return this;
}
public String getMimeType(){
return mimeType;
}
public String getCatalog() {
return catalog;
}
public UploadFileExt toCatalog(String catalog) {
this.catalog = catalog;
return this;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
// private static String perseMimeType(byte[] bytes){
// try {
// MagicMatch match = Magic.getMagicMatch(bytes);
// String mimeType = match.getMimeType();
// return mimeType;
// } catch (Exception e) {
// return null;
// }
// }
}

View File

@@ -0,0 +1,15 @@
package xy.FileSystem.File;
//上传后回调信息
public class UploadResult {
public String fileName;
public long fsize;
public String key;//文件系统中的关键Key,用于查询定位,Fastdsf\ali\Seaweedfs都是用key定位文件
public String location;//一些扩展信息
public String bucket;
public String tag;//一些扩展信息
public String hash;
public int width;
public int height;
}

View File

@@ -1,5 +1,6 @@
package xy.FileSystem.File;
//上传文件简单扩展模型
public class UploadedFile {
public int length;

View File

@@ -1,5 +1,6 @@
package xy.FileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -8,14 +9,33 @@ import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import xy.FileSystem.Cache.UsesCache;
import xy.FileSystem.File.StoreSource;
import xy.FileSystem.Propert.StorageProperties;
import xy.FileSystem.Service.AliService;
import xy.FileSystem.Service.FastdfsServcice;
import xy.FileSystem.Service.MongoService;
import xy.FileSystem.Service.QiniuService;
import xy.FileSystem.Service.SeaweedfsService;
import xy.FileSystem.Service.StorageService;
@SpringBootApplication
@EnableScheduling
@EnableConfigurationProperties(StorageProperties.class)
public class FileSystemApplication {
@Autowired
private StorageProperties prop;
@Autowired
QiniuService qiniuService;
@Autowired
AliService aliService;
@Autowired
FastdfsServcice fastdfsServcice;
@Autowired
MongoService mongoService;
@Autowired
SeaweedfsService seaweedfsService;
public static void main(String[] args) {
SpringApplication.run(FileSystemApplication.class, args);
}
@@ -27,14 +47,38 @@ public class FileSystemApplication {
storageService.init();
initCache();
registerStoreSource();
};
}
public void registerStoreSource() {
if (prop.isToqiniu()){
StoreSource.RegisterListensers(qiniuService);
}
if (prop.isToalioss()){
StoreSource.RegisterListensers(aliService);
}
if (prop.isTofastdfs()){
StoreSource.RegisterListensers(fastdfsServcice);
}
if (prop.isTomongodb()){
StoreSource.RegisterListensers(mongoService);
}
if (prop.isToseaweedfs()){
StoreSource.RegisterListensers(seaweedfsService);
}
}
public void initCache() {
//TODO init cache
UsesCache.files = 100;
UsesCache.groups = 100;
UsesCache.usedspace = 100000;
//TODO cache test
UsesCache.files = 1000000;
UsesCache.groups = 1000;
UsesCache.usedspace = 1000000000;
}

View File

@@ -9,6 +9,8 @@ public class StorageProperties {
private boolean rename = true;
private String downloadfrom = "";
private boolean todisk = true;
private boolean toqiniu = true;
private boolean tofastdfs = false;
@@ -27,6 +29,81 @@ public class StorageProperties {
private String gridfsdbname = "";
private String gridfscollectionname = "";
private String seaweedfshost = "";
private int seaweedfsport = 9333;
private int seaweedfstimeout = 5000;
private String aliendpoint = "";
private String aliaccesskeyid = "";
private String aliaccesskeysecret = "";
private String alibucketname = "";
private String alidownloadkey = "";
public String getAliendpoint() {
return aliendpoint;
}
public void setAliendpoint(String aliendpoint) {
this.aliendpoint = aliendpoint;
}
public String getAliaccesskeyid() {
return aliaccesskeyid;
}
public void setAliaccesskeyid(String aliaccesskeyid) {
this.aliaccesskeyid = aliaccesskeyid;
}
public String getAliaccesskeysecret() {
return aliaccesskeysecret;
}
public void setAliaccesskeysecret(String aliaccesskeysecret) {
this.aliaccesskeysecret = aliaccesskeysecret;
}
public String getAlibucketname() {
return alibucketname;
}
public void setAlibucketname(String alibucketname) {
this.alibucketname = alibucketname;
}
public String getAlidownloadkey() {
return alidownloadkey;
}
public void setAlidownloadkey(String alidownloadkey) {
this.alidownloadkey = alidownloadkey;
}
public String getSeaweedfshost() {
return seaweedfshost;
}
public void setSeaweedfshost(String seaweedfshost) {
this.seaweedfshost = seaweedfshost;
}
public int getSeaweedfsport() {
return seaweedfsport;
}
public void setSeaweedfsport(int seaweedfsport) {
this.seaweedfsport = seaweedfsport;
}
public int getSeaweedfstimeout() {
return seaweedfstimeout;
}
public void setSeaweedfstimeout(int seaweedfstimeout) {
this.seaweedfstimeout = seaweedfstimeout;
}
public String getQiniuprefix() {
return qiniuprefix;
@@ -163,4 +240,12 @@ public class StorageProperties {
public void setToseaweedfs(boolean toseaweedfs) {
this.toseaweedfs = toseaweedfs;
}
public String getDownloadfrom() {
return downloadfrom;
}
public void setDownloadfrom(String downloadfrom) {
this.downloadfrom = downloadfrom;
}
}

View File

@@ -0,0 +1,36 @@
package xy.FileSystem.Schedule;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 6000000)
public void timeReport() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
//生成每日报表
@Scheduled(fixedRate = 86400000)
public void makeDailyReport() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
//删除旧文件
@Scheduled(fixedRate = 86400000)
public void deleleOldFiles() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}

View File

@@ -1,5 +1,138 @@
package xy.FileSystem.Service;
public class AliService {
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.DownloadFileResult;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import xy.FileSystem.File.FileListener;
import xy.FileSystem.File.UploadFileExt;
import xy.FileSystem.File.UploadResult;
import xy.FileSystem.Propert.StorageProperties;
@Service
public class AliService implements FileListener{
@Autowired
StorageProperties prop;
private OSS init(){
return new OSSClientBuilder().build(prop.getAliendpoint(),
prop.getAliaccesskeyid(),
prop.getAliaccesskeysecret());
}
@Override
public UploadResult Store(UploadFileExt ufe) {
//上传回调
CompleteMultipartUploadResult cmuResult = store(ufe.getUrl(),ufe.getFileName());
UploadResult result = new UploadResult();
if (ufe.getBytes() != null) {
//TODO byte[] upload
} else {
result.fileName = ufe.getFileName();
result.fsize = ufe.getFile().length();
result.hash = Integer.toString(ufe.getFile().hashCode());
result.key = cmuResult.getKey();
result.bucket = cmuResult.getBucketName();
result.location = cmuResult.getLocation();
result.tag = cmuResult.getETag();
}
return result;
}
@Override
public void Download(String fileKeyorName) {
//下载回调
}
public CompleteMultipartUploadResult store(String filePath, String fileName) {
OSS ossClient = init();
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(prop.getAlibucketname(), prop.getAlidownloadkey());
// The local file to upload---it must exist.
uploadFileRequest.setUploadFile(filePath);
// Sets the concurrent upload task number to 5.
uploadFileRequest.setTaskNum(5);
// Sets the part size to 1MB.
uploadFileRequest.setPartSize(1024 * 1024 * 1);
// Enables the checkpoint file. By default it's off.
uploadFileRequest.setEnableCheckpoint(true);
UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
CompleteMultipartUploadResult multipartUploadResult =
uploadResult.getMultipartUploadResult();
System.out.println(multipartUploadResult.getETag());
return multipartUploadResult;
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
return null;
}
public void download(String localFile, String fileKey){
OSS ossClient = init();
try {
// 下载请求10个任务并发下载启动断点续传。
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(prop.getAlibucketname(), fileKey);
downloadFileRequest.setDownloadFile(localFile);
downloadFileRequest.setPartSize(1 * 1024 * 1024);
downloadFileRequest.setTaskNum(5);//5线程
downloadFileRequest.setEnableCheckpoint(true);
DownloadFileResult downloadResult = ossClient.downloadFile(downloadFileRequest);
ObjectMetadata objectMetadata = downloadResult.getObjectMetadata();
System.out.println(objectMetadata.getETag());
System.out.println(objectMetadata.getLastModified());
System.out.println(objectMetadata.getUserMetadata().get("meta"));
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
}
}

View File

@@ -1,5 +0,0 @@
package xy.FileSystem.Service;
public class DailyreportService {
}

View File

@@ -1,5 +1,30 @@
package xy.FileSystem.Service;
public class FastdfsServcice {
import org.springframework.stereotype.Service;
import xy.FileSystem.File.FileListener;
import xy.FileSystem.File.UploadFileExt;
import xy.FileSystem.File.UploadResult;
@Service
public class FastdfsServcice implements FileListener {
public void store(String filePath, String finalFilename) {
// TODO Auto-generated method stub
}
@Override
public UploadResult Store(UploadFileExt ufe) {
///上传回调
return null;
}
@Override
public void Download(String fileKeyorName) {
// TODO Auto-generated method stub
}
}

View File

@@ -15,22 +15,23 @@ import org.springframework.stereotype.Service;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import xy.FileSystem.File.FileListener;
import xy.FileSystem.File.UploadFileExt;
import xy.FileSystem.File.UploadResult;
import xy.FileSystem.Propert.StorageProperties;
@Service
public class MongoService {
public class MongoService implements FileListener{
@Autowired
private StorageProperties prop;
private StorageProperties prop;
private MongoClient mongo;
/**
* @MethodName : getMongo
* @Description : 获取数据连接
@@ -39,7 +40,7 @@ public class MongoService {
private MongoClient getMongo(){
MongoClient mongo=null;
try {
mongo = new MongoClient(new ServerAddress(prop.getGridfshost(), prop.getGridfsport()));
mongo = new MongoClient(new ServerAddress(prop.getGridfshost(), prop.getGridfsport()));
} catch (Exception e) {
e.printStackTrace();
@@ -47,32 +48,49 @@ public class MongoService {
return mongo;
}
public void store(String filePath, String finalFilename) {
// TODO Auto-generated method stub
}
@Override
public UploadResult Store(UploadFileExt ufe) {
// TODO Auto-generated method stub
return null;
}
@Override
public void Download(String fileKeyorName) {
// TODO Auto-generated method stub
}
/**
* @MethodName : uploadFile
* @Description : 上传文件
* @param file 文件File类型
* @param id 唯一标示文件可根据id查询到文件.必须设置
* @param dbName :库名,每个系统使用一个库
* @param collectionName集合名如果传入的集合名库中没有则会自动新建并保存
* @param prop.getGridfsdbname() :库名,每个系统使用一个库
* @param prop.getGridfscollectionname():集合名,如果传入的集合名库中没有,则会自动新建并保存
* @param map放入你想要保存的属性例如文件类型“congtentType”".jpg",字符串类型,区分大小写,如果属性没有的话会自动创建并保存
*/
public void uploadFile(File file,String id,String dbName,String collectionName,LinkedHashMap<String, Object> map){
public void uploadFile(File file,String id,LinkedHashMap<String, Object> map){
//把mongoDB的数据库地址配置在外部。
try {
MongoClient mongo =getMongo();
//每个系统用一个库
DB db = mongo.getDB(dbName);
DB db = mongo.getDB(prop.getGridfsdbname());
System.out.println(db.toString());
//每个库中可以分子集
GridFS gridFS= new GridFS(db,collectionName);
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
// 创建gridfsfile文件
GridFSFile gridFSFile = gridFS.createFile(file);
//判断是否已经存在文件,如果存在则先删除
GridFSDBFile gridFSDBFile=getFileById(id, dbName, collectionName);
GridFSDBFile gridFSDBFile=getFileById(id);
if(gridFSDBFile!=null){
deleteFile(id, dbName, collectionName);
deleteFile(id);
}
//将文件属性设置到
gridFSFile.put("_id", id);
@@ -84,33 +102,39 @@ public class MongoService {
}
//保存上传
gridFSFile.save();
mongo.close();
} catch (Exception e) {
e.printStackTrace();
}
finally{
mongo.close();
}
}
/**
* @MethodName : deleteFile
* @Description : 删除文件
* @param id文件对应的id
* @param dbName文件所在的库
* @param collectionName文件所在的集合
* @param prop.getGridfsdbname():文件所在的库
* @param prop.getGridfscollectionname():文件所在的集合
*/
public void deleteFile(String id,String dbName,String collectionName){
public void deleteFile(String id){
try {
//获得mongoDB数据库连接。
MongoClient mongo =getMongo();
//获得库
DB db= mongo.getDB(dbName);
DB db= mongo.getDB(prop.getGridfsdbname());
//获得子集
GridFS gridFS= new GridFS(db,collectionName);
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
//删除文件
DBObject query=new BasicDBObject("_id", id);
gridFS.remove(query);
} catch (Exception e) {
e.printStackTrace();
}
finally{
mongo.close();
}
}
/**
@@ -118,18 +142,17 @@ public class MongoService {
* @MethodName : deleteFileByIds
* @Description : TODO
* @param ids
* @param dbName
* @param collectionName
* @param prop.getGridfsdbname()
* @param prop.getGridfscollectionname()
*
*/
public void deleteFileByIds(String[] ids,String dbName,String collectionName){
public void deleteFileByIds(String[] ids){
try {
//获得mongoDB数据库连接。
MongoClient mongo =getMongo();
//获得库
DB db= mongo.getDB(dbName);
DB db= mongo.getDB(prop.getGridfsdbname());
//获得子集
GridFS gridFS= new GridFS(db,collectionName);
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
Map<String,String> map = new HashMap<String,String>();
for(int i=0;i<ids.length;i++){
//删除文件
@@ -139,57 +162,63 @@ public class MongoService {
} catch (Exception e) {
e.printStackTrace();
}
finally{
mongo.close();
}
}
/**
* @MethodName : getFileById
* @Description : 根据Id获得文件
* @param id 文件Id
* @param dbName: 数据库名
* @param collectionName集合名
* @param prop.getGridfsdbname(): 数据库名
* @param prop.getGridfscollectionname():集合名
* @return GridFSDBFile
*/
public GridFSDBFile getFileById(String id,String dbName,String collectionName){
public GridFSDBFile getFileById(String id){
GridFSDBFile gridFSDBFile=null;
try {
//获得mongoDB数据库连接。
Mongo mongo =getMongo();
//获得库
DB db= mongo.getDB(dbName);
DB db= mongo.getDB(prop.getGridfsdbname());
//获得子集
GridFS gridFS= new GridFS(db,collectionName);
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
//获得文件
DBObject query=new BasicDBObject("_id", id);
gridFSDBFile=gridFS.findOne(query);
} catch (Exception e) {
e.printStackTrace();
}
finally{
mongo.close();
}
//返回数据
return gridFSDBFile;
}
/**查询集合中所有文件
* @MethodName : getAllFile
* @Description : TODO
* @param dbName
* @param collectionName *
* @param prop.getGridfsdbname()
* @param prop.getGridfscollectionname() *
* @return
*/
public List<GridFSDBFile> getAllFile(String dbName,String collectionName){
public List<GridFSDBFile> getAllFile(){
List<GridFSDBFile> gridFSDBFileList=null;
try {
//获得mongoDB数据库连接。
Mongo mongo =getMongo();
//获得库
DB db= mongo.getDB(dbName);
DB db= mongo.getDB(prop.getGridfsdbname());
//获得子集
GridFS gridFS= new GridFS(db,collectionName);
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
//获得文件
DBObject query=new BasicDBObject();//空的构造
gridFSDBFileList = gridFS.find(query);
} catch (Exception e) {
e.printStackTrace();
}
finally{
mongo.close();
}
//返回数据
return gridFSDBFileList;
}
@@ -201,14 +230,15 @@ public class MongoService {
* @param fileName
* @return
*/
public GridFSDBFile getByFileName(String fileName,String dbName,String collectionName){
public GridFSDBFile getByFileName(String fileName){
DBObject query = new BasicDBObject("filename", fileName);
DB db= getMongo().getDB(dbName);
GridFS gridFS= new GridFS(db,collectionName);
DB db= getMongo().getDB(prop.getGridfsdbname());
GridFS gridFS= new GridFS(db,prop.getGridfscollectionname());
GridFSDBFile gridFSDBFile = gridFS.findOne(query);
return gridFSDBFile;
}
@SuppressWarnings("unused")
private void downloadFile(GridFSDBFile gridFSDBFile,HttpServletResponse res,String filename)
{
try
@@ -233,5 +263,7 @@ public class MongoService {
e.printStackTrace();
}
}
}

View File

@@ -1,12 +1,10 @@
package xy.FileSystem.Service;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
@@ -15,81 +13,133 @@ import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import xy.FileSystem.Cache.FileCache;
import xy.FileSystem.File.FileListener;
import xy.FileSystem.File.UploadFileExt;
import xy.FileSystem.File.UploadResult;
import xy.FileSystem.Propert.StorageProperties;
@Service
public class QiniuService {
@Autowired
private StorageProperties prop;
public void store(String filePath, String fileName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.huadong());//俺家是华东区的
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//生成上传凭证,然后准备上传
public class QiniuService implements FileListener {
private Zone zone = Zone.huadong();// 七牛华东存储区
@Autowired
StorageProperties prop;
private Auth init() {
if (FileCache.qiniuAuth != null)
return FileCache.qiniuAuth; // 注入缓存
String bucket = prop.getQiniubucket();
String accessKey = prop.getQiniuak();
String secretKey = prop.getQiniusk();
//如果是Windows情况下格式是 D:\\qiniu\\test.png
//如果是linux格式是/home/qiniu/test.png
String localFilePath = filePath;
//默认不指定key的情况下以文件内容的hash值作为文件名
String key = fileName;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(localFilePath, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
FileCache.qiniuAuth = auth;
return auth;
}
public void store(byte[] bytes, String fileName) {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
@Override
public UploadResult Store(UploadFileExt ufe) {
UploadResult result = new UploadResult();
if (ufe.getBytes() != null) {
System.out.println("qiniu upload:"+ufe.getFileName());
DefaultPutRet dpr = store(ufe.getBytes(), ufe.getFileName());
result.fileName = ufe.getFileName();
result.fsize = ufe.getSize();
result.hash = dpr.hash;
result.key = dpr.key;
result.bucket = prop.getQiniubucket();
result.location = prop.getQiniuprefix() + ufe.getFileName();
result.tag = "";
} else {
DefaultPutRet dpr = store(ufe.getUrl(), ufe.getFileName());
result.fileName = ufe.getFileName();
result.fsize = ufe.getFile().length();
result.hash = dpr.hash;
result.key = dpr.key;
result.bucket = prop.getQiniubucket();
result.location = prop.getQiniuprefix() + ufe.getFileName();
result.tag = "";
}
System.out.println("#########################");
System.out.println("qiniu upload success,download url:"+ result.location);
System.out.println("#########################");
return result;
}
@Override
public void Download(String fileKeyorName) {
// TODO 下载回调
}
public DefaultPutRet store(String filePath, String fileName) {
Auth auth = init();
// 构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(zone);
UploadManager uploadManager = new UploadManager(cfg);
//生成上传凭证,然后准备上传
String upToken = auth.uploadToken(prop.getQiniubucket());
try {
Response response = uploadManager.put(filePath, fileName, upToken);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return putRet;
// System.out.println(putRet.key);
// System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
try {
DefaultPutRet errorRet = new Gson().fromJson(r.bodyString(), DefaultPutRet.class);
return errorRet;
} catch (JsonSyntaxException | QiniuException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println(r.toString());
}
return null;
}
public DefaultPutRet store(byte[] bytes, String fileName) {
Auth auth = init();
Configuration cfg = new Configuration(zone);
UploadManager uploadManager = new UploadManager(cfg);
// 生成上传凭证,然后准备上传
String bucket = prop.getQiniubucket();
String accessKey = prop.getQiniuak();
String secretKey = prop.getQiniusk();
//默认不指定key的情况下以文件内容的hash值作为文件名
// 默认不指定key的情况下以文件内容的hash值作为文件名
String key = fileName;
byte[] uploadBytes = bytes;//"hello qiniu cloud".getBytes("utf-8");
//ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
Auth auth = Auth.create(accessKey, secretKey);
byte[] uploadBytes = bytes;// "aaa".getBytes("utf-8");
// ByteArrayInputStream byteInputStream=new
// ByteArrayInputStream(uploadBytes);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(uploadBytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
Response response = uploadManager.put(uploadBytes, key, upToken);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
// System.out.println(putRet.key);
// System.out.println(putRet.hash);
return putRet;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
Response r = ex.response;
try {
DefaultPutRet errorRet = new Gson().fromJson(r.bodyString(), DefaultPutRet.class);
return errorRet;
} catch (JsonSyntaxException | QiniuException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println(r.toString());
}
return null;
}
}

View File

@@ -1,5 +0,0 @@
package xy.FileSystem.Service;
public class SeafileService {
}

View File

@@ -1,5 +1,159 @@
package xy.FileSystem.Service;
public class SeaweedfsService {
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.http.entity.ContentType;
import org.lokra.seaweedfs.core.FileSource;
import org.lokra.seaweedfs.core.FileTemplate;
import org.lokra.seaweedfs.core.file.FileHandleStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xy.FileSystem.File.FileListener;
import xy.FileSystem.File.UploadFileExt;
import xy.FileSystem.File.UploadResult;
import xy.FileSystem.Propert.StorageProperties;
@Service
public class SeaweedfsService implements FileListener {
@Autowired
private StorageProperties prop;
private FileTemplate init(){
FileSource fileSource = new FileSource();
fileSource.setHost(prop.getSeaweedfshost());
fileSource.setPort(prop.getSeaweedfsport());
fileSource.setConnectionTimeout(prop.getSeaweedfstimeout());
try {
fileSource.startup();
FileTemplate template = new FileTemplate(fileSource.getConnection());
return template;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public UploadResult Store(UploadFileExt ufe) {
UploadResult result = new UploadResult();
if (ufe.getBytes() != null) {
FileHandleStatus dpr = store(ufe.getBytes(), ufe.getFileName());
result.fileName = ufe.getFileName();
result.fsize = ufe.getSize();
result.hash = Integer.toString(dpr.hashCode());;
result.key = dpr.getFileId();
result.bucket = prop.getQiniubucket();
result.location = "";
result.tag = "";
} else {
FileHandleStatus dpr = store(ufe.getUrl(), ufe.getFileName());
result.fileName = ufe.getFileName();
result.fsize = ufe.getFile().length();
result.hash = Integer.toString(dpr.hashCode());
result.key = dpr.getFileId();
result.bucket = prop.getQiniubucket();
result.location = "";
result.tag = "";
}
return result;
}
@Override
public void Download(String fileKeyorName) {
// TODO Auto-generated method stub
}
public FileHandleStatus store(String filePath, String fileName) {
FileTemplate template = init();
if (template == null)
return null;
byte[] bytes;
try {
bytes = FileUtils.readFileToByteArray(new File(filePath));
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
return template.saveFileByStream(fileName, byteInputStream);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
public FileHandleStatus store(byte[] bytes, String fileName){
FileTemplate template = init();
if (template == null)
return null;
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
try {
return template.saveFileByStream(fileName, byteInputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public FileHandleStatus store(byte[] bytes, String fileName,ContentType contentType){
FileTemplate template = init();
if (template == null)
return null;
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
try {
return template.saveFileByStream(fileName, byteInputStream, contentType);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String download(String fileId){
FileTemplate template = init();
if (template == null)
return null;
try {
return template.getFileUrl(fileId);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public String delete(String fileId){
FileTemplate template = init();
if (template == null)
return null;
try {
template.deleteFile(fileId);
return "ok";
} catch (IOException e) {
return "error";
}
}
}

View File

@@ -1,5 +0,0 @@
package xy.FileSystem.Service;
public class SfsService {
}

View File

@@ -0,0 +1,28 @@
package xy.FileSystem.Utils;
public class FilePathUtil {
public static final String DIR_SPLITER = "/";
public static final String HTTP_PREFIX = "http://";
public static final String HTTPS_PREFIX = "https://";
public static String parseFileExtension(String filePath){
if(filePath.contains("/")){
filePath = filePath.substring(filePath.lastIndexOf("/"));
}
filePath = filePath.split("\\?")[0];
if(filePath.contains(".")){
return filePath.substring(filePath.lastIndexOf(".") + 1);
}
return null;
}
public static String parseFileName(String filePath){
filePath = filePath.split("\\?")[0];
int index = filePath.lastIndexOf("/") + 1;
if(index > 0){
return filePath.substring(index);
}
return filePath;
}
}

View File

@@ -0,0 +1,39 @@
package xy.FileSystem.Utils;
import java.util.HashMap;
import java.util.Map;
public class MimeTypeUtil {
private static Map<String,String> maps = new HashMap<>();
static{
maps.put("image/jpeg",".jpg");
maps.put("image/gif",".gif" );
maps.put("image/png",".png" );
maps.put("image/bmp",".bmp" );
maps.put("text/plain",".txt");
maps.put("application/zip",".zip" );
maps.put("application/x-zip-compressed",".zip" );
maps.put("multipart/x-zip",".zip" );
maps.put("application/x-compressed",".zip" );
maps.put("audio/mpeg3",".mp3" );
maps.put("video/avi",".avi" );
maps.put("audio/wav",".wav" );
maps.put("application/x-gzip",".gzip" );
maps.put("application/x-gzip",".gz");
maps.put("text/html",".html");
maps.put("application/x-shockwave-flash",".svg");
maps.put("application/pdf",".pdf" );
maps.put("application/msword",".doc" );
maps.put("application/vnd.openxmlformats-officedocument.wordprocessingml.document",".docx" );
maps.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",".xlsx" );
maps.put("application/vnd.ms-excel",".xls" );
maps.put("application/vnd.ms-powerpoint",".ppt" );
maps.put("application/vnd.openxmlformats-officedocument.presentationml.presentation",".pptx" );
}
public static String getFileExtension(String mimeType){
return maps.get(mimeType);
}
}

View File

@@ -9,8 +9,9 @@ storage.location = uploadfiledir
# \u662f\u5426\u91cd\u65b0\u547d\u540d\u6587\u4ef6\u540d\uff0c\u5982\u679c\u662f\u4e2a\u4eba\u6587\u4ef6\u5219\u91cd\u547d\u540d\u4e3a\uff1ausername_ + \u539f\u6587\u4ef6\u540d\uff1b\u5982\u679c\u662f\u7fa4\u7ec4\u6587\u4ef6\u5219\u91cd\u547d\u540d\u4e3a\uff1agroupid_ + \u539f\u6587\u4ef6\u540d #
storage.rename = true
# \u5b58\u50a8\u6e90\uff0c\u7c7b\u578b\u6709\uff1axyfs\\Seafile\\FastDFS\\SeaweedFS\\MongoDB\\aliOSS\\qiniu\\CFS #
storage.todisk = true
# \u5b58\u50a8\u6e90.\u53ef\u591a\u9009.\u53ef\u4ee5\u540c\u65f6\u50a8\u5b58\u591a\u4e2a\u6570\u636e\u6e90,\u4fbf\u4e8e\u5907\u4efd #
# \u7c7b\u578b\u6709\uff1axyfs\\Seafile\\FastDFS\\SeaweedFS\\MongoDB\\aliOSS\\qiniu\\CFS #
storage.todisk = true
storage.toqiniu = true
storage.tofastdfs = false
storage.tomongodb = false
@@ -18,6 +19,11 @@ storage.toseaweedfs = false
storage.toalioss = false
storage.tocfs = false
# \u7528\u4e8e\u8bbe\u7f6e\u4e0b\u8f7d\u7684\u6e90 ,\u5355\u9009 #
# \u7c7b\u578b\u6709\uff1axyfs\\Seafile\\FastDFS\\SeaweedFS\\MongoDB\\aliOSS\\qiniu\\CFS #
storage.downloadfrom = xyfs
# xyfs\u5b58\u50a8\u914d\u7f6e #
storage.diskprefix = http://localhost:9091/files/
# \u4e03\u725b\u5b58\u50a8\u914d\u7f6e #
@@ -44,7 +50,14 @@ storage.fastdfstrackerserver = 192.168.17.112:22122
# SeaweedFS \u914d\u7f6e #
storage.seaweedfshost = localhost
storage.seaweedfsport = 9333
storage.seaweedfstimeout = 5000
storage.seaweedfstimeout = 10
# ali OSS \u914d\u7f6e #
storage.aliendpoint = ""
storage.aliaccesskeyid = ""
storage.aliaccesskeysecret = ""
storage.alibucketname = ""
storage.alidownloadkey = ""
# \u7b2c\u4e00\u6b21\u4f7f\u7528\u7684\u914d\u7f6e\uff1a\u81ea\u52a8\u521b\u5efa\u6570\u636e\u5e93\u8868\uff0c\u5982\u679c\u662f\u8fd0\u884cSQL\u811a\u672c\u5219\u65e0\u9700\u4f7f\u7528\u6b64\u914d\u7f6e #
# spring.jpa.hibernate.ddl-auto = create
@@ -62,6 +75,12 @@ spring.jpa.database-platform= xy.FileSystem.Dialect.SQLiteDialect
#spring.datasource.username=root
#spring.datasource.password=mysql
# oracle\u6570\u636e\u6e90 #
#spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:torcl
#spring.datasource.username=center
#spring.datasource.password=centerDB12345
# dbcp2\u8fde\u63a5\u6c60\u914d\u7f6e #
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.max-wait-millis=10000
@@ -86,7 +105,6 @@ spring.datasource.dbcp2.connection-properties=characterEncoding=utf8
#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#spring.datasource.filters=stat,wall,log4j
# spring jackson \u65f6\u95f4\u914d\u7f6e #
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.joda-date-time-format=yyyy-MM-dd HH:mm:ss
@@ -97,6 +115,5 @@ spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=1024MB
spring.servlet.multipart.max-request-size=1024MB
# oss \u65e5\u5fd7\u914d\u7f6e #
logging.config=classpath:logback.xml