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

34 lines
1.1 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.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 filePath;
private final Logger logger = LoggerFactory.getLogger(FileService.class);
public File newFile(String filename) throws FileNotFoundException {
File txtDir = new File(URLDecoder.decode(ResourceUtils.getURL("classpath:").getPath(), StandardCharsets.UTF_8) + filePath.replace("classpath:/", ""));
if (!txtDir.exists() && txtDir.mkdirs()) {
logger.info("成功初始化上传论文目录:" + txtDir.getAbsolutePath());
}
return new File(txtDir, filename );
}
public File newFile() throws FileNotFoundException {
return newFile(UUID.randomUUID() + ".txt");
}
}