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.File; import java.io.FileNotFoundException; 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"); } }