This commit is contained in:
Looly 2021-01-22 21:48:28 +08:00
parent b7ca34d0e8
commit 93df8333df
6 changed files with 73 additions and 26 deletions

View File

@ -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");
}
}

View File

@ -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 {

View File

@ -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));
}

View File

@ -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
*

View File

@ -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");

View File

@ -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();
}
}