pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.haan</groupId>
<artifactId>filerename</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>filerename</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件重命名页</title>
</head>
<body>
<form action="rename">
文件路径:<input type="text" name="path"/><br>
截取字段:<input type="text" name="old"/><br>
新字段:<input type="text" name="new"/><br>
<input type="submit" value="批量更改"/>
</form>
</body>
</html>
控制层
package com.haan.filerename.controller;
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;
@RestController
public class FileController {
@RequestMapping("/rename")
public String index(@RequestParam("path") String path,
@RequestParam("old") String oldsub,
@RequestParam("new") String newsub)
{
//文件更改文件夹
File directory = new File(path);
File[] files=directory.listFiles();
for (File file:files){
String oldfilepath = file.getAbsolutePath();
String newfilepath = oldfilepath.replace(oldsub,newsub);
file.renameTo(new File(newfilepath));
System.out.println(file.getName());
}
return "success";
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了