String batchImportPPAPFile() {
String successStr;
request.fileNames.each {
//读取sheet页
MultipartFile multipartFile = request.getFile(it);
InputStream is = multipartFile.getInputStream();
InputStream stream = new BufferedInputStream(is);
//ZipInputStream zinstream = new ZipInputStream(stream);
//解决zip导入时中文名问题
ZipInputStream zinstream = new ZipInputStream(stream,Charset.forName("gbk"));
try {
ZipEntry ze;
while ((ze = zinstream.getNextEntry()) != null) {
String zeName = ze.getName();
String[] arr = zeName.split("/");
int length = arr.length;
if (ze.isDirectory()) {
//具体业务
} else {
System.err.println("file - " + ze.getName() + " : "
+ ze.getSize() + " bytes");
long size = ze.getSize();
if (size > 0) {
if(arr[length-1].lastIndexOf("_")<0&&arr[length-2].lastIndexOf("_")<0){
byte[] data = getByte(zinstream); // 获取当前条目的字节数组
InputStream ins = new ByteArrayInputStream(data); // 把当前条目的字节数据转换成Inputstream流
Workbook workbook = WorkbookFactory.create(ins);
Sheet sheet = workbook.getSheetAt(0)
if(sheet==null){
throw new BusinessException("....")
}
Row row
Cell cell
int num
.......
//具体业务
}else if(arr[length-2].lastIndexOf("_")>0){
byte[] bytes = getByte(zinstream);
UploadFile uploadFile = new UploadFile()
uploadFile.name = arr[length-1]
uploadFile.data = bytes
uploadFile.extension = multipartFile.contentType
uploadFile.length = ze.size
if(dataMap.keySet().contains(arr[length-2])){
List<String> existList = dataMap.get(arr[length-2]);
FileEntity fileEntity = fileEntitySharedService.doSaveFiles(uploadFile, 0L, 0L,null);
FileEntityDto fileEntityDto = new FileEntityDto()
BeanUtils.copyProperties(fileEntityDto, fileEntity)
String textContent=ResponseUtil.getSuccessOm().writeValueAsString(fileEntityDto);
existList.add(textContent+"@"+fileEntity.getTrueName());
dataMap.put(arr[length-2], existList);
}
}
}
}
}
if(dataMap.size != 0 && dataList.size() != 0){
successStr = //具体业务
}else{
throw new BusinessException("上传数据为空,请重新操作");
}
} finally {
zinstream.closeEntry();
is.close()
}
}
webReturn successStr;
}
/**
* 获取条目byte[]字节
* @param zis
* @return
*/
def byte[] getByte(InflaterInputStream zis) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
byte[] buf = null;
int length = 0;
while ((length = zis.read(temp, 0, 1024)) != -1) {
bout.write(temp, 0, length);
}
buf = bout.toByteArray();
bout.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}