From 575a64b4f6a107f135f0b79fbbe2ddab35a360ab Mon Sep 17 00:00:00 2001 From: ddaa2000 <2470872965@qq.com> Date: Sat, 11 Apr 2020 16:57:01 +0800 Subject: [PATCH] fileSystem added --- .../ase/component/activity/FileSystem.java | 192 ++++++++++++++++++ .../exception/conflict/FileNameConflict.java | 11 + .../AppendixFileNotFoundException.java | 15 ++ .../ase/model/activity/AppendixFile.java | 27 +++ .../activity/AppendixFileRespository.java | 10 + .../ase/service/AppendixFileService.java | 36 ++++ .../ase/service/IAppendixFileService.java | 21 ++ .../codesdream/ase/test/FileSystemTest.java | 75 +++++++ 8 files changed, 387 insertions(+) create mode 100644 src/main/java/com/codesdream/ase/component/activity/FileSystem.java create mode 100644 src/main/java/com/codesdream/ase/exception/conflict/FileNameConflict.java create mode 100644 src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java create mode 100644 src/main/java/com/codesdream/ase/model/activity/AppendixFile.java create mode 100644 src/main/java/com/codesdream/ase/repository/activity/AppendixFileRespository.java create mode 100644 src/main/java/com/codesdream/ase/service/AppendixFileService.java create mode 100644 src/main/java/com/codesdream/ase/service/IAppendixFileService.java create mode 100644 src/test/java/com/codesdream/ase/test/FileSystemTest.java diff --git a/src/main/java/com/codesdream/ase/component/activity/FileSystem.java b/src/main/java/com/codesdream/ase/component/activity/FileSystem.java new file mode 100644 index 0000000..659437d --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/activity/FileSystem.java @@ -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 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 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 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(); + } + } + } + } + +} diff --git a/src/main/java/com/codesdream/ase/exception/conflict/FileNameConflict.java b/src/main/java/com/codesdream/ase/exception/conflict/FileNameConflict.java new file mode 100644 index 0000000..a9b7669 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/conflict/FileNameConflict.java @@ -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; + } + +} diff --git a/src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java b/src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java new file mode 100644 index 0000000..03f4784 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java @@ -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; + } +} diff --git a/src/main/java/com/codesdream/ase/model/activity/AppendixFile.java b/src/main/java/com/codesdream/ase/model/activity/AppendixFile.java new file mode 100644 index 0000000..f9e075b --- /dev/null +++ b/src/main/java/com/codesdream/ase/model/activity/AppendixFile.java @@ -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); +} diff --git a/src/main/java/com/codesdream/ase/repository/activity/AppendixFileRespository.java b/src/main/java/com/codesdream/ase/repository/activity/AppendixFileRespository.java new file mode 100644 index 0000000..d7b2b58 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/activity/AppendixFileRespository.java @@ -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 { + +} diff --git a/src/main/java/com/codesdream/ase/service/AppendixFileService.java b/src/main/java/com/codesdream/ase/service/AppendixFileService.java new file mode 100644 index 0000000..a146cd9 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/AppendixFileService.java @@ -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 findById(int id) { + return appendixFileRespository.findById(id); + } + + @Override + public Iterable findAll() { + return appendixFileRespository.findAll(); + } +} diff --git a/src/main/java/com/codesdream/ase/service/IAppendixFileService.java b/src/main/java/com/codesdream/ase/service/IAppendixFileService.java new file mode 100644 index 0000000..65e4c75 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/IAppendixFileService.java @@ -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 findById(int id); + + //找到所有文件条目 + Iterable findAll(); + +} diff --git a/src/test/java/com/codesdream/ase/test/FileSystemTest.java b/src/test/java/com/codesdream/ase/test/FileSystemTest.java new file mode 100644 index 0000000..108089b --- /dev/null +++ b/src/test/java/com/codesdream/ase/test/FileSystemTest.java @@ -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 appendixFiles = appendixFileService.findAll(); + for (AppendixFile appendixFile: + appendixFiles) { + System.out.println(appendixFile.getFileName()+" "+appendixFile.getId()+" "+appendixFile.getType()+ + " "+appendixFile.getLastEditTime().toString()); + + } + } + + @Test + public void DeleteAllFilesTest() + { + Iterable 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()); + } + +}