java导入zip包,zip中包含excel文件

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失败");
		}
	    }
	}
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值