poi导入excel判断
实现maven依赖
<!--poi-->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.1.0</version>
</dependency>
1、首先可以使用file后缀进行简单判断
String originalFilename = file.getOriginalFilename();
if(StrUtil.isBlank(originalFilename)){
throw new RRException("文件名称不能为空");
}
if(!(originalFilename.endsWith(".xls") || originalFilename.endsWith(".xlsx"))){
throw new RRException("文件既不是xls,也不是xlsx");
}
2、使用FileMagic进行判断
-
FileMagic是基于文件首字节的文件标识,可以通过FileMagic.valueOf(InputStream is)获得
-
/** * Get the file magic of the supplied InputStream (which MUST * support mark and reset).<p> * 注意:InputStream需要能够标记和重置 * If unsure if your InputStream does support mark / reset, * use {@link #prepareToCheckMagic(InputStream)} to wrap it and make * sure to always use that, and not the original!<p> * * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean, * that the ZIP stream has leading junk bytes * * @param inp An InputStream which supports either mark/reset */ public static FileMagic valueOf(InputStream inp) throws IOException { if (!inp.markSupported()) { throw new IOException("getFileMagic() only operates on streams which support mark(int)"); } // Grab the first bytes of this stream byte[] data = IOUtils.peekFirstNBytes(inp, MAX_PATTERN_LENGTH); return FileMagic.valueOf(data); }
-
inp.markSupported()方法判断IO是否能够标记和重置,inputStream默认是返回false,因此可以用BufferedInputStream包一下,缓冲流的markSupported默认返回的是true
-
/** * Tests if this input stream supports the <code>mark</code> and * <code>reset</code> methods. Whether or not <code>mark</code> and * <code>reset</code> are supported is an invariant property of a * particular input stream instance. The <code>markSupported</code> method * of <code>InputStream</code> returns <code>false</code>. * * @return <code>true</code> if this stream instance supports the mark * and reset methods; <code>false</code> otherwise. * @see java.io.InputStream#mark(int) * @see java.io.InputStream#reset() */ public boolean markSupported() { return false; }
-
使用FileMagic判断之前需要判断file是否为空
-
// Grab the first bytes of this stream byte[] data = IOUtils.peekFirstNBytes(inp, MAX_PATTERN_LENGTH); //peekFirstNBytes源码 /** * Peeks at the first N bytes of the stream. Returns those bytes, but * with the stream unaffected. Requires a stream that supports mark/reset, * or a PushbackInputStream. If the stream has >0 but <N bytes, * remaining bytes will be zero. * @throws EmptyFileException if the stream is empty */ public static byte[] peekFirstNBytes(InputStream stream, int limit) throws IOException, EmptyFileException { checkByteSizeLimit(limit); stream.mark(limit); UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(limit); copy(new BoundedInputStream(stream, limit), bos); // 为空时,会抛出EmptyFileException异常,因此为了防止这种异常,我们可以先捕捉,换成我们自己异常 int readBytes = bos.size(); if (readBytes == 0) { throw new EmptyFileException(); } if (readBytes < limit) { bos.write(new byte[limit-readBytes]); } byte[] peekedBytes = bos.toByteArray(); if(stream instanceof PushbackInputStream) { PushbackInputStream pin = (PushbackInputStream)stream; pin.unread(peekedBytes, 0, readBytes); } else { stream.reset(); } return peekedBytes; }
-
下面是我自己实现的,大家可以参考参考
-
InputStream is = new BufferedInputStream(file.getInputStream()); if(is.available() == 0){ throw new RRException("不支持空文件导入"); } if(FileMagic.OLE2 == FileMagic.valueOf(is)){ throw new RRException("这是xls文件"); } if(FileMagic.OOXML == FileMagic.valueOf(is)){ throw new RRException("这是xlsx文件"); } throw new RRException("这里既不是xls文件,也不是xlsx文件");
完整判断
public void importExcel(MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
if(StrUtil.isBlank(originalFilename)){
throw new RRException("文件名称不能为空");
}
if(!(originalFilename.endsWith(".xls") || originalFilename.endsWith(".xlsx"))){
throw new RRException("请导入excel文件");
}
InputStream is = new BufferedInputStream(file.getInputStream());
if(is.available() == 0){
throw new RRException("不支持空文件导入");
}
if(FileMagic.OLE2 == FileMagic.valueOf(is)){
throw new RRException("这是xls文件");
}
if(FileMagic.OOXML == FileMagic.valueOf(is)){
throw new RRException("这是xlsx文件");
}
throw new RRException("这里既不是xls文件,也不是xlsx文件");
}