From c7bf1b2d449cd1055cb2b848995fa8cc100bd7bd Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Fri, 31 Jul 2020 20:54:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=98=85=E8=AF=BB=E8=AE=BA?= =?UTF-8?q?=E6=96=87=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 +++++++-- .../bupt/note/Controller/FileController.java | 8 +++++--- .../bupt/note/Controller/NoteController.java | 1 + src/main/java/com/bupt/note/Model/Note.java | 10 ++++++++++ src/main/java/com/bupt/note/dto/NoteForm.java | 10 ++++++++++ .../com/bupt/note/service/FileService.java | 18 ++++++++++++++++-- .../com/bupt/note/NoteApplicationTests.java | 11 ++++++++++- 7 files changed, 59 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 74acc4f..da4a87c 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,11 @@ commons-lang3 + + commons-io + commons-io + 2.6 + @@ -77,8 +82,8 @@ org.apache.maven.plugins maven-compiler-plugin - 10 - 10 + 11 + 11 diff --git a/src/main/java/com/bupt/note/Controller/FileController.java b/src/main/java/com/bupt/note/Controller/FileController.java index 0b4b15a..503e98f 100644 --- a/src/main/java/com/bupt/note/Controller/FileController.java +++ b/src/main/java/com/bupt/note/Controller/FileController.java @@ -8,6 +8,7 @@ import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.dto.FileForm; import com.bupt.note.dto.QueryContent; import com.bupt.note.service.FileService; +import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -17,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; /** * 论文文本管理接口 @@ -64,13 +66,13 @@ public class FileController { } @GetMapping("find") - public ResponseData get(QueryContent queryContent,@CookieValue("user") String username) { + public ResponseData get(QueryContent queryContent,@CookieValue("user") String username) throws IOException { if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(),username)) { PaperNote paperNote=paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(),username); - return ResponseDataUtil.buildSuccess(paperNote.getUrlPath()); + return ResponseDataUtil.buildSuccess(FileUtils.readFileToString(new File(paperNote.getFilePath()), StandardCharsets.UTF_8)); }else if(fileRepository.existsById(queryContent.getFileId())){ com.bupt.note.Model.File file=fileRepository.getOne(queryContent.getFileId()); - return ResponseDataUtil.buildSuccess(file.getUrlPath()); + return ResponseDataUtil.buildSuccess(FileUtils.readFileToString(new File(file.getFilePath()), StandardCharsets.UTF_8)); } else{ logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId())); return ResponseDataUtil.buildError(); diff --git a/src/main/java/com/bupt/note/Controller/NoteController.java b/src/main/java/com/bupt/note/Controller/NoteController.java index d68610d..9d07072 100644 --- a/src/main/java/com/bupt/note/Controller/NoteController.java +++ b/src/main/java/com/bupt/note/Controller/NoteController.java @@ -49,6 +49,7 @@ public class NoteController { @PostMapping("add") public ResponseData addNote(@RequestBody NoteForm noteForm, @CookieValue("user") String username) { if (noteForm.getPaperId() != null && StringUtils.isNoneBlank(noteForm.getNoteId(), + noteForm.getOriginalText(), noteForm.getNoteTitle(), noteForm.getNoteContent(), noteForm.getContent())) { Note note = noteForm.toNote(); note.setUserName(username); diff --git a/src/main/java/com/bupt/note/Model/Note.java b/src/main/java/com/bupt/note/Model/Note.java index 16d8454..7a570ae 100644 --- a/src/main/java/com/bupt/note/Model/Note.java +++ b/src/main/java/com/bupt/note/Model/Note.java @@ -14,6 +14,7 @@ public class Note implements Serializable { private String noteContent; private Long paperId; private String userName; + private String originalText; @Id public String getNoteId() { @@ -59,5 +60,14 @@ public class Note implements Serializable { public void setUserName(String userName) { this.userName = userName; } + + @Column(nullable = false) + public String getOriginalText() { + return originalText; + } + + public void setOriginalText(String originalText) { + this.originalText = originalText; + } } diff --git a/src/main/java/com/bupt/note/dto/NoteForm.java b/src/main/java/com/bupt/note/dto/NoteForm.java index 50c3c26..c11a40c 100644 --- a/src/main/java/com/bupt/note/dto/NoteForm.java +++ b/src/main/java/com/bupt/note/dto/NoteForm.java @@ -9,6 +9,7 @@ public class NoteForm { private String noteId; private String noteTitle; private String noteContent; + private String originalText; private Long paperId; private Long fileId; private String content; @@ -61,8 +62,17 @@ public class NoteForm { this.fileId = fileId; } + public String getOriginalText() { + return originalText; + } + + public void setOriginalText(String originalText) { + this.originalText = originalText; + } + public Note toNote(){ Note note=new Note(); + note.setOriginalText(this.originalText); note.setPaperId(this.paperId); note.setNoteContent(this.noteContent); note.setNoteTitle(this.noteTitle); diff --git a/src/main/java/com/bupt/note/service/FileService.java b/src/main/java/com/bupt/note/service/FileService.java index 8790da0..0b7f478 100644 --- a/src/main/java/com/bupt/note/service/FileService.java +++ b/src/main/java/com/bupt/note/service/FileService.java @@ -6,8 +6,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.ResourceUtils; -import java.io.File; -import java.io.FileNotFoundException; +import java.io.*; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.UUID; @@ -28,4 +27,19 @@ public class FileService { return new File(txtDir, UUID.randomUUID() + ".txt"); } + + public String getContent(String path) { + StringBuilder sb=new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new FileReader(path, StandardCharsets.UTF_8))) { + String tempString; + while ((tempString = reader.readLine()) != null) { + sb.append(tempString); + } + return sb.toString(); + } catch (IOException e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + } + return ""; + } } diff --git a/src/test/java/com/bupt/note/NoteApplicationTests.java b/src/test/java/com/bupt/note/NoteApplicationTests.java index f9941c0..6512a40 100644 --- a/src/test/java/com/bupt/note/NoteApplicationTests.java +++ b/src/test/java/com/bupt/note/NoteApplicationTests.java @@ -1,5 +1,6 @@ package com.bupt.note; +import com.bupt.note.service.FileService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -12,6 +13,8 @@ import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import java.io.IOException; + @SpringBootTest class NoteApplicationTests { @@ -20,6 +23,9 @@ class NoteApplicationTests { protected MockMvc mvc; + @Autowired + private FileService fileService; + @BeforeEach public void setUp() { mvc = MockMvcBuilders.webAppContextSetup(context).build(); @@ -35,5 +41,8 @@ class NoteApplicationTests { } - + @Test + public void testRead() throws IOException { + String a= fileService.getContent("E:\\JetBrains\\IdeaProjects\\back-end code\\target\\classes\\resources\\static\\txt\\1aee7062-58a7-46e0-ba4d-84a9a14f4205.txt"); + } }