mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix code
This commit is contained in:
@@ -68,22 +68,6 @@ import java.util.zip.Checksum;
|
|||||||
*/
|
*/
|
||||||
public class FileUtil extends PathUtil {
|
public class FileUtil extends PathUtil {
|
||||||
|
|
||||||
/**
|
|
||||||
* Class文件扩展名
|
|
||||||
*/
|
|
||||||
public static final String CLASS_EXT = FileNameUtil.EXT_CLASS;
|
|
||||||
/**
|
|
||||||
* Jar文件扩展名
|
|
||||||
*/
|
|
||||||
public static final String JAR_FILE_EXT = FileNameUtil.EXT_JAR;
|
|
||||||
/**
|
|
||||||
* 在Jar中的路径jar的扩展名形式
|
|
||||||
*/
|
|
||||||
public static final String JAR_PATH_EXT = ".jar!";
|
|
||||||
/**
|
|
||||||
* 当Path为文件形式时, path会加入一个表示文件的前缀
|
|
||||||
*/
|
|
||||||
public static final String PATH_FILE_PRE = URLUtil.FILE_URL_PREFIX;
|
|
||||||
/**
|
/**
|
||||||
* 文件路径分隔符<br>
|
* 文件路径分隔符<br>
|
||||||
* 在Unix和Linux下 是{@code '/'}; 在Windows下是 {@code '\'}
|
* 在Unix和Linux下 是{@code '/'}; 在Windows下是 {@code '\'}
|
||||||
@@ -94,6 +78,7 @@ public class FileUtil extends PathUtil {
|
|||||||
* 在Unix和Linux下 是{@code ':'}; 在Windows下是 {@code ';'}
|
* 在Unix和Linux下 是{@code ':'}; 在Windows下是 {@code ';'}
|
||||||
*/
|
*/
|
||||||
public static final String PATH_SEPARATOR = File.pathSeparator;
|
public static final String PATH_SEPARATOR = File.pathSeparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 绝对路径判断正则
|
* 绝对路径判断正则
|
||||||
*/
|
*/
|
||||||
@@ -271,7 +256,7 @@ public class FileUtil extends PathUtil {
|
|||||||
if (path == null) {
|
if (path == null) {
|
||||||
return new ArrayList<>(0);
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
int index = path.lastIndexOf(FileUtil.JAR_PATH_EXT);
|
int index = path.lastIndexOf(FileNameUtil.EXT_JAR_PATH);
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
// 普通目录
|
// 普通目录
|
||||||
final List<String> paths = new ArrayList<>();
|
final List<String> paths = new ArrayList<>();
|
||||||
@@ -287,7 +272,7 @@ public class FileUtil extends PathUtil {
|
|||||||
// jar文件
|
// jar文件
|
||||||
path = getAbsolutePath(path);
|
path = getAbsolutePath(path);
|
||||||
// jar文件中的路径
|
// jar文件中的路径
|
||||||
index = index + FileUtil.JAR_FILE_EXT.length();
|
index = index + FileNameUtil.EXT_JAR.length();
|
||||||
JarFile jarFile = null;
|
JarFile jarFile = null;
|
||||||
try {
|
try {
|
||||||
jarFile = new JarFile(path.substring(0, index));
|
jarFile = new JarFile(path.substring(0, index));
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ public class FileNameUtil {
|
|||||||
*/
|
*/
|
||||||
public static final String EXT_JAR = ".jar";
|
public static final String EXT_JAR = ".jar";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在Jar中的路径jar的扩展名形式
|
||||||
|
*/
|
||||||
|
public static final String EXT_JAR_PATH = ".jar!";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 类Unix路径分隔符
|
* 类Unix路径分隔符
|
||||||
*/
|
*/
|
||||||
@@ -40,7 +45,7 @@ public class FileNameUtil {
|
|||||||
/**
|
/**
|
||||||
* Windows下文件名中的无效字符
|
* Windows下文件名中的无效字符
|
||||||
*/
|
*/
|
||||||
private static final Pattern FILE_NAME_INVALID_PATTERN_WIN = Pattern.compile("[\\\\/:*?\"<>|]");
|
private static final Pattern FILE_NAME_INVALID_PATTERN_WIN = Pattern.compile("[\\\\/:*?\"<>|\r\n]");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 特殊后缀
|
* 特殊后缀
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package cn.hutool.core.lang;
|
|||||||
import cn.hutool.core.classloader.ClassLoaderUtil;
|
import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.iter.EnumerationIter;
|
import cn.hutool.core.collection.iter.EnumerationIter;
|
||||||
import cn.hutool.core.io.FileUtil;
|
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
|
import cn.hutool.core.io.file.FileNameUtil;
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
import cn.hutool.core.io.resource.ResourceUtil;
|
||||||
import cn.hutool.core.net.URLDecoder;
|
import cn.hutool.core.net.URLDecoder;
|
||||||
import cn.hutool.core.net.URLUtil;
|
import cn.hutool.core.net.URLUtil;
|
||||||
@@ -298,14 +298,14 @@ public class ClassScanner implements Serializable {
|
|||||||
private void scanFile(final File file, final String rootDir) {
|
private void scanFile(final File file, final String rootDir) {
|
||||||
if (file.isFile()) {
|
if (file.isFile()) {
|
||||||
final String fileName = file.getAbsolutePath();
|
final String fileName = file.getAbsolutePath();
|
||||||
if (fileName.endsWith(FileUtil.CLASS_EXT)) {
|
if (fileName.endsWith(FileNameUtil.EXT_CLASS)) {
|
||||||
final String className = fileName//
|
final String className = fileName//
|
||||||
// 8为classes长度,fileName.length() - 6为".class"的长度
|
// 8为classes长度,fileName.length() - 6为".class"的长度
|
||||||
.substring(rootDir.length(), fileName.length() - 6)//
|
.substring(rootDir.length(), fileName.length() - 6)//
|
||||||
.replace(File.separatorChar, CharUtil.DOT);//
|
.replace(File.separatorChar, CharUtil.DOT);//
|
||||||
//加入满足条件的类
|
//加入满足条件的类
|
||||||
addIfAccept(className);
|
addIfAccept(className);
|
||||||
} else if (fileName.endsWith(FileUtil.JAR_FILE_EXT)) {
|
} else if (fileName.endsWith(FileNameUtil.EXT_JAR)) {
|
||||||
try {
|
try {
|
||||||
scanJar(new JarFile(file));
|
scanJar(new JarFile(file));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
@@ -332,7 +332,7 @@ public class ClassScanner implements Serializable {
|
|||||||
for (final JarEntry entry : new EnumerationIter<>(jar.entries())) {
|
for (final JarEntry entry : new EnumerationIter<>(jar.entries())) {
|
||||||
name = StrUtil.removePrefix(entry.getName(), StrUtil.SLASH);
|
name = StrUtil.removePrefix(entry.getName(), StrUtil.SLASH);
|
||||||
if (StrUtil.isEmpty(packagePath) || name.startsWith(this.packagePath)) {
|
if (StrUtil.isEmpty(packagePath) || name.startsWith(this.packagePath)) {
|
||||||
if (name.endsWith(FileUtil.CLASS_EXT) && false == entry.isDirectory()) {
|
if (name.endsWith(FileNameUtil.EXT_CLASS) && false == entry.isDirectory()) {
|
||||||
final String className = name//
|
final String className = name//
|
||||||
.substring(0, name.length() - 6)//
|
.substring(0, name.length() - 6)//
|
||||||
.replace(CharUtil.SLASH, CharUtil.DOT);//
|
.replace(CharUtil.SLASH, CharUtil.DOT);//
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
package cn.hutool.core.net;
|
package cn.hutool.core.net;
|
||||||
|
|
||||||
|
import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.io.FileUtil;
|
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
|
import cn.hutool.core.io.file.FileNameUtil;
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
import cn.hutool.core.io.resource.ResourceUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.net.url.UrlQuery;
|
import cn.hutool.core.net.url.UrlQuery;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
|
||||||
import cn.hutool.core.classloader.ClassLoaderUtil;
|
|
||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.text.StrUtil;
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -438,7 +438,7 @@ public class URLUtil {
|
|||||||
public static boolean isJarFileURL(final URL url) {
|
public static boolean isJarFileURL(final URL url) {
|
||||||
Assert.notNull(url, "URL must be not null");
|
Assert.notNull(url, "URL must be not null");
|
||||||
return (URL_PROTOCOL_FILE.equals(url.getProtocol()) && //
|
return (URL_PROTOCOL_FILE.equals(url.getProtocol()) && //
|
||||||
url.getPath().toLowerCase().endsWith(FileUtil.JAR_FILE_EXT));
|
url.getPath().toLowerCase().endsWith(FileNameUtil.EXT_JAR));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
15
hutool-core/src/test/java/cn/hutool/core/io/file/FileNameUtilTest.java
Executable file
15
hutool-core/src/test/java/cn/hutool/core/io/file/FileNameUtilTest.java
Executable file
@@ -0,0 +1,15 @@
|
|||||||
|
package cn.hutool.core.io.file;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class FileNameUtilTest {
|
||||||
|
@Test
|
||||||
|
public void cleanInvalidTest(){
|
||||||
|
String name = FileNameUtil.cleanInvalid("1\n2\n");
|
||||||
|
Assert.assertEquals("12", name);
|
||||||
|
|
||||||
|
name = FileNameUtil.cleanInvalid("\r1\r\n2\n");
|
||||||
|
Assert.assertEquals("12", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user