下面列出了怎么用org.springframework.util.FileSystemUtils的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public File write(Package pkg, File targetDirectory) {
PackageMetadata packageMetadata = pkg.getMetadata();
File tmpDir = TempFileUtils.createTempDirectory("skipper" + packageMetadata.getName()).toFile();
File rootPackageDir = new File(tmpDir,
String.format("%s-%s", packageMetadata.getName(), packageMetadata.getVersion()));
rootPackageDir.mkdir();
writePackage(pkg, rootPackageDir);
if (!pkg.getDependencies().isEmpty()) {
File packagesDir = new File(rootPackageDir, "packages");
packagesDir.mkdir();
for (Package dependencyPkg : pkg.getDependencies()) {
File packageDir = new File(packagesDir, dependencyPkg.getMetadata().getName());
packageDir.mkdir();
writePackage(dependencyPkg, packageDir);
}
}
File targetZipFile = PackageFileUtils.calculatePackageZipFile(pkg.getMetadata(), targetDirectory);
ZipUtil.pack(rootPackageDir, targetZipFile, true);
FileSystemUtils.deleteRecursively(tmpDir);
return targetZipFile;
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some test images
*
* @return Spring Boot {@link CommandLineRunner} automatically
* run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some test images
*
* @return Spring Boot {@link CommandLineRunner} automatically
* run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some test images
*
* @return Spring Boot {@link CommandLineRunner} automatically
* run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some test images
*
* @return Spring Boot {@link CommandLineRunner} automatically
* run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
@Before
public void setUp() throws IOException {
operations.dropCollection(Image.class);
operations.insert(new Image("1",
"learning-spring-boot-cover.jpg"));
operations.insert(new Image("2",
"learning-spring-boot-2nd-edition-cover.jpg"));
operations.insert(new Image("3",
"bazinga.png"));
FileSystemUtils.deleteRecursively(new File(ImageService.UPLOAD_ROOT));
Files.createDirectory(Paths.get(ImageService.UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(ImageService.UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(ImageService.UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(ImageService.UPLOAD_ROOT + "/bazinga.png"));
}
/**
* Pre-load some test images
*
* @return Spring Boot {@link CommandLineRunner} automatically
* run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some test images
*
* @return Spring Boot {@link CommandLineRunner} automatically
* run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
/**
* Pre-load some fake images
*
* @return Spring Boot {@link CommandLineRunner} automatically run after app context is loaded.
*/
@Bean
CommandLineRunner setUp() throws IOException {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(UPLOAD_ROOT));
Files.createDirectory(Paths.get(UPLOAD_ROOT));
FileCopyUtils.copy("Test file",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-cover.jpg"));
FileCopyUtils.copy("Test file2",
new FileWriter(UPLOAD_ROOT +
"/learning-spring-boot-2nd-edition-cover.jpg"));
FileCopyUtils.copy("Test file3",
new FileWriter(UPLOAD_ROOT + "/bazinga.png"));
};
}
private byte[] createDownloadFile(Function<File, File> action) {
Path tmpDir = null;
try {
tmpDir = createTempDir();
if (logger.isDebugEnabled()) {
logger.debug("Generating project in temporary directory " + tmpDir.toFile());
}
File download = action.apply(tmpDir.toFile());
return fileToByteArray(download);
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
if (tmpDir != null) {
if (!FileSystemUtils.deleteRecursively(tmpDir.toFile())) {
logger.warn("Unable to delete temporary directory " + tmpDir.toFile());
}
}
}
}
/**
* Delete a resource version directory after taking appropriate lock.
*
* @param resourceVersionDir Directory to be deleted
* @throws IOException
*/
private void cleanUpResourceVersion(final File resourceVersionDir)
throws LockException, IOException {
/*
* Acquire a lock on the lock file for the resource version being deleted.
* Delete the entire directory for the resource version
*/
try (
CloseableLock lock = fileLockFactory.getLock(
touchCacheResourceVersionLockFile(resourceVersionDir)
)
) {
//critical section begin
lock.lock();
//Remove the data file. If last download was successful for the resource, only
//data file would exist
FileSystemUtils.deleteRecursively(getCacheResourceVersionDataFile(resourceVersionDir));
//data.tmp file could exist if the last download of the resource failed in the middle
//and after that a newer version was downloaded. So, delete it too
FileSystemUtils.deleteRecursively(getCacheResourceVersionDownloadFile(resourceVersionDir));
//critical section end
}
}
@Before
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.alwaysDo(print())
.apply(SecurityMockMvcConfigurers.springSecurity())
.addFilters(new OpenCasStorageSessionForRequestFilter())
.build();
if (!initialized) {
userRepository.create(new User("admin", Role.ROLE_ADMIN));
initialized = true;
FileSystemUtils.deleteRecursively(new File("target/RemoteApiController2Test"));
}
}
void copy(List<File> baseDirs, File output) {
File images = new File(output, "images");
baseDirs.forEach(input -> {
try {
File inputImages = new File(input, "images");
if (!inputImages.exists()) {
return;
}
Logger.info("Will copy [" + inputImages + "] files to [" + images + "]");
FileSystemUtils.copyRecursively(inputImages, images);
}
catch (IOException e) {
throw new IllegalStateException(e);
}
});
}
@Test
void should_generate_adocs_from_templates() {
File file = new File("target/test-train-docs");
FileSystemUtils.deleteRecursively(file);
List<Project> projects = Arrays.asList(new Project("spring-cloud-foo", "1.0.0"),
new Project("spring-cloud-bar", "2.0.0"),
new Project("spring-boot", "3.0.0"),
new Project("spring-cloud", "4.0.0"));
List<ConfigurationProperty> configurationProperties = Arrays.asList(
new ConfigurationProperty("first", "firstDefault", "firstDescription"),
new ConfigurationProperty("second", "secondDefault",
"secondDescription"));
File outputFolder = new TemplateGenerator(file).generate(projects,
configurationProperties);
BDDAssertions.then(outputFolder).isNotEmptyDirectory();
}
@Test
void should_generate_adocs_from_spring_cloud_sleuth_docs()
throws URISyntaxException, IOException {
File generatedAdocs = new File("target/test-train-sleuth-docs");
FileSystemUtils.deleteRecursively(generatedAdocs);
File testPom = new File(GenerateReleaseTrainDocsTests.class
.getResource("/test/sleuth-only.xml").toURI());
File starterPom = new File(GenerateReleaseTrainDocsTests.class
.getResource("/test/starter-pom.xml").toURI());
File unzippedDocs = new File("target/test-unpacked-sleuth-docs/");
GenerateReleaseTrainDocs.main(testPom.getAbsolutePath(),
starterPom.getAbsolutePath(), "https://github.com/spring-cloud/",
unzippedDocs.getAbsolutePath(), generatedAdocs.getAbsolutePath());
BDDAssertions.then(generatedAdocs).isNotEmptyDirectory();
BDDAssertions.then(configProps(generatedAdocs)).contains(
"|spring.sleuth.async.configurer.enabled | true | Enable default AsyncConfigurer.");
}
@After
public void closeContext() {
if (this.context != null) {
this.context.close();
}
FileSystemUtils.deleteRecursively(new File("transaction-logs"));
}
void deleteLocalRepository() {
try {
FileSystemUtils.deleteRecursively(this.localRepositoryLocation);
}
catch (IOException ex) {
// Continue
}
}
public void close() throws IOException {
if (testDriver != null) {
try {
testDriver.close();
} catch (StreamsException e) {
// temporary workaround for https://github.com/apache/kafka/pull/4713
if (!(e.getCause() instanceof DirectoryNotEmptyException)) {
throw e;
}
}
}
FileSystemUtils.deleteRecursively(kafkaTempDir);
}
@BeforeClass
public static void copyFiles()
throws URISyntaxException, IOException {
csvFilesPath = Paths.get(new ClassPathResource("csv").getURI());
testInputsPath = Paths.get("target/test-inputs");
try {
Files.createDirectory(testInputsPath);
} catch (Exception e) {
// if directory exists do nothing
}
FileSystemUtils.copyRecursively(csvFilesPath, testInputsPath);
}