增加论文上传接口

pdf
pan 4 years ago
parent c9a8884386
commit a2458e3871
  1. 55
      doc/db_table.md
  2. 15
      pom.xml
  3. 93
      src/main/java/com/bupt/note/Controller/FileController.java
  4. 40
      src/main/java/com/bupt/note/Controller/PaperController.java
  5. 32
      src/main/java/com/bupt/note/Controller/SignUpController.java
  6. 38
      src/main/java/com/bupt/note/Controller/UserController.java
  7. 37
      src/main/java/com/bupt/note/Model/File.java
  8. 114
      src/main/java/com/bupt/note/Model/Paper.java
  9. 3
      src/main/java/com/bupt/note/Model/User.java
  10. 5
      src/main/java/com/bupt/note/Repository/FileRepository.java
  11. 7
      src/main/java/com/bupt/note/Repository/PapaerRepository.java
  12. 8
      src/main/java/com/bupt/note/Repository/UserRepository.java
  13. 18
      src/main/java/com/bupt/note/dto/FileForm.java
  14. 91
      src/main/java/com/bupt/note/dto/UploadPaper.java
  15. 11
      src/main/resources/application.yaml

@ -1,55 +1,32 @@
# 用户表 User
# 用户表 sys_user
| 列名 | 数据类型 | 约束条件 | 含义 |
| ---- | -------- | -------- | ---- |
| id | bigint | primary key; identity | 用户编号 |
| id | bigint | primary key; identity | 用户id |
| user_name | varchar | not null | 用户名 |
| password | varchar | not null | 密码 |
| | | | |
# 论文 Paper
| 列名 | 数据类型 | 约束条件 | 含义 |
| ---- | -------- | -------- | ---- |
| Pid | int | primary key; identity | 文章编号 |
| Ptitle | varchar | not null | 文章标题 |
| Ptime | date | | 发表时间 |
# 作者 Author
| 列名 | 数据类型 | 约束条件 | 含义 |
| ---- | -------- | -------- | ---- |
| Aid | int | primary key; identity | 作者编号 |
| Aname | varchar | not null | 作者姓名 |
# 论文作者关系 PaperAuthor
| 列名 | 数据类型 | 约束条件 | 含义 |
| ---- | -------- | -------- | ---- |
| PAid | int | primary key; identity | 编号 |
| Aid | int | foreign key | 作者编号 |
| Pid | int | foreign key | 文章编号 |
# 标签 Tag
# 论文文件表 sys_file
| 列名 | 数据类型 | 约束条件 | 含义 |
| ---- | -------- | -------- | ---- |
| Tid | int | primary key; identity | 标签编号 |
| Tname | varchar | not null | 标签描述 |
| id | bigint | primary key; identity | 文件id |
| file_path | varchar | not null | 文件路径 |
# 论文标签 PaperTag
# 论文 sys_paper
| 列名 | 数据类型 | 约束条件 | 含义 |
| ---- | -------- | -------- | ---- |
| PTid | int | primary key; identity | 编号 |
| Pid | int | foreign key | 文章编号 |
| Tid | int | foreign key | 标签编号 |
| id | int | primary key; identity | 论文id |
| type | varchar | not null | 论文类型 |
| author | varchar | not null | 论文作者 |
| profession | varchar | not null | 学科专业 |
| school | varchar | not null | 学校 |
| year | int | not null | 学位年度 |
| summary | varchar | not null | 摘要 |
| tag | varchar | not null | 论文标签 |
| file_id | bigint | not null | 正文文件id |
| user_name | varchar | not null | 上传用户 |
# 笔记 Note

@ -56,13 +56,12 @@
<version>1.2.12</version>
</dependency>
<!-- file -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
@ -74,6 +73,14 @@
<fork>false</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>

@ -0,0 +1,93 @@
package com.bupt.note.Controller;
import com.bupt.note.Repository.FileRepository;
import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.dto.FileForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.UUID;
/**
* 论文文本管理接口
*/
@RestController
@RequestMapping(value = "/v1/api/file")
public class FileController {
@Value("${spring.resources.static-locations}")
private String txtPath;
private static Logger logger = LoggerFactory.getLogger(FileController.class);
@Autowired
private FileRepository fileRepository;
//上传论文文本
@PostMapping("upload")
public ResponseData upload(FileForm fileForm){
MultipartFile file=fileForm.getFile();
if(file!=null&& MediaType.TEXT_PLAIN_VALUE.equals(file.getContentType())){
File txtFile;
File txtDir;
try {
txtDir=new File(URLDecoder.decode(ResourceUtils.getURL("classpath:").getPath(), StandardCharsets.UTF_8) + txtPath.replace("classpath:/", ""));
if(!txtDir.exists()&&txtDir.mkdirs()){
logger.info("成功初始化上传论文目录:"+txtDir.getAbsolutePath());
}
txtFile=new File(txtDir, UUID.randomUUID()+".txt");
logger.debug("论文保存到"+txtFile.getAbsolutePath());
file.transferTo(txtFile);
com.bupt.note.Model.File f=new com.bupt.note.Model.File();
f.setFilePath(txtFile.getAbsolutePath());
fileRepository.save(f);
return ResponseDataUtil.buildSuccess(f.getId());
} catch (IOException e) {
logger.error(String.valueOf(e));
e.printStackTrace();
return ResponseDataUtil.buildError();
}
}else{
return ResponseDataUtil.buildError();
}
}
/**
* 删除论文文本文件
* @param id 文件id
* @return
*/
@DeleteMapping("remove/{id}")
public ResponseData remove(@PathVariable Long id){
if(fileRepository.existsById(id)){
Optional<com.bupt.note.Model.File> f=fileRepository.findById(id);
if(f.isPresent()){
File txtFile=new File(f.get().getFilePath());
if(txtFile.exists()&&txtFile.delete()) {
fileRepository.deleteById(id);
return ResponseDataUtil.buildSuccess();
}else{
return ResponseDataUtil.buildError();
}
}else {
return ResponseDataUtil.buildError();
}
}else{
return ResponseDataUtil.buildError();
}
}
}

@ -0,0 +1,40 @@
package com.bupt.note.Controller;
import com.bupt.note.Model.Paper;
import com.bupt.note.Repository.PapaerRepository;
import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.dto.UploadPaper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 论文管理接口
*/
@RestController
@RequestMapping(value = "/v1/api/paper")
public class PaperController {
@Autowired
private PapaerRepository papaerRepository;
@PostMapping("upload")
public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) {
if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getType(),
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){
Paper paper = uploadPaper.toPaper();
paper.setUserName(username);
papaerRepository.save(paper);
if(papaerRepository.existsById(paper.getId())){
return ResponseDataUtil.buildSuccess();
}else{
return ResponseDataUtil.buildError();
}
}else{
return ResponseDataUtil.buildError();
}
}
}

@ -1,32 +0,0 @@
package com.bupt.note.Controller;
import com.bupt.note.Model.User;
import com.bupt.note.Repository.UserRepository;
import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.ResponseData.ResultEnums;
import com.bupt.note.dto.UserForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/v1/api")
public class SignUpController {
@Autowired
UserRepository userRepository;
@RequestMapping("/sign_up")
public ResponseData signUp(@RequestBody UserForm user) {
// 用来创建User(即注册)
User exist = userRepository.findByUserName(user.getUserName());
if (exist == null) {
userRepository.save(user.getUser());
User u = userRepository.findByUserName(user.getUserName());
return ResponseDataUtil.buildSuccess(u);
}
return ResponseDataUtil.buildError(ResultEnums.ErrUserNameConflict);
}
}

@ -5,11 +5,16 @@ import com.bupt.note.Repository.UserRepository;
import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.ResponseData.ResultEnums;
import com.bupt.note.dto.UserForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.List;
import java.util.Optional;
/**
* 用户接口
*/
@RestController
@RequestMapping(value = "/v1/api/users")
public class UserController {
@ -17,6 +22,37 @@ public class UserController {
@Autowired
UserRepository userRepository;
/**
* 注册用户
* @param user 用户信息
* @return
*/
@PostMapping("/sign_up")
public ResponseData signUp(@RequestBody UserForm user) {
User exist = userRepository.findByUserName(user.getUserName());
if (exist == null) {
userRepository.save(user.getUser());
User u = userRepository.findByUserName(user.getUserName());
return ResponseDataUtil.buildSuccess(u);
}
return ResponseDataUtil.buildError(ResultEnums.ErrUserNameConflict);
}
/**
* 登录用户
* @param user 用户信息
* @return
*/
@PostMapping("/sign_in")
public ResponseData signIn(@RequestBody UserForm user){
User exist = userRepository.findByUserName(user.getUserName());
if(exist==null||!exist.getPassword().equals(user.getPassword())){
return ResponseDataUtil.buildError(ResultEnums.ERROR);
}else {
return ResponseDataUtil.buildSuccess(ResultEnums.SUCCESS);
}
}
@RequestMapping(method=RequestMethod.GET)
public ResponseData<List<User>> getUserList() {
// 处理"/users/"的GET请求,用来获取用户列表

@ -2,26 +2,17 @@ package com.bupt.note.Model;
import javax.persistence.*;
/**
* 论文文本实体
*/
@Entity
@Table(name = "sys_file")
public class File {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
@Column(nullable = false)
private Long ownerId;
@Column(nullable = false)
private String fileName;
public File() {
super();
}
public File(Long ownerId, String fileName) {
this.ownerId = ownerId;
this.fileName = fileName;
}
private Long Id;
private String filePath;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return Id;
@ -31,19 +22,13 @@ public class File {
Id = id;
}
public Long getOwnerId() {
return ownerId;
}
public void setOwnerId(Long ownerId) {
this.ownerId = ownerId;
@Column(nullable = false)
public String getFilePath() {
return filePath;
}
public String getFileName() {
return fileName;
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}

@ -0,0 +1,114 @@
package com.bupt.note.Model;
import javax.persistence.*;
/**
* 论文实体
*/
@Entity
@Table(name = "sys_paper")
public class Paper {
private Long id;
private String type;
private String author;
private String profession;
private String school;
private Integer year;
private String summary;
private String tag;
private Long fileId;
private String userName;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(nullable = false)
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column(nullable = false)
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Column(nullable = false)
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
@Column(nullable = false)
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
@Column(nullable = false)
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
@Column(nullable = false)
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
@Column(nullable = false)
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
@Column(nullable = false)
public Long getFileId() {
return fileId;
}
public void setFileId(Long fileId) {
this.fileId = fileId;
}
@Column(nullable = false)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}

@ -3,6 +3,9 @@ package com.bupt.note.Model;
import javax.persistence.*;
import java.io.Serializable;
/*
用户实体
*/
@Entity
@Table(name = "sys_user")
public class User implements Serializable {

@ -3,11 +3,6 @@ package com.bupt.note.Repository;
import com.bupt.note.Model.File;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface FileRepository extends JpaRepository<File, Long> {
@Override
Optional<File> findById(Long id);
void deleteById(Long id);
}

@ -0,0 +1,7 @@
package com.bupt.note.Repository;
import com.bupt.note.Model.Paper;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PapaerRepository extends JpaRepository<Paper, Long> {
}

@ -3,15 +3,7 @@ package com.bupt.note.Repository;
import com.bupt.note.Model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUserName(String userName);
void deleteByUserName(String userName);
@Override
Optional<User> findById(Long integer);
List<User> findAll();
}

@ -0,0 +1,18 @@
package com.bupt.note.dto;
import org.springframework.web.multipart.MultipartFile;
public class FileForm {
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}

@ -0,0 +1,91 @@
package com.bupt.note.dto;
import com.bupt.note.Model.Paper;
public class UploadPaper {
private String type;
private String author;
private String profession;
private String school;
private Integer year;
private String summary;
private String tag;
private Long fileId;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public Long getFileId() {
return fileId;
}
public void setFileId(Long fileId) {
this.fileId = fileId;
}
public Paper toPaper(){
Paper paper=new Paper();
paper.setType(this.type);
paper.setAuthor(this.author);
paper.setProfession(this.profession);
paper.setSchool(this.school);
paper.setYear(this.year);
paper.setSummary(this.summary);
paper.setTag(this.tag);
paper.setFileId(this.fileId);
return paper;
}
}

@ -7,11 +7,10 @@ spring:
hibernate:
ddl-auto: update
show-sql: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 200MB
enabled: true
file-size-threshold: 2KB
#静态资源目录
mvc:
static-path-pattern: /txt/**
resources:
static-locations: classpath:/resources/static/txt
file:
upload-dir: note_file/
Loading…
Cancel
Save