读取压缩包文件,将压缩包的json文件转为为bean保存到数据库,其他文件另存为

一、这里只写出主要代码

1.解析zip

// An highlighted block
public static void parseZip() {
		//String rarPath = "D:/01/2019-04-记-9lht.rar";
		String rarPath = "D:/01/2019-04-记-9lht3.zip";
		String destPath = "D:/01/temp";
		List<File> list = ZipUtil.unZipOrRar(rarPath, destPath,"GBK");
		for (File file : list) {
			if(file.getAbsolutePath().endsWith(".json")) {
				String jsonStr = readJsonFile(file);
				MapModel model = parseJson(jsonStr);
				saveModel(model,file.getName());
			} else {
				//另存文件
				String outPath = "D:/01/downFile/"+file.getName();
				InputStream is = null;
				try {
					is = new FileInputStream(file);
					FileUtil.copyFile(is, outPath);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} finally {
					FileUtil.close(is);
				}

			}
		}
		FileUtil.delete(destPath);

	}
  1. 解析json
// An highlighted block
public static MapModel parseJson(String jsonStr) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		JSONObject obj = JSONObject.fromObject(jsonStr);

		Map<String,Object> classMap = new HashMap<String,Object>();
		classMap.put("main", Map.class); 
		classMap.put("lines", Map.class); 
		MapModel model= (MapModel) JSONObject.toBean(obj, MapModel.class,classMap);
		return model;

	}
  1. 读取json文件
// An highlighted block
public static String readJsonFile(File jsonFile) {
		String jsonStr = "";
		try {
			FileReader fileReader = new FileReader(jsonFile);

			Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
			int ch = 0;
			StringBuffer sb = new StringBuffer();
			while ((ch = reader.read()) != -1) {
				sb.append((char) ch);
			}
			fileReader.close();
			reader.close();
			jsonStr = sb.toString();
			return jsonStr;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
  1. 保存Map数据
// An highlighted block
@SuppressWarnings("rawtypes")
   public static boolean exeMap(Map<String,Object> data,String ClassStr,String tablename) {
   	List<String> valueList = new ArrayList<String>();
   	String sql="";
   	Class demo;
   	try {
   		demo = Class.forName(ClassStr);
   		Field[] fields=demo.getDeclaredFields();
   		if(null != fields && fields.length>0) {
   			sql += "INSERT INTO "+tablename+" (";
   			StringBuffer fieldStr = new StringBuffer();
   			StringBuffer valueStr = new StringBuffer();
   			for (int i = 0; i < fields.length; i++) {
   				String key= fields[i].getName();
   				if(null == data.get(key)) {
   					continue;
   				}
   				if(i== fields.length-1) {
   					fieldStr.append(key+")");
   					valueStr.append("?)");
   					valueList.add(String.valueOf(data.get(key)));
   				} else {
   					fieldStr.append(key+",");
   					valueStr.append("?,");
   					valueList.add(String.valueOf(data.get(key)));
   				}

   			} 
   			sql+= fieldStr + " VALUES(" + valueStr;


   			if(source_jdbc_driver==null){
   				getProperties();
   			}
   			System.out.println(sql);
   			executeUpdate2(sql,valueList,source_jdbc_driver,source_jdbc_url,source_jdbc_username,source_jdbc_password);
   			return true;
   		}

   	}
   	catch (ClassNotFoundException e) {
   		e.printStackTrace();
   		return false;
   	}
   	return false;

   }

你可能感兴趣的:(代码)