public String importMonitorScript(HttpServletRequest request, HttpServletResponse response, MultipartFile file,
String realName) {
// 得到工作空间
Workbook workbook = null;
ZipInputStream in = null;
String path = request.getSession().getServletContext().getRealPath("/");
String realPath = path + "filedata/monitorScript/";
try {
// 检测编码格式
String code = codeString(file);
in = new ZipInputStream(file.getInputStream(), Charset.forName(code));
// 定义zip包里每一个文件的入口
ZipEntry entry1 = null;
while ((entry1 = in.getNextEntry()) != null) {
allFileName.add(entry1.getName());
long size = entry1.getSize();
if(0 == size){
throw new RuntimeException("文件"+entry1.getName()+"里内容为空");
}
}
in.closeEntry();
in = new ZipInputStream(file.getInputStream(), Charset.forName(code));
entry1 = null;
while ((entry1 = in.getNextEntry()) != null) {
if (entry1.getName().endsWith(".xls") || entry1.getName().endsWith(".xlsx")) {
// 存放的是每行记录的集合
List<Map<String, String>> allList = new
if (entry1.getName().toLowerCase().contains(".xlsx")) {
// 其对应的是EXCEL2007+ ,扩展名为.xlsx
workbook = new XSSFWorkbook(in);
} else {
// 针对EXCEL 2003版本,扩展名为.xls
workbook = new HSSFWorkbook(in);
}
Sheet sheet = workbook.getSheetAt(0);
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
int rowNumData = rowNum + 1;
boolean isNull = isAllNull(row);
if (isNull) {
continue;
}
String cellValue0 = this.getCellValueToString(row.getCell(0));
String cellValue1 = this.getCellValueToString(row.getCell(1));
Map<String, String> map = new HashMap<String, String>();
map.put("cellValue0", cellValue0);
map.put("cellValue1", cellValue1);
allList.add(map);
}
for (Map<String, String> map : allList) {
in = new ZipInputStream(file.getInputStream(), Charset.forName(code));
entry1 = null;
File dest = new File(realPath);
if (!dest.exists() && !dest.isDirectory()) {
dest.setWritable(true, false);
dest.mkdirs();
logger.info("创建文件夹");
} else {
logger.info("文件夹存在");
}
while ((entry1 = in.getNextEntry()) != null) {
if (!StringUtils.isBlank(decFilePathMap.get(entry1.getName()))) {
String path2 = decFilePathMap.get(entry1.getName());
outFile(in, path2);
}
if (!StringUtils.isBlank(filePathMap.get(entry1.getName()))) {
String path2 = filePathMap.get(entry1.getName());
outFile(in, path2);
}
}
in.closeEntry();
} catch (Exception e) {
logger.error("导入失败"+e);
e.printStackTrace();
return e+"";
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
logger.error("关闭inputStream失败");
}
}
}
return "00";
}
public static String codeString(MultipartFile file) {
String code = "";
BufferedInputStream bin = null;
int p;
try {
bin = new BufferedInputStream(file.getInputStream());
p = (bin.read() << 8) + bin.read();
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
} catch (IOException e) {
logger.error("查询编码错误:" + e);
} finally {
if (null != bin) {
try {
bin.close();
} catch (IOException e) {
logger.error("关闭BufferedInputStream流失败:" + e);
}
}
}
return code;
}
private void outFile(ZipInputStream in, String filePath) {
FileOutputStream out = null;
try {
out = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
logger.error("写出文件错误:" + e);
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
logger.error("关闭out失败");
}
}
}
}
05-24
09-01
1195