parent
bb9508c389
commit
dce9531a8a
@ -1,116 +0,0 @@ |
||||
package com.bupt.note.Controller; |
||||
|
||||
import com.bupt.note.Model.PaperNote; |
||||
import com.bupt.note.Repository.FileRepository; |
||||
import com.bupt.note.Repository.PaperNoteRepository; |
||||
import com.bupt.note.ResponseData.ResponseData; |
||||
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.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.web.bind.annotation.*; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.util.UUID; |
||||
|
||||
/** |
||||
* 论文文本管理接口 |
||||
*/ |
||||
@RestController |
||||
@RequestMapping(value = "/v1/api/file") |
||||
public class FileController { |
||||
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(FileController.class); |
||||
|
||||
@Autowired |
||||
private FileRepository fileRepository; |
||||
|
||||
@Autowired |
||||
private PaperNoteRepository paperNoteRepository; |
||||
|
||||
@Autowired |
||||
private FileService fileService; |
||||
|
||||
//上传论文文本
|
||||
@PostMapping("upload") |
||||
public ResponseData upload(FileForm fileForm) { |
||||
MultipartFile multipartFile = fileForm.getFile(); |
||||
if (multipartFile != null && (MediaType.TEXT_PLAIN_VALUE.equals(multipartFile.getContentType()) || MediaType.APPLICATION_PDF_VALUE.equals(multipartFile.getContentType()))) { |
||||
|
||||
try { |
||||
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(file.getAbsolutePath()); |
||||
fileRepository.save(f); |
||||
return ResponseDataUtil.buildSuccess(f.getId()); |
||||
} catch (IOException e) { |
||||
logger.error(String.valueOf(e)); |
||||
e.printStackTrace(); |
||||
return ResponseDataUtil.buildError(); |
||||
} |
||||
} else { |
||||
logger.error("上传论文表单校验失败"); |
||||
return ResponseDataUtil.buildError(); |
||||
} |
||||
} |
||||
|
||||
@GetMapping("find") |
||||
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(); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 删除论文文本文件 |
||||
* |
||||
* @param id 文件id |
||||
* @return |
||||
*/ |
||||
@DeleteMapping("remove/{id}") |
||||
public ResponseData remove(@PathVariable Long id) { |
||||
if (fileRepository.existsById(id)) { |
||||
com.bupt.note.Model.File txtFile = fileRepository.getOne(id); |
||||
File f = new File(txtFile.getFilePath()); |
||||
if (f.exists() && f.delete()) { |
||||
fileRepository.deleteById(id); |
||||
return ResponseDataUtil.buildSuccess(); |
||||
} else { |
||||
logger.error(String.format("删除论文id=%d失败", id)); |
||||
return ResponseDataUtil.buildError(); |
||||
} |
||||
} else { |
||||
logger.error(String.format("论文id=%d不存在", id)); |
||||
return ResponseDataUtil.buildError(); |
||||
} |
||||
} |
||||
} |
@ -1,33 +0,0 @@ |
||||
package com.bupt.note.Model; |
||||
|
||||
import javax.persistence.*; |
||||
|
||||
/** |
||||
* 论文文本实体 |
||||
*/ |
||||
@Entity |
||||
@Table(name = "sys_file") |
||||
public class File { |
||||
|
||||
private Long Id; |
||||
private String filePath; |
||||
|
||||
@Id |
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
public Long getId() { |
||||
return Id; |
||||
} |
||||
|
||||
public void setId(Long id) { |
||||
Id = id; |
||||
} |
||||
|
||||
@Column(nullable = false) |
||||
public String getFilePath() { |
||||
return filePath; |
||||
} |
||||
|
||||
public void setFilePath(String filePath) { |
||||
this.filePath = filePath; |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
package com.bupt.note.Model; |
||||
|
||||
import javax.persistence.*; |
||||
|
||||
@Entity |
||||
@Table(name = "sys_similarity") |
||||
public class Similarity { |
||||
private SimilarityId id; |
||||
private Double similarity; |
||||
|
||||
@EmbeddedId |
||||
public SimilarityId getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(SimilarityId id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
@Column(nullable = false) |
||||
public Double getSimilarity() { |
||||
return similarity; |
||||
} |
||||
|
||||
public void setSimilarity(Double similarity) { |
||||
this.similarity = similarity; |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
package com.bupt.note.Model; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Objects; |
||||
|
||||
public class SimilarityId implements Serializable { |
||||
private Long sourcePaper; |
||||
private Long targetPaper; |
||||
|
||||
public Long getSourcePaper() { |
||||
return sourcePaper; |
||||
} |
||||
|
||||
public void setSourcePaper(Long sourcePaper) { |
||||
this.sourcePaper = sourcePaper; |
||||
} |
||||
|
||||
public Long getTargetPaper() { |
||||
return targetPaper; |
||||
} |
||||
|
||||
public void setTargetPaper(Long targetPaper) { |
||||
this.targetPaper = targetPaper; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object o) { |
||||
if (this == o) return true; |
||||
if (o == null || getClass() != o.getClass()) return false; |
||||
SimilarityId that = (SimilarityId) o; |
||||
return Objects.equals(sourcePaper, that.sourcePaper) && |
||||
Objects.equals(targetPaper, that.targetPaper); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return Objects.hash(sourcePaper, targetPaper); |
||||
} |
||||
} |
@ -0,0 +1,32 @@ |
||||
package com.bupt.note.Model; |
||||
|
||||
import javax.persistence.*; |
||||
|
||||
/** |
||||
* 词库 |
||||
*/ |
||||
@Entity |
||||
@Table(name = "sys_word") |
||||
public class Word { |
||||
private String word; |
||||
private String flag; |
||||
|
||||
@Id |
||||
@Column(columnDefinition = "varchar(255) binary") |
||||
public String getWord() { |
||||
return word; |
||||
} |
||||
|
||||
public void setWord(String word) { |
||||
this.word = word; |
||||
} |
||||
|
||||
@Column(nullable = false) |
||||
public String getFlag() { |
||||
return flag; |
||||
} |
||||
|
||||
public void setFlag(String flag) { |
||||
this.flag = flag; |
||||
} |
||||
} |
@ -1,8 +0,0 @@ |
||||
package com.bupt.note.Repository; |
||||
|
||||
import com.bupt.note.Model.File; |
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
||||
public interface FileRepository extends JpaRepository<File, Long> { |
||||
|
||||
} |
@ -0,0 +1,10 @@ |
||||
package com.bupt.note.Repository; |
||||
|
||||
import com.bupt.note.Model.Similarity; |
||||
import com.bupt.note.Model.SimilarityId; |
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
||||
public interface SimilarityRepository extends JpaRepository<Similarity, SimilarityId>, JpaSpecificationExecutor<Similarity> { |
||||
|
||||
} |
@ -1,39 +1,11 @@ |
||||
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)); |
||||
} |
||||
|
||||
|
||||
@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)); |
||||
|
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue