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.
32 lines
1.0 KiB
32 lines
1.0 KiB
4 years ago
|
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");
|
||
|
|
||
|
}
|
||
|
}
|