1
private void threadToZip(MultipartFile file) {
new Thread() {
public void run() {
ZipInputStream zipInputStream = null;
ByteArrayOutputStream arrayOutputStream = null;
try {
zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));
for(ZipEntry zipEntry; (zipEntry = zipInputStream.getNextEntry()) != null;){
if(!zipEntry.isDirectory()){
byte[] buffer = new byte[1024];
arrayOutputStream=new ByteArrayOutputStream();
for(int i;(i=zipInputStream.read(buffer)) > -1;){
arrayOutputStream.write(buffer,0,i);
}
arrayOutputStream.flush();
// 关闭当前zip条目并准备读取下一个条目
zipInputStream.closeEntry();
// 读入构造器:文件流,读取成哪个Java对象,读取监听器
ExcelReader excelReader = EasyExcel.read(new ByteArrayInputStream(arrayOutputStream.toByteArray()), ComplaintWorkOrders.class, easyExcelListener)
.excelType(ExcelTypeEnum.CSV).registerConverter(new CustomConverter())
.charset(CharsetUtil.CHARSET_GBK)
.build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet);
excelReader.finish();
}
}
} catch (Exception e) {
}finally {
IOUtils.close(arrayOutputStream);
IOUtils.close(zipInputStream);
}
}
}.start();
}
2
private void threadToZip(MultipartFile file) {
new Thread() {
public void run() {
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
InputStream zipInputStream = null;
try {
inputStream = file.getInputStream();
File newFile = new File(file.getOriginalFilename()+System.currentTimeMillis());
fileOutputStream = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
for(int bytesRead ;(bytesRead = inputStream.read(buffer,0,1024)) != -1;){
fileOutputStream.write(buffer,0,bytesRead);
}
fileOutputStream.flush();
ZipFile zipFile = new ZipFile(newFile);
for(Enumeration<? extends ZipEntry> e = zipFile.entries();e.hasMoreElements();){
zipInputStream = zipFile.getInputStream(e.nextElement());
// 读入构造器:文件流,读取成哪个Java对象,读取监听器
ExcelReader excelReader = EasyExcel.read(zipInputStream, ComplaintWorkOrders.class, easyExcelListener)
.excelType(ExcelTypeEnum.CSV).registerConverter(new CustomConverter())
.charset(CharsetUtil.CHARSET_GBK)
.build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet);
excelReader.finish();
}
//删除临时文件
newFile.delete();
} catch (Exception e) {
}finally {
IOUtils.close(zipInputStream);
IOUtils.close(fileOutputStream);
IOUtils.close(inputStream);
}
}
}.start();
}