public static String fileExtendName(String fileName) { if (StringUtils.isBlank(fileName)) { return null; } if (!fileName.contains(".")) { return null; } if (fileName.endsWith(".")) { return null; } int index = fileName.lastIndexOf("."); return fileName.substring(index + 1); } public static String simpleFileName(String fileName) { if (StringUtils.isBlank(fileName)) { return null; } if (!fileName.contains(".")) { return null; } if (fileName.endsWith(".")) { return null; } return fileName.substring(0, fileName.lastIndexOf(".")); }
import com.alibaba.fastjson.JSON;import lombok.extern.slf4j.Slf4j; import okhttp3.Response; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.File; import java.io.IOException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.util.*; /** * @Title: FileUtil * @Description: TODO * @Author: [email protected] * @Date: 2021-03-19 15:15:42 */ @Slf4j @Component public class FileUtil extends FileUtils { /** * <功能描述:> 获取指定文件夹下的文件,支持可配置是否扫描其子文件夹下的文件 * */ public static List<File> getFileList(File file, boolean containsChildDir) { List<File> list = new ArrayList<>(); if (!file.isDirectory()) { list.add(file); return list; } File[] childFileList = file.listFiles(); for (File childFile : childFileList) { if (!childFile.isDirectory()) { list.add(childFile); continue; } if (containsChildDir) { list.addAll(getFileList(childFile, containsChildDir)); } } return list; } /** * <功能描述:> 远程文件下载到本地 */ public static void downRemoteFile2Local(String remoteUrl,String localPath){ String filePath = localPath.substring(0,localPath.lastIndexOf("/")+1); File file = new File(filePath); if(!file.exists()){ file.mkdirs(); } try (InputStream in = new URL(remoteUrl).openConnection().getInputStream(); FileOutputStream out = new FileOutputStream(new File(localPath))) { IOUtils.copy(in,out); } catch (Exception e) { log.error("远程文件下载到本地失败,远程地址:{},失败原因:{}",remoteUrl,e); throw new RuntimeException("远程文件下载到本地失败",e); } } public static boolean deleteAllDirAndFile(String delpath) { try { File file = new File(delpath); if (!file.exists()) { return true; } if (!file.isDirectory()) { boolean delFlag = file.delete(); if (!delFlag) { log.warn("文件删除失败!"); } } else if (file.isDirectory()) { String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File delfile = new File(delpath + File.separator + filelist[i]); if (!delfile.isDirectory()) { boolean delFlag = delfile.delete(); if (!delFlag) { log.warn("文件删除失败!"); } } else if (delfile.isDirectory()) { deleteAllDirAndFile(delpath + File.separator + filelist[i]); } } boolean dirDelFlag = file.delete(); if (!dirDelFlag) { log.warn("目录删除失败!"); } } return true; } catch (Exception e) { log.error(String.format("删除[%s]路径下的文件及文件夹失败!", delpath), e); } return false; } }
标签:文件,return,file,fileName,File,操作,import,String From: https://www.cnblogs.com/JYB2021/p/16822601.html