diff --git a/doc/db_table.md b/doc/db_table.md
index fe56fce..e4b0ee0 100644
--- a/doc/db_table.md
+++ b/doc/db_table.md
@@ -12,17 +12,14 @@
| ---- | -------- | -------- | ---- |
| id | bigint | primary key; identity | 文件id |
| file_path | varchar | not null | 文件路径 |
-| url_path | varchar | not null | 访问路径 |
-# 论文笔记表 sys_paper_note
+# 笔记文件表 sys_paper_note
| 列名 | 数据类型 | 约束条件 | 含义 |
| ---- | -------- | -------- | ---- |
| id | bigint | primary key; identity | 文件id |
| paper_id | bigint | not null | 论文id |
-| file_id | bigint | not null | 原文件id |
| file_path | varchar | not null | 文件路径 |
-| url_path | varchar | not null | 访问路径 |
| user_name | varchar | not null | 修改用户 |
# 论文 sys_paper
@@ -38,9 +35,8 @@
| year | int | not null | 学位年度 |
| summary | varchar | not null | 摘要 |
| tag | varchar | not null | 论文标签 |
-| file_id | bigint | not null | 正文文件id |
-| user_name | varchar | not null | 上传用户 |
-| create_time | Long | not null | 上传时间戳 |
+| file_id | bigint | not null | 论文文件id |
+| create_time | Long | not null | 创建时间戳 |
# 论文评价 sys_paper_rating
| 列名 | 数据类型 | 约束条件 | 含义 |
diff --git a/pom.xml b/pom.xml
index da4a87c..0de5c72 100644
--- a/pom.xml
+++ b/pom.xml
@@ -67,6 +67,13 @@
2.6
+
+
+ org.apache.pdfbox
+ pdfbox
+ 2.0.20
+
+
diff --git a/src/main/java/com/bupt/note/Controller/FileController.java b/src/main/java/com/bupt/note/Controller/FileController.java
index 503e98f..9b7d934 100644
--- a/src/main/java/com/bupt/note/Controller/FileController.java
+++ b/src/main/java/com/bupt/note/Controller/FileController.java
@@ -8,7 +8,6 @@ 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;
@@ -18,7 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
+import java.util.UUID;
/**
* 论文文本管理接口
@@ -42,16 +41,21 @@ public class FileController {
//上传论文文本
@PostMapping("upload")
public ResponseData upload(FileForm fileForm) {
- MultipartFile file = fileForm.getFile();
- if (file != null && MediaType.TEXT_PLAIN_VALUE.equals(file.getContentType())) {
+ MultipartFile multipartFile = fileForm.getFile();
+ if (multipartFile != null && (MediaType.TEXT_PLAIN_VALUE.equals(multipartFile.getContentType()) || MediaType.APPLICATION_PDF_VALUE.equals(multipartFile.getContentType()))) {
try {
- File txtFile = fileService.newFile();
- logger.debug("论文保存到" + txtFile.getAbsolutePath());
- file.transferTo(txtFile);
+ String name = multipartFile.getOriginalFilename();
+ File file;
+ if (name != null && name.contains(".")) {
+ file = fileService.newFile(name.replace(multipartFile.getOriginalFilename().split("\\.")[0], UUID.randomUUID().toString()));
+ } else {
+ file = fileService.newFile();
+ }
+ logger.debug("论文保存到" + file.getAbsolutePath());
+ multipartFile.transferTo(file);
com.bupt.note.Model.File f = new com.bupt.note.Model.File();
- f.setFilePath(txtFile.getAbsolutePath());
- f.setUrlPath("/txt/" + txtFile.getName());
+ f.setFilePath(file.getAbsolutePath());
fileRepository.save(f);
return ResponseDataUtil.buildSuccess(f.getId());
} catch (IOException e) {
@@ -66,15 +70,21 @@ public class FileController {
}
@GetMapping("find")
- 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(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(FileUtils.readFileToString(new File(file.getFilePath()), StandardCharsets.UTF_8));
- } else{
- logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId()));
+ public ResponseData get(QueryContent queryContent, @CookieValue("user") String username){
+ try {
+ if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(), username)) {
+ PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(), username);
+ return ResponseDataUtil.buildSuccess(fileService.getContent(paperNote.getFilePath()));
+ } else if (fileRepository.existsById(queryContent.getFileId())) {
+ com.bupt.note.Model.File file = fileRepository.getOne(queryContent.getFileId());
+ return ResponseDataUtil.buildSuccess(fileService.getContent(file.getFilePath()));
+ } else {
+ logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId()));
+ return ResponseDataUtil.buildError();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ logger.error(String.format("解析文件失败,原因是%s", e));
return ResponseDataUtil.buildError();
}
}
@@ -95,11 +105,11 @@ public class FileController {
fileRepository.deleteById(id);
return ResponseDataUtil.buildSuccess();
} else {
- logger.error(String.format("删除论文id=%d失败",id));
+ logger.error(String.format("删除论文id=%d失败", id));
return ResponseDataUtil.buildError();
}
} else {
- logger.error(String.format("论文id=%d不存在",id));
+ logger.error(String.format("论文id=%d不存在", id));
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 809d83d..f2c5be1 100644
--- a/src/main/java/com/bupt/note/Controller/NoteController.java
+++ b/src/main/java/com/bupt/note/Controller/NoteController.java
@@ -44,7 +44,7 @@ public class NoteController {
@Autowired
private FileService fileService;
- private Logger logger = LoggerFactory.getLogger(NoteController.class);
+ private final Logger logger = LoggerFactory.getLogger(NoteController.class);
/**
* 添加笔记
@@ -72,11 +72,8 @@ public class NoteController {
} else {
f = fileService.newFile();
paperNote = new PaperNote();
- paperNote.setFileId(noteForm.getFileId());
paperNote.setPaperId(noteForm.getPaperId());
paperNote.setFilePath(f.getAbsolutePath());
- paperNote.setUrlPath("/txt/" + f.getName());
- paperNote.setUserName(username);
paperNoteRepository.save(paperNote);
}
wr = new FileWriter(f, StandardCharsets.UTF_8);
diff --git a/src/main/java/com/bupt/note/Controller/PaperController.java b/src/main/java/com/bupt/note/Controller/PaperController.java
index 897b5e1..21adefd 100644
--- a/src/main/java/com/bupt/note/Controller/PaperController.java
+++ b/src/main/java/com/bupt/note/Controller/PaperController.java
@@ -7,7 +7,6 @@ import com.bupt.note.Repository.PaperRepository;
import com.bupt.note.Repository.RatingRepository;
import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil;
-import com.bupt.note.dto.Page;
import com.bupt.note.dto.QueryPaper;
import com.bupt.note.dto.RatingForm;
import com.bupt.note.dto.UploadPaper;
@@ -16,6 +15,8 @@ import org.hibernate.query.criteria.internal.OrderImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import javax.persistence.criteria.Order;
@@ -46,7 +47,6 @@ public class PaperController {
if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(),uploadPaper.getType(),
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){
Paper paper = uploadPaper.toPaper();
- paper.setUserName(username);
paperRepository.save(paper);
if(paperRepository.existsById(paper.getId())){
return ResponseDataUtil.buildSuccess();
@@ -64,9 +64,9 @@ public class PaperController {
public ResponseData list(QueryPaper queryPaper,@CookieValue("user") String username){
try {
- Page list=new Page<>();
- list.setTotal(paperRepository.count());
- List papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
+// Page list=new Page<>();
+// list.setTotal(paperRepository.count());
+ Page papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
List predicates = new ArrayList<>();
if(StringUtils.isNotEmpty(queryPaper.getTitle())){
predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%"));
@@ -92,12 +92,11 @@ public class PaperController {
}
Order order=new OrderImpl(root.get("createTime"),false);
return criteriaQuery.where(predicates.toArray(new Predicate[0])).orderBy(order).getRestriction();
- });
+ }, PageRequest.of(queryPaper.getPage()-1,10));
papers.forEach(paper -> {
paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(),username));
});
- list.setData(papers);
- return ResponseDataUtil.buildSuccess(list);
+ return ResponseDataUtil.buildSuccess(papers);
} catch (Exception e) {
e.printStackTrace();
logger.error(String.valueOf(e));
diff --git a/src/main/java/com/bupt/note/Model/File.java b/src/main/java/com/bupt/note/Model/File.java
index cc04078..7ac5625 100644
--- a/src/main/java/com/bupt/note/Model/File.java
+++ b/src/main/java/com/bupt/note/Model/File.java
@@ -11,7 +11,6 @@ public class File {
private Long Id;
private String filePath;
- private String urlPath;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -31,13 +30,4 @@ public class File {
public void setFilePath(String filePath) {
this.filePath = filePath;
}
-
- @Column(nullable = false)
- public String getUrlPath() {
- return urlPath;
- }
-
- public void setUrlPath(String urlPath) {
- this.urlPath = urlPath;
- }
}
diff --git a/src/main/java/com/bupt/note/Model/Paper.java b/src/main/java/com/bupt/note/Model/Paper.java
index 3639cb6..e85c03c 100644
--- a/src/main/java/com/bupt/note/Model/Paper.java
+++ b/src/main/java/com/bupt/note/Model/Paper.java
@@ -19,7 +19,6 @@ public class Paper {
private String summary;
private String tag;
private Long fileId;
- private String userName;
private Long createTime;
private boolean hasCollect;
private Long collectTime;
@@ -115,15 +114,6 @@ public class Paper {
this.fileId = fileId;
}
- @Column(nullable = false)
- public String getUserName() {
- return userName;
- }
-
- public void setUserName(String userName) {
- this.userName = userName;
- }
-
@Column(nullable = false)
public Long getCreateTime() {
return createTime;
diff --git a/src/main/java/com/bupt/note/Model/PaperNote.java b/src/main/java/com/bupt/note/Model/PaperNote.java
index 899d3c9..bfabb64 100644
--- a/src/main/java/com/bupt/note/Model/PaperNote.java
+++ b/src/main/java/com/bupt/note/Model/PaperNote.java
@@ -6,10 +6,8 @@ import javax.persistence.*;
@Table(name = "sys_paper_note")
public class PaperNote {
private Long id;
- private Long fileId;
private Long paperId;
private String filePath;
- private String urlPath;
private String userName;
@Id
@@ -22,15 +20,6 @@ public class PaperNote {
this.id = id;
}
- @Column(nullable = false)
- public Long getFileId() {
- return fileId;
- }
-
- public void setFileId(Long fileId) {
- this.fileId = fileId;
- }
-
@Column(nullable = false)
public String getFilePath() {
return filePath;
@@ -40,15 +29,6 @@ public class PaperNote {
this.filePath = filePath;
}
- @Column(nullable = false)
- public String getUrlPath() {
- return urlPath;
- }
-
- public void setUrlPath(String urlPath) {
- this.urlPath = urlPath;
- }
-
@Column(nullable = false)
public String getUserName() {
return userName;
diff --git a/src/main/java/com/bupt/note/dto/QueryPaper.java b/src/main/java/com/bupt/note/dto/QueryPaper.java
index 866a62f..529e621 100644
--- a/src/main/java/com/bupt/note/dto/QueryPaper.java
+++ b/src/main/java/com/bupt/note/dto/QueryPaper.java
@@ -13,6 +13,8 @@ public class QueryPaper {
private Integer endYear;
private Boolean own=false;
+ private Integer page;
+
public String getTitle() {
return title;
}
@@ -76,4 +78,12 @@ public class QueryPaper {
public void setOwn(Boolean own) {
this.own = own;
}
+
+ public Integer getPage() {
+ return page;
+ }
+
+ public void setPage(Integer page) {
+ this.page = page;
+ }
}
diff --git a/src/main/java/com/bupt/note/service/FileService.java b/src/main/java/com/bupt/note/service/FileService.java
index 0b7f478..416795b 100644
--- a/src/main/java/com/bupt/note/service/FileService.java
+++ b/src/main/java/com/bupt/note/service/FileService.java
@@ -1,12 +1,17 @@
package com.bupt.note.service;
+import org.apache.commons.io.FileUtils;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.text.PDFTextStripper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
-import java.io.*;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
@@ -15,31 +20,32 @@ import java.util.UUID;
public class FileService {
@Value("${spring.resources.static-locations}")
- private String txtPath;
+ private String filePath;
- private Logger logger = LoggerFactory.getLogger(FileService.class);
+ private final Logger logger = LoggerFactory.getLogger(FileService.class);
- public File newFile() throws FileNotFoundException {
- File txtDir = new File(URLDecoder.decode(ResourceUtils.getURL("classpath:").getPath(), StandardCharsets.UTF_8) + txtPath.replace("classpath:/", ""));
+ public File newFile(String filename) throws FileNotFoundException {
+ File txtDir = new File(URLDecoder.decode(ResourceUtils.getURL("classpath:").getPath(), StandardCharsets.UTF_8) + filePath.replace("classpath:/", ""));
if (!txtDir.exists() && txtDir.mkdirs()) {
logger.info("成功初始化上传论文目录:" + txtDir.getAbsolutePath());
}
- return new File(txtDir, UUID.randomUUID() + ".txt");
+ return new File(txtDir, filename );
+ }
+ public File newFile() throws FileNotFoundException {
+ return newFile(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);
+ public String getContent(String filePath) throws IOException {
+ if(filePath.endsWith(".txt")||filePath.endsWith(".text")){
+ return FileUtils.readFileToString(new File(filePath), StandardCharsets.UTF_8);
+ }else if(filePath.endsWith(".pdf")){
+ PDDocument document = PDDocument.load(new File(filePath));
+ PDFTextStripper s = new PDFTextStripper();
+ s.setParagraphStart(" ");
+ return s.getText(document);
+ }else{
+ throw new RuntimeException("解析文件内容失败");
}
- return sb.toString();
- } catch (IOException e) {
- e.printStackTrace();
- logger.error(String.valueOf(e));
- }
- return "";
}
}
diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml
index 18fab79..2cdbafd 100644
--- a/src/main/resources/application.yaml
+++ b/src/main/resources/application.yaml
@@ -1,3 +1,4 @@
+#ba
spring:
datasource:
url: jdbc:mysql://localhost:3306/note?serverTimezone=UTC&characterEncoding=utf-8
@@ -9,8 +10,8 @@ spring:
show-sql: true
#静态资源目录
mvc:
- static-path-pattern: /txt/**
+ static-path-pattern: /file/**
resources:
- static-locations: classpath:/resources/static/txt
+ static-locations: classpath:/resources/static/file
file:
upload-dir: note_file/
\ No newline at end of file
diff --git a/src/test/java/com/bupt/note/MyTest.java b/src/test/java/com/bupt/note/MyTest.java
index 099d3a6..ec19356 100644
--- a/src/test/java/com/bupt/note/MyTest.java
+++ b/src/test/java/com/bupt/note/MyTest.java
@@ -1,10 +1,39 @@
package com.bupt.note;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.jupiter.api.Test;
+import java.io.File;
+import java.io.IOException;
+
public class MyTest {
@Test
- public void test1(){
- System.out.println(String.format("%d=%d",1,1L));
+ public void test1() {
+ System.out.println(String.format("%d=%d", 1, 1L));
+ }
+
+
+ @Test
+ public void testPDF() throws IOException {
+ PDDocument document = PDDocument.load(new File("E:\\JetBrains\\PycharmProjects\\python_requirements_summary\\libcon\\Management+Game环境下戴乐国际表业有限公司总体战略规划.pdf"));
+ PDFTextStripper s = new PDFTextStripper();
+// for (PDPage page : document.getPages()) {
+//
+//
+//
+// ByteArrayOutputStream result = new ByteArrayOutputStream();
+// byte[] buffer = new byte[1024];
+// int length;
+// while ((length = page.getContents().read(buffer)) != -1) {
+// result.write(buffer, 0, length);
+// }
+//// String f = new String(result.toByteArray());
+//// System.out.println(f);
+// }
+// s.setArticleStart(" ");
+// s.setParagraphEnd(" .\n");
+ System.out.println(s.getText(document));
+
}
}