mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
859aac43b3
commit
fa324608b2
@ -1,7 +1,6 @@
|
||||
package cn.hutool.poi.excel;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import org.apache.poi.poifs.filesystem.FileMagic;
|
||||
|
||||
import java.io.File;
|
||||
@ -10,50 +9,35 @@ import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Excel文件工具类
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
* @since 4.2.1
|
||||
*/
|
||||
public class ExcelFileUtil {
|
||||
// ------------------------------------------------------------------------------------------------ isXls
|
||||
|
||||
/**
|
||||
* 是否为XLS格式的Excel文件(HSSF)<br>
|
||||
* XLS文件主要用于Excel 97~2003创建
|
||||
*
|
||||
* XLS文件主要用于Excel 97~2003创建<br>
|
||||
* 此方法会自动调用{@link InputStream#reset()}方法
|
||||
*
|
||||
* @param in excel输入流
|
||||
* @return 是否为XLS格式的Excel文件(HSSF)
|
||||
*/
|
||||
public static boolean isXls(InputStream in) {
|
||||
/*
|
||||
* {@link java.io.PushbackInputStream}
|
||||
* PushbackInputStream的markSupported()为false,并不支持mark和reset
|
||||
* 如果强转成PushbackInputStream在调用FileMagic.valueOf(inputStream)时会报错
|
||||
* {@link FileMagic}
|
||||
* 报错内容:getFileMagic() only operates on streams which support mark(int)
|
||||
* 此处修改成 final InputStream inputStream = FileMagic.prepareToCheckMagic(in)
|
||||
* @author kefan.qu
|
||||
*/
|
||||
final InputStream inputStream = FileMagic.prepareToCheckMagic(in);
|
||||
try {
|
||||
return FileMagic.valueOf(inputStream) == FileMagic.OLE2;
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
return FileMagic.OLE2 == getFileMagic(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为XLSX格式的Excel文件(XSSF)<br>
|
||||
* XLSX文件主要用于Excel 2007+创建
|
||||
*
|
||||
* XLSX文件主要用于Excel 2007+创建<br>
|
||||
* 此方法会自动调用{@link InputStream#reset()}方法
|
||||
*
|
||||
* @param in excel输入流
|
||||
* @return 是否为XLSX格式的Excel文件(XSSF)
|
||||
*/
|
||||
public static boolean isXlsx(InputStream in) {
|
||||
try {
|
||||
return FileMagic.valueOf(IoUtil.toMarkSupportStream(in)) == FileMagic.OOXML;
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
return FileMagic.OOXML == getFileMagic(in);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,4 +55,27 @@ public class ExcelFileUtil {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link java.io.PushbackInputStream}
|
||||
* PushbackInputStream的markSupported()为false,并不支持mark和reset
|
||||
* 如果强转成PushbackInputStream在调用FileMagic.valueOf(inputStream)时会报错
|
||||
* {@link FileMagic}
|
||||
* 报错内容:getFileMagic() only operates on streams which support mark(int)
|
||||
* 此处修改成 final InputStream inputStream = FileMagic.prepareToCheckMagic(in)
|
||||
*
|
||||
* @param in {@link InputStream}
|
||||
* @author kefan.qu
|
||||
*/
|
||||
private static FileMagic getFileMagic(InputStream in) {
|
||||
FileMagic magic;
|
||||
final InputStream inputStream = FileMagic.prepareToCheckMagic(in);
|
||||
try {
|
||||
magic = FileMagic.valueOf(inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
|
||||
return magic;
|
||||
}
|
||||
}
|
||||
|
33
hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelFileUtilTest.java
Executable file
33
hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelFileUtilTest.java
Executable file
@ -0,0 +1,33 @@
|
||||
package cn.hutool.poi.excel;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ExcelFileUtilTest {
|
||||
|
||||
@Test
|
||||
public void xlsTest(){
|
||||
InputStream in = FileUtil.getInputStream("aaa.xls");
|
||||
try{
|
||||
Assert.assertTrue(ExcelFileUtil.isXls(in));
|
||||
Assert.assertFalse(ExcelFileUtil.isXlsx(in));
|
||||
} finally {
|
||||
IoUtil.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xlsxTest(){
|
||||
InputStream in = FileUtil.getInputStream("aaa.xlsx");
|
||||
try{
|
||||
Assert.assertFalse(ExcelFileUtil.isXls(in));
|
||||
Assert.assertTrue(ExcelFileUtil.isXlsx(in));
|
||||
} finally {
|
||||
IoUtil.close(in);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user