- 建立http连接,获取BufferedInputStream输入流
public static BufferedInputStream getInputStreamByUrl(String urlStr) {
try {
URL url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setConnectTimeout(1000 * 5);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.connect();
int fileLength = httpURLConnection.getContentLength();
return new BufferedInputStream(httpURLConnection.getInputStream());
} catch (Exception e) {
log.error(e.getMessage());
}
}
- 将BufferedInputStream流转成ZipInputStream,此处需要设置编码为“GBK”
public static ZipInputStream getZipInputStreamByUrl(String urlStr) {
BufferedInputStream inputStream = getInputStreamByUrl(urlStr)
Charset gbk = Charset.forName("gbk")
return new ZipInputStream(inputStream, gbk)
}
- 读取ZipInputStream流中的Excel文件,并将其他文件解压到本地路径,此处需要引入poi-ooxml依赖去解析Excel成XSSFWorkbook对象
public static List<XSSFWorkbook> workbook findExcelInZip(String url, String localUnZipPath) {
ZipInputStream zin = FileUtil.getZipInputStreamByUrl(url)
File pathFile = new File(localUnZipPath)
if (!pathFile.exists()) {
log.info("临时文件夹不存在,创建目录[{}]", localUnZipPath)
pathFile.mkdirs()
}
ByteArrayOutputStream baos = new ByteArrayOutputStream()
List<XSSFWorkbook> workbook = new ArrayList<>()
try {
//读取一个目录
ZipEntry nextEntry = zin.getNextEntry()
//不为空进入循环
while (nextEntry != null) {
String name = nextEntry.getName()
File file = new File(localUnZipPath + name)
//如果是目录,创建目录
if (name.endsWith("/")) {
file.mkdir()
} else {
//文件则写入具体的路径中
String zipFileName = nextEntry.getName()
if (zipFileName.contains(".") && (zipFileName.endsWith("xlsx"))) {
byte[] buffer = new byte[1024]
int len
while ((len = zin.read(buffer)) > -1) {
baos.write(buffer, 0, len)
}
baos.flush()
@Cleanup ByteArrayInputStream stream1 = new ByteArrayInputStream(baos.toByteArray())
workbook.add(new XSSFWorkbook(stream1))
} else {
FileOutputStream fileOutputStream = new FileOutputStream(file)
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)
int n
byte[] bytes = new byte[1024]
while ((n = zin.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, n)
}
//关闭流
bufferedOutputStream.close()
fileOutputStream.close()
}
}
//关闭当前目录
zin.closeEntry()
//读取下一个目录,作为循环条件
nextEntry = zin.getNextEntry()
}
zin.close()
} catch (Exception e) {
log.error(e.getMessage())
}
return workbook
}