mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix bom
This commit is contained in:
parent
b7ca34d0e8
commit
93df8333df
@ -108,4 +108,11 @@ public class CaptchaTest {
|
||||
captcha.write("d:/test/gif_captcha.gif");
|
||||
assert captcha.verify(captcha.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bgTest(){
|
||||
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 1);
|
||||
captcha.setBackground(Color.WHITE);
|
||||
captcha.write("d:/test/test.jpg");
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PushbackInputStream;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
|
||||
/**
|
||||
* 读取带BOM头的流内容,<code>getCharset()</code>方法调用后会得到BOM头的编码,且会去除BOM头<br>
|
||||
* 读取带BOM头的流内容,{@code getCharset()}方法调用后会得到BOM头的编码,且会去除BOM头<br>
|
||||
* BOM定义:http://www.unicode.org/unicode/faq/utf_bom.html<br>
|
||||
* <ul>
|
||||
* <li>00 00 FE FF = UTF-32, big-endian</li>
|
||||
@ -46,10 +46,20 @@ public class BOMInputStream extends InputStream {
|
||||
}
|
||||
// ----------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 获取默认编码
|
||||
*
|
||||
* @return 默认编码
|
||||
*/
|
||||
public String getDefaultCharset() {
|
||||
return defaultCharset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BOM头中的编码
|
||||
*
|
||||
* @return 编码
|
||||
*/
|
||||
public String getCharset() {
|
||||
if (!isInited) {
|
||||
try {
|
||||
@ -115,4 +125,4 @@ public class BOMInputStream extends InputStream {
|
||||
|
||||
isInited = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -638,7 +638,7 @@ public class FileUtil extends PathUtil {
|
||||
* @return 父目录
|
||||
*/
|
||||
public static File mkParentDirs(File file) {
|
||||
if(null == file){
|
||||
if (null == file) {
|
||||
return null;
|
||||
}
|
||||
return mkdir(file.getParentFile());
|
||||
@ -1019,8 +1019,8 @@ public class FileUtil extends PathUtil {
|
||||
* @param isRetainExt 是否保留原文件的扩展名,如果保留,则newName不需要加扩展名
|
||||
* @param isOverride 是否覆盖目标文件
|
||||
* @return 目标文件
|
||||
* @since 3.0.9
|
||||
* @see PathUtil#rename(Path, String, boolean)
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static File rename(File file, String newName, boolean isRetainExt, boolean isOverride) {
|
||||
if (isRetainExt) {
|
||||
@ -1704,6 +1704,17 @@ public class FileUtil extends PathUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取带BOM头的文件为Reader
|
||||
*
|
||||
* @param file 文件
|
||||
* @return BufferedReader对象
|
||||
* @since 5.5.8
|
||||
*/
|
||||
public static BufferedReader getBOMReader(File file) {
|
||||
return IoUtil.getReader(getBOMInputStream(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个文件读取器
|
||||
*
|
||||
@ -2919,8 +2930,8 @@ public class FileUtil extends PathUtil {
|
||||
/**
|
||||
* 将流的内容写入文件
|
||||
*
|
||||
* @param dest 目标文件
|
||||
* @param in 输入流
|
||||
* @param dest 目标文件
|
||||
* @param in 输入流
|
||||
* @param isCloseIn 是否关闭输入流
|
||||
* @return dest
|
||||
* @throws IORuntimeException IO异常
|
||||
@ -3183,17 +3194,17 @@ public class FileUtil extends PathUtil {
|
||||
*/
|
||||
public static String getMimeType(String filePath) {
|
||||
String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath);
|
||||
if(null == contentType){
|
||||
if (null == contentType) {
|
||||
// 补充一些常用的mimeType
|
||||
if(filePath.endsWith(".css")){
|
||||
if (filePath.endsWith(".css")) {
|
||||
contentType = "text/css";
|
||||
} else if(filePath.endsWith(".js")){
|
||||
} else if (filePath.endsWith(".js")) {
|
||||
contentType = "application/x-javascript";
|
||||
}
|
||||
}
|
||||
|
||||
// 补充
|
||||
if(null == contentType){
|
||||
if (null == contentType) {
|
||||
contentType = getMimeType(Paths.get(filePath));
|
||||
}
|
||||
|
||||
|
@ -222,6 +222,17 @@ public class IoUtil extends NioUtil {
|
||||
return getReader(in, Charset.forName(charsetName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从{@link BOMInputStream}中获取Reader
|
||||
*
|
||||
* @param in {@link BOMInputStream}
|
||||
* @return {@link BufferedReader}
|
||||
* @since 5.5.8
|
||||
*/
|
||||
public static BufferedReader getReader(BOMInputStream in) {
|
||||
return getReader(in, in.getCharset());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个Reader
|
||||
*
|
||||
|
@ -14,7 +14,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link FileUtil} 单元测试类
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class FileUtilTest {
|
||||
@ -26,10 +26,10 @@ public class FileUtilTest {
|
||||
|
||||
// 构建目录中出现非子目录抛出异常
|
||||
FileUtil.file(file, "../ccc");
|
||||
|
||||
|
||||
FileUtil.file("E:/");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getAbsolutePathTest() {
|
||||
String absolutePath = FileUtil.getAbsolutePath("LICENSE-junit.txt");
|
||||
@ -58,7 +58,7 @@ public class FileUtilTest {
|
||||
boolean result = FileUtil.del("e:/Hutool_test_3434543533409843.txt");
|
||||
Assert.assertTrue(result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void delTest2() {
|
||||
@ -118,7 +118,7 @@ public class FileUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equlasTest() {
|
||||
public void equalsTest() {
|
||||
// 源文件和目标文件都不存在
|
||||
File srcFile = FileUtil.file("d:/hutool.jpg");
|
||||
File destFile = FileUtil.file("d:/hutool.jpg");
|
||||
@ -261,7 +261,7 @@ public class FileUtilTest {
|
||||
Console.log(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void listFileNamesTest2() {
|
||||
@ -318,7 +318,7 @@ public class FileUtilTest {
|
||||
String dir = "d:\\aaa\\bbb\\cc\\ddd";
|
||||
int index = FileUtil.lastIndexOfSeparator(dir);
|
||||
Assert.assertEquals(13, index);
|
||||
|
||||
|
||||
String file = "ddd.jpg";
|
||||
int index2 = FileUtil.lastIndexOfSeparator(file);
|
||||
Assert.assertEquals(-1, index2);
|
||||
@ -371,7 +371,7 @@ public class FileUtilTest {
|
||||
Assert.assertNotNull(webRoot);
|
||||
Assert.assertEquals("hutool-core", webRoot.getName());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getMimeTypeTest() {
|
||||
String mimeType = FileUtil.getMimeType("test2Write.jpg");
|
||||
|
@ -5,32 +5,32 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ExcelUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void indexToColNameTest() {
|
||||
Assert.assertEquals("A", ExcelUtil.indexToColName(0));
|
||||
Assert.assertEquals("B", ExcelUtil.indexToColName(1));
|
||||
Assert.assertEquals("C", ExcelUtil.indexToColName(2));
|
||||
|
||||
|
||||
Assert.assertEquals("AA", ExcelUtil.indexToColName(26));
|
||||
Assert.assertEquals("AB", ExcelUtil.indexToColName(27));
|
||||
Assert.assertEquals("AC", ExcelUtil.indexToColName(28));
|
||||
|
||||
|
||||
Assert.assertEquals("AAA", ExcelUtil.indexToColName(702));
|
||||
Assert.assertEquals("AAB", ExcelUtil.indexToColName(703));
|
||||
Assert.assertEquals("AAC", ExcelUtil.indexToColName(704));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void colNameToIndexTest() {
|
||||
Assert.assertEquals(704, ExcelUtil.colNameToIndex("AAC"));
|
||||
Assert.assertEquals(703, ExcelUtil.colNameToIndex("AAB"));
|
||||
Assert.assertEquals(702, ExcelUtil.colNameToIndex("AAA"));
|
||||
|
||||
|
||||
Assert.assertEquals(28, ExcelUtil.colNameToIndex("AC"));
|
||||
Assert.assertEquals(27, ExcelUtil.colNameToIndex("AB"));
|
||||
Assert.assertEquals(26, ExcelUtil.colNameToIndex("AA"));
|
||||
|
||||
|
||||
Assert.assertEquals(2, ExcelUtil.colNameToIndex("C"));
|
||||
Assert.assertEquals(1, ExcelUtil.colNameToIndex("B"));
|
||||
Assert.assertEquals(0, ExcelUtil.colNameToIndex("A"));
|
||||
@ -42,4 +42,12 @@ public class ExcelUtilTest {
|
||||
Assert.assertEquals(0, a11.getX());
|
||||
Assert.assertEquals(10, a11.getY());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAndWriteTest(){
|
||||
ExcelReader reader = ExcelUtil.getReader("d:\\test/select.xls");
|
||||
ExcelWriter writer = reader.getWriter();
|
||||
writer.writeCellValue(1, 2, "设置值");
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user