写这个小demo的目的是喜欢Windows 10的锁屏背景图,想用来做桌面,可是打开存放路径发现都没有后缀,而且非常多张图片,手动修改过于麻烦。又因为正在学spring boot,索性用spring boot做一个小demo,遍历给定文件夹下的所有文件,添加”.jpg”后缀。
没有技术难度,纯粹出于好玩。
package com.example.demo.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
/**
* Author: 木丁_luxam
* Package:com.example.demo.web
* Date: 2017/9/1
* Time: 9:40
*/
@RestController
@RequestMapping(value = "/rename")
public class RenameController {
@RequestMapping(value = "")
public String rename(@RequestParam String path){
//文件夹路径
File folder = new File(path);
//判断文件夹是否存在
if(!folder.exists()){
return "文件夹不存在";
}
//文件夹下所有的文件数组
File[] files = folder.listFiles();
//重命名
for (File file :
files) {
File newFile = new File(file.getAbsolutePath() + ".jpg");
file.renameTo(newFile);
}
return "批量重命名成功!";
}
}