【方法记录】前端传入文件地址,后端把对应文件下的.png(可以改)的中文名全部转为拼音

pom

   <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.0</version>
        </dependency>

代码

package com.sci.web.system.controller;

import com.sci.common.core.controller.BaseController;
import com.sci.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * glzz菜单Controller
 *
 * @author sci
 * @date 2024-05-13
 */
@SuppressWarnings({"DefaultAnnotationParam", "PointlessBooleanExpression"})
@Api(value = "glzz菜单", tags = {"glzz菜单API"})
@RestController
@RequestMapping("/system/glzzMenuCopy")

@Slf4j
public class TGlzzMenuCopyController extends BaseController {

    /**
     * 读取目标文件夹下文件并全部转为pinyin
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "directoryPath", value = "文件地址", required = false, paramType = "query"),
    })
    @ApiOperation("读取目标文件夹下文件并全部转为pinyin")
    @GetMapping("/changeNameToPinyin")
    public AjaxResult changeNameToPinyin(
            @RequestParam(value = "directoryPath", required = false) String directoryPath
    ) {

//      读取目标文件夹下文件并全部转为应为
//        String directoryPath = "D:\\erzhongicons";
        renameIconsInDirectory(directoryPath);
        return success();
    }

    public static void renameIconsInDirectory(String directoryPath) {
        File directory = new File(directoryPath);
        if (!directory.exists() || !directory.isDirectory()) {
            System.out.println("Directory does not exist or is not a directory: " + directoryPath);
            return;
        }

        File[] files = directory.listFiles((dir, name) -> name.endsWith(".png"));
        if (files != null) {
            for (File file : files) {
                String originalName = file.getName();
                String newName = renameIcon(originalName);

                File newFile = new File(directory, newName);
                try {
                    Files.move(file.toPath(), newFile.toPath());
                    System.out.println("Renamed: " + originalName + " to " + newName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String renameIcon(String originalText) {
        StringBuilder pinyinBuilder = new StringBuilder();
        char[] chars = originalText.toCharArray();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for (char c : chars) {
            if (Character.toString(c).matches("[\\u4e00-\\u9fa5]+")) { // 检查是否为中文字符
                try {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null) {
                        pinyinBuilder.append(pinyinArray[0]);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                // 非中文字符直接添加到拼音字符串中(如果需要的话)
                // 这里可以根据你的需求来处理非中文字符
                pinyinBuilder.append(c);
            }
        }

        return pinyinBuilder.toString();
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值