package com.bupt.note.service; 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.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.UUID; @Service public class FileService { @Value("${spring.resources.static-locations}") private String txtPath; private 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:/", "")); if (!txtDir.exists() && txtDir.mkdirs()) { logger.info("成功初始化上传论文目录:" + txtDir.getAbsolutePath()); } 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 ""; } }