更新阅读论文接口

pdf
pan 4 years ago
parent 79c9c809f9
commit c7bf1b2d44
  1. 9
      pom.xml
  2. 8
      src/main/java/com/bupt/note/Controller/FileController.java
  3. 1
      src/main/java/com/bupt/note/Controller/NoteController.java
  4. 10
      src/main/java/com/bupt/note/Model/Note.java
  5. 10
      src/main/java/com/bupt/note/dto/NoteForm.java
  6. 18
      src/main/java/com/bupt/note/service/FileService.java
  7. 11
      src/test/java/com/bupt/note/NoteApplicationTests.java

@ -61,6 +61,11 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies> </dependencies>
@ -77,8 +82,8 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>10</source> <source>11</source>
<target>10</target> <target>11</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

@ -8,6 +8,7 @@ import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.dto.FileForm; import com.bupt.note.dto.FileForm;
import com.bupt.note.dto.QueryContent; import com.bupt.note.dto.QueryContent;
import com.bupt.note.service.FileService; import com.bupt.note.service.FileService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -17,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
/** /**
* 论文文本管理接口 * 论文文本管理接口
@ -64,13 +66,13 @@ public class FileController {
} }
@GetMapping("find") @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)) { if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(),username)) {
PaperNote paperNote=paperNoteRepository.findByPaperIdAndUserName(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())){ }else if(fileRepository.existsById(queryContent.getFileId())){
com.bupt.note.Model.File file=fileRepository.getOne(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{ } else{
logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId())); logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId()));
return ResponseDataUtil.buildError(); return ResponseDataUtil.buildError();

@ -49,6 +49,7 @@ public class NoteController {
@PostMapping("add") @PostMapping("add")
public ResponseData addNote(@RequestBody NoteForm noteForm, @CookieValue("user") String username) { public ResponseData addNote(@RequestBody NoteForm noteForm, @CookieValue("user") String username) {
if (noteForm.getPaperId() != null && StringUtils.isNoneBlank(noteForm.getNoteId(), if (noteForm.getPaperId() != null && StringUtils.isNoneBlank(noteForm.getNoteId(),
noteForm.getOriginalText(),
noteForm.getNoteTitle(), noteForm.getNoteContent(), noteForm.getContent())) { noteForm.getNoteTitle(), noteForm.getNoteContent(), noteForm.getContent())) {
Note note = noteForm.toNote(); Note note = noteForm.toNote();
note.setUserName(username); note.setUserName(username);

@ -14,6 +14,7 @@ public class Note implements Serializable {
private String noteContent; private String noteContent;
private Long paperId; private Long paperId;
private String userName; private String userName;
private String originalText;
@Id @Id
public String getNoteId() { public String getNoteId() {
@ -59,5 +60,14 @@ public class Note implements Serializable {
public void setUserName(String userName) { public void setUserName(String userName) {
this.userName = userName; this.userName = userName;
} }
@Column(nullable = false)
public String getOriginalText() {
return originalText;
}
public void setOriginalText(String originalText) {
this.originalText = originalText;
}
} }

@ -9,6 +9,7 @@ public class NoteForm {
private String noteId; private String noteId;
private String noteTitle; private String noteTitle;
private String noteContent; private String noteContent;
private String originalText;
private Long paperId; private Long paperId;
private Long fileId; private Long fileId;
private String content; private String content;
@ -61,8 +62,17 @@ public class NoteForm {
this.fileId = fileId; this.fileId = fileId;
} }
public String getOriginalText() {
return originalText;
}
public void setOriginalText(String originalText) {
this.originalText = originalText;
}
public Note toNote(){ public Note toNote(){
Note note=new Note(); Note note=new Note();
note.setOriginalText(this.originalText);
note.setPaperId(this.paperId); note.setPaperId(this.paperId);
note.setNoteContent(this.noteContent); note.setNoteContent(this.noteContent);
note.setNoteTitle(this.noteTitle); note.setNoteTitle(this.noteTitle);

@ -6,8 +6,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
@ -28,4 +27,19 @@ public class FileService {
return new File(txtDir, UUID.randomUUID() + ".txt"); 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 "";
}
} }

@ -1,5 +1,6 @@
package com.bupt.note; package com.bupt.note;
import com.bupt.note.service.FileService;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; 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.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import java.io.IOException;
@SpringBootTest @SpringBootTest
class NoteApplicationTests { class NoteApplicationTests {
@ -20,6 +23,9 @@ class NoteApplicationTests {
protected MockMvc mvc; protected MockMvc mvc;
@Autowired
private FileService fileService;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(context).build(); 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");
}
} }

Loading…
Cancel
Save