mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 20:27:58 +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 {
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个文件读取器
|
||||
*
|
||||
@ -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
|
||||
*
|
||||
|
@ -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");
|
||||
|
@ -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…
Reference in New Issue
Block a user