fileSystem added

This commit is contained in:
ddaa2000 2020-04-11 16:57:01 +08:00
parent 0511bfb79c
commit 575a64b4f6
8 changed files with 387 additions and 0 deletions

View File

@ -0,0 +1,192 @@
package com.codesdream.ase.component.activity;
import com.codesdream.ase.exception.conflict.FileNameConflict;
import com.codesdream.ase.exception.notfound.AppendixFileNotFoundException;
import com.codesdream.ase.model.activity.Activity;
import com.codesdream.ase.model.activity.AppendixFile;
import com.codesdream.ase.repository.activity.ActivityRepository;
import com.codesdream.ase.service.AppendixFileService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.*;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Optional;
@Component
public class FileSystem {
@Resource
private AppendixFileService appendixFileService;
public static final int FAILED = -1;
//默认文件系统根路径
private static final String rootDir = "d:/temp";
/**
* 用于创建文件条目的辅助函数
* @param fileName 文件名
* @return 文件在数据库中的条目
*/
private AppendixFile createFileTable(String fileName)
{
AppendixFile appendixFile = new AppendixFile();
appendixFile.setFileName(fileName);
appendixFile.setLastEditTime(LocalDateTime.now());
String[] temp = fileName.split("\\.",2);
for(String s : temp)
System.out.println(s);
if(temp.length == 1)
appendixFile.setType("");
else
appendixFile.setType(temp[temp.length-1]);
return appendixFile;
}
/**
* 向磁盘中添加一个文件并在数据库建立条目
* @param data 文件的数据
* @param fileName 文件名包括拓展名
* @return 成功时返回文件id失败时返回FileSystem.FAILED
*/
public int addFile(byte data[], String fileName) throws Exception
{
AppendixFile appendixFile = createFileTable(fileName);
appendixFile = appendixFileService.save(appendixFile);
File file = new File(rootDir,""+appendixFile.getId());
FileOutputStream outputStream;
if(file.exists())
throw new FileNameConflict(
"file name conflict,there is a file in the directory, and is not created by this program",
file.getName());
File parent = file.getParentFile();
if(!parent.exists())
parent.mkdirs();
try{
file.createNewFile();
outputStream = new FileOutputStream(file);
outputStream.write(data);
outputStream.close();
return appendixFile.getId();
}
catch (Exception e){
appendixFileService.delete(appendixFile);
e.printStackTrace();
throw new Exception(e);
}
}
/**
* 根据id获取一个磁盘中的文件
* @param id 文件的id
* @return 成功返回文件的InputStream失败返回null
*/
public InputStream getFile(int id)throws AppendixFileNotFoundException
{
Optional<AppendixFile> optionalAppendixFile = appendixFileService.findById(id);
if(!optionalAppendixFile.isPresent())
throw new AppendixFileNotFoundException(
"the required id does not exist in the database",id,
AppendixFileNotFoundException.ID_NOT_FOUND);
AppendixFile appendixFile = appendixFileService.findById(id).get();
File file = new File(rootDir,""+appendixFile.getId());
if(file.exists())
{
try {
InputStream inputStream = new FileInputStream(file);
return inputStream;
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new AppendixFileNotFoundException(
"the required id exists in the database, but the stream can not be opened",id,
AppendixFileNotFoundException.STREAM_FAILURE);
}
}
else
throw new AppendixFileNotFoundException(
"the required id exists in the database, but the file is missing",id,
AppendixFileNotFoundException.FILE_NOT_fOUND);
}
/**
* 删除一个文件如果该文件不存在则不会发生操作
* @param id 要删除文件的id
*/
public void deleteFile(int id)
{
Optional<AppendixFile> optionalAppendixFile = appendixFileService.findById(id);
if(!optionalAppendixFile.isPresent())
return;
AppendixFile appendixFile = appendixFileService.findById(id).get();
File file = new File(rootDir,""+appendixFile.getId());
if(file.exists()) {
file.delete();
appendixFileService.delete(appendixFile);
}
}
/**
* 根据id获取一个文件的条目其中包含文件信息
* @param id 要寻找条目的id
*/
public AppendixFile getFileData(int id)
{
Optional<AppendixFile> optionalAppendixFile = appendixFileService.findById(id);
if(!optionalAppendixFile.isPresent())
throw new AppendixFileNotFoundException(
"the required id does not exist in the database",id,
AppendixFileNotFoundException.ID_NOT_FOUND);
AppendixFile appendixFile = appendixFileService.findById(id).get();
return appendixFile;
}
/**
* 维护数据库删去所有文件已经缺失的条目仅用于在文件系统出现故障时的维护
*/
public void databaseRefresh()
{
for (AppendixFile appendixFile:
appendixFileService.findAll()) {
File file = new File(rootDir,""+appendixFile.getId());
if(!file.exists())
appendixFileService.delete(appendixFile);
}
}
/**
* 维护磁盘删除指定根目录下所有不在数据库中的文件仅用于文件系统出现故障时的维护
*/
public void diskRefresh()
{
File dir = new File(rootDir);
if (dir.exists()) {
if (null == dir.listFiles()) {
return;
}
for(File file : dir.listFiles())
{
int id;
try{
id = Integer.parseInt(file.getName());
if(!appendixFileService.findById(id).isPresent())
file.delete();
}
catch (Exception ex){
file.delete();
}
}
}
}
}

View File

@ -0,0 +1,11 @@
package com.codesdream.ase.exception.conflict;
public class FileNameConflict extends RuntimeException {
public String conflictName;
public FileNameConflict(String msg,String conflictName)
{
super(msg);
this.conflictName = conflictName;
}
}

View File

@ -0,0 +1,15 @@
package com.codesdream.ase.exception.notfound;
import com.codesdream.ase.model.activity.AppendixFile;
public class AppendixFileNotFoundException extends NotFoundException {
public int id;
public int type;
public static final int ID_NOT_FOUND = 1, FILE_NOT_fOUND = 2, STREAM_FAILURE = 3;
public AppendixFileNotFoundException(String msg,int id,int type){
super(msg);
this.id = id;
this.type = type;
}
}

View File

@ -0,0 +1,27 @@
package com.codesdream.ase.model.activity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Date;
@Data
@Entity
@Table(name = "appendixFile")
public class AppendixFile {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "file_name")
private String fileName;
@Column(name = "type")
private String type;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@Column(name = "last_edit_time")//, nullable = false)
private LocalDateTime lastEditTime = LocalDateTime.of(2020,2,18,16,36);
}

View File

@ -0,0 +1,10 @@
package com.codesdream.ase.repository.activity;
import com.codesdream.ase.model.activity.AppendixFile;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AppendixFileRespository extends CrudRepository<AppendixFile, Integer> {
}

View File

@ -0,0 +1,36 @@
package com.codesdream.ase.service;
import com.codesdream.ase.model.activity.AppendixFile;
import com.codesdream.ase.repository.activity.AppendixFileRespository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Optional;
@Service
public class AppendixFileService implements IAppendixFileService {
@Resource
private AppendixFileRespository appendixFileRespository;
@Override
public AppendixFile save(AppendixFile appendixFile) {
return appendixFileRespository.save(appendixFile);
}
@Override
public void delete(AppendixFile appendixFile) {
appendixFileRespository.delete(appendixFile);
}
@Override
public Optional<AppendixFile> findById(int id) {
return appendixFileRespository.findById(id);
}
@Override
public Iterable<AppendixFile> findAll() {
return appendixFileRespository.findAll();
}
}

View File

@ -0,0 +1,21 @@
package com.codesdream.ase.service;
import com.codesdream.ase.model.activity.AppendixFile;
import java.util.Optional;
public interface IAppendixFileService {
//存储磁盘文件条目
AppendixFile save(AppendixFile appendixFile);
//删除磁盘文件条目
void delete(AppendixFile appendixFile);
//通过ID寻找文件条目
Optional<AppendixFile> findById(int id);
//找到所有文件条目
Iterable<AppendixFile> findAll();
}

View File

@ -0,0 +1,75 @@
package com.codesdream.ase.test;
import com.codesdream.ase.component.activity.FileSystem;
import com.codesdream.ase.model.activity.AppendixFile;
import com.codesdream.ase.repository.activity.AppendixFileRespository;
import com.codesdream.ase.service.AppendixFileService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
@SpringBootTest
@RunWith(SpringRunner.class)
public class FileSystemTest {
@Resource
FileSystem fileSystem;
@Resource
AppendixFileService appendixFileService;
@Test
public void getPresentFilesTest()
{
Iterable<AppendixFile> appendixFiles = appendixFileService.findAll();
for (AppendixFile appendixFile:
appendixFiles) {
System.out.println(appendixFile.getFileName()+" "+appendixFile.getId()+" "+appendixFile.getType()+
" "+appendixFile.getLastEditTime().toString());
}
}
@Test
public void DeleteAllFilesTest()
{
Iterable<AppendixFile> appendixFiles = appendixFileService.findAll();
for (AppendixFile appendixFile:
appendixFiles) {
fileSystem.deleteFile(appendixFile.getId());
}
}
@Test
public void createFile()
{
int id1 = 0;
try {
id1 = fileSystem.addFile("asfasefasgasgasg".getBytes(),"test1.txt");
} catch (Exception e) {
e.printStackTrace();
}
try {
int id2 = fileSystem.addFile("aspgjparjgpoarjgpjpeg".getBytes(),"test2.jpeg");
} catch (Exception e) {
e.printStackTrace();
}
InputStream inputStream = fileSystem.getFile(id1);
byte[] bytes = new byte[200];
try {
inputStream.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(bytes.toString());
}
}