You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudnote_server/src/main/java/com/bupt/note/service/FileService.java

45 lines
1.5 KiB

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 "";
}
}