信息和图片附件导入导出

背景是cms信息发布,在发布信息是导出信息和图片、附件,同事发送到指定的主机,进行数据导入,实现信息共享,在此是信息的导出导入部分,信息压缩后传递部分见我的文章HttpURLConnection上传文件

导出代码

static final int BUFFER = 2048;
	/**
	 * 数据导出
	 * @param strSiteID 站点编号
	 * @param strInfoID 欲导出数据keyid
	 * @param ContextPath 当前项目名称
	 * @return
	 */
	public boolean expInfo(String strSiteID, String strInfoID,String ContextPath) {
		ConfigInfo configinfo = new ConfigInfo();
		String strPath = configinfo.getPropertiesValue("parameter", "workpath")+ "exp";
		String path = configinfo.getPropertiesValue("parameter", "workpath").substring(0,configinfo.getPropertiesValue("parameter", "workpath").length() - 1);
		P_web info = new P_web();
		String strInfoPath = "";
		String strInfoFile = "";
		try {
			// 填充bean
			DBService dbservice = new DBService();
			DBSet dbset = new DBSet();
			String strSQL = "";
			strSQL = "SELECT * FROM "+strSiteID+"_web WHERE KEYID='" + strInfoID + "'";
			dbset = dbservice.getSelectSet(strSQL);
			if (dbset.size() > 0) {
				info.setKEYID(dbset.getValue("KEYID"));
				info.setTID(dbset.getValue("TID"));
				info.setTITLE(dbset.getValue("TITLE"));
				info.setTITLESTYLE(dbset.getValue("TITLESTYLE"));
				info.setTITLEPICURL(dbset.getValue("TITLEPICURL"));
				info.setTITLEFILEURL(dbset.getValue("TITLEFILEURL"));
				info.setINDEXTITLENUM(dbset.getIntValue("INDEXTITLENUM"));
				info.setTITLEBRWORDNUM(dbset.getIntValue("TITLEBRWORDNUM"));
				info.setBGTITLE(dbset.getValue("BGTITLE"));
				info.setSUBHEAD(dbset.getValue("SUBHEAD"));
				info.setINFOSUMMARY(dbset.getValue("INFOSUMMARY"));
				info.setCOLUMNID(dbset.getValue("COLUMNID"));
				info.setCOLUMNNAME(dbset.getValue("COLUMNNAME"));
				info.setDEPTID(dbset.getValue("DEPTID"));
				info.setDEPTNAME(dbset.getValue("DEPTNAME"));
				info.setSHOWTYPE(dbset.getValue("SHOWTYPE"));
				info.setTSTATE(dbset.getValue("TSTATE"));
				info.setDOTNUM(dbset.getIntValue("DOTNUM"));
				info.setINFOSOURCE(dbset.getValue("INFOSOURCE"));
				info.setAUTHOR(dbset.getValue("AUTHOR"));
				info.setAGENTIP(dbset.getValue("AGENTIP"));
				info.setAGENTID(dbset.getValue("AGENTID"));
				info.setAGENTNAME(dbset.getValue("AGENTNAME"));
				info.setENDTIME(dbset.getValue("ENDTIME"));
				info.setRECORDTIME(dbset.getValue("RECORDTIME"));
				info.setENDEDITTIME(dbset.getValue("ENDEDITTIME"));
				info.setENDEDITORNAME(dbset.getValue("ENDEDITORNAME"));
				info.setRELEASETIME(dbset.getValue("RELEASETIME"));
				info.setRELEASENAME(dbset.getValue("RELEASENAME"));
				info.setRELATINGINFO(dbset.getValue("RELATINGINFO"));
				info.setISCRITIC(dbset.getValue("ISCRITIC"));
				info.setISWELCOMEPAGE(dbset.getValue("ISWELCOMEPAGE"));
				info.setISHASIMG(dbset.getValue("ISHASIMG"));
				info.setISHASVIDEO(dbset.getValue("ISHASVIDEO"));
				info.setSORTID(dbset.getIntValue("SORTID"));
				info.setTOPID(dbset.getIntValue("TOPID"));
				info.setISPAGE(dbset.getValue("ISPAGE"));
				info.setMAXCHARPERPAGE(dbset.getIntValue("MAXCHARPERPAGE"));
				info.setISSIGN(dbset.getValue("ISSIGN"));
				info.setHTMLURL(dbset.getValue("HTMLURL"));
				info.setSITEID(dbset.getValue("SITEID"));
				info.setINFOTYPE(dbset.getValue("INFOTYPE"));
				info.setISSTATIC(dbset.getValue("ISSTATIC"));
				P_webContent p_webc = new P_webContent();
				p_webc.setTID(dbset.getValue("TID"));
				p_webc.setSiteID(dbset.getValue("SITEID"));
				p_webc.setColumnID(dbset.getValue("COLUMNID"));
				String strSQL1 = "select WebContent from " + strSiteID
						+ "_webcontent where TID='" + strInfoID + "'";
				p_webc.setWebContent(dbservice.getClob(strSQL1, "WebContent"));
				info.setWebc(p_webc);
			}
			// 生成目录
			strInfoPath = strPath + "/" + info.getKEYID();
			new File(strInfoPath).mkdirs();

			// 检索WebContent中的图片和附件
			Document doc = Jsoup.parse(info.getWebc().getWebContent());
			// 当前页中的图片
			Elements srcLinks = doc.select("img[src]");
			String imagesPath = "";
			for (Element link : srcLinks) {
				// 剔除标签,只剩链接路径
				String imagesPaths = link.attr("src");
				String ht = imagesPaths.substring(0, 4);
				String htt = imagesPaths.substring(0, 1);
				if (!ht.equals("http") && htt.equals("/")) {
					imagesPath = imagesPaths.trim().replaceAll(ContextPath, "");
				} else {
					imagesPath = "";
				}
				//System.out.println("---导入WebContent中的图片---" + imagesPath);
				if (!imagesPath.equals("")) {
					exportCopy(imagesPath, path, strInfoID);
				}
			}
			// 提取所有的href连接
			String filePaths = "";
			Elements linehrefs = doc.select("a[href]");
			for (Element link : linehrefs) {
				filePaths = link.attr("href").trim()
						.replaceAll(ContextPath, "");
				//System.out.println("---导入WebContent中的文件---" + filePaths);
				if (!filePaths.equals("")) {
					exportCopy(filePaths, path, strInfoID);
				}
			}
			// 导出图片
			if (!info.getTITLEPICURL().equals("")) {
				exportCopy(info.getTITLEPICURL(), path, strInfoID);
			}
			// 导出附件
			if (!info.getTITLEFILEURL().equals("")) {
				exportCopy(info.getTITLEFILEURL(), path, strInfoID);
			}

			// 生成xml文件
			File file = new File(strPath + "/" + info.getKEYID() + "/"
					+ info.getKEYID() + ".xml");
			XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
			encoder.writeObject(info);
			encoder.close();

			// 生成zip包
			BufferedInputStream origin = null;
			strInfoFile = strPath + "/" + info.getKEYID() + ".zip";
			// 删除以前导出的zip文件
			File fInfoFile = new File(strInfoFile);
			if (fInfoFile.exists()) {
				fInfoFile.delete();
			}
			FileOutputStream dest = new FileOutputStream(strInfoFile);
			ZipOutputStream zipOut = new ZipOutputStream(
					new BufferedOutputStream(dest));
			byte data[] = new byte[BUFFER];
			File f = new File(strPath + "/" + info.getKEYID() + "/");
			File files[] = f.listFiles();
			for (int i = 0; i < files.length; i++) {
				FileInputStream fi = new FileInputStream(files[i]);
				origin = new BufferedInputStream(fi, BUFFER);
				ZipEntry entry = new ZipEntry(files[i].getName());
				zipOut.putNextEntry(entry);
				int count;
				while ((count = origin.read(data, 0, BUFFER)) != -1) {
					zipOut.write(data, 0, count);
				}
				origin.close();
			}
			zipOut.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			new FileOperate().deletePath(strInfoPath);
		}
		return true;
	}

导入代码

/**
	 * 数据导入
	 * @param strSiteID 站点编号
	 * @param strColumnID 栏目编号
	 * @param strInfoID 欲导出数据keyid
	 * @param ContextPath 当前项目名称
	 * @param strDespatcher 发送信息人
	 * @param strMechanism 发送信息机构
	 * @param strOther1 其他信息
	 * @return
	 */
	public boolean impInfo(String strSiteID, String strColumnID,String strInfoID, String ContextPath,String strDespatcher,String strMechanism,String strOther1) {
		DBService dbservice = new DBService();
		ConfigInfo configinfo = new ConfigInfo();
		String strPath = configinfo.getPropertiesValue("parameter", "workpath")+ "imp";
		String path = configinfo.getPropertiesValue("parameter", "workpath").substring(0,configinfo.getPropertiesValue("parameter", "workpath").length() - 1);
		String fileName = strPath + "/" + strInfoID + ".zip";
		String filePath = strPath + "/" + strInfoID;
		// 解压缩zip包
		try {
			ZipFile zipFile = new ZipFile(fileName);
			Enumeration<ZipEntry> emu = zipFile.getEntries();
			while (emu.hasMoreElements()) {
				ZipEntry entry = emu.nextElement();
				// 会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
				if (entry.isDirectory()) {
					new File(filePath + "/" + entry.getName()).mkdirs();
					continue;
				}
				InputStream bis = zipFile.getInputStream(entry);
				File file = new File(filePath + "/" + entry.getName());
				// 加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
				// 而这个文件所在的目录还没有出现过,所以要建出目录来。
				File parent = file.getParentFile();
				if (parent != null && (!parent.exists())) {
					parent.mkdirs();
				}
				FileOutputStream fos = new FileOutputStream(file);
				int count;
				byte data[] = new byte[BUFFER];
				while ((count = bis.read(data, 0, BUFFER)) != -1) {
					fos.write(data, 0, count);
				}
				bis.close();
				fos.close();
			}
			zipFile.close();

			// 读取xml文件
			File file = new File(strPath + "/" + strInfoID + "/" + strInfoID
					+ ".xml");
			XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
			P_web info = (P_web) decoder.readObject();
			decoder.close();

			String strSQL = "";
			String keyID=dbservice.getSequence();
			LinkedList<String> listdata = new LinkedList<String>();
			strSQL = "INSERT INTO "+ strSiteID+ "_WEB (KEYID, TID, TITLE, TITLESTYLE, TITLEPICURL, TITLEFILEURL, INDEXTITLENUM, TITLEBRWORDNUM, BGTITLE, SUBHEAD, INFOSUMMARY, COLUMNID, COLUMNNAME, DEPTID, DEPTNAME, SHOWTYPE, TSTATE, DOTNUM, INFOSOURCE, AUTHOR, AGENTIP, AGENTID, AGENTNAME, ENDTIME, RECORDTIME, ENDEDITTIME, ENDEDITORNAME, RELEASETIME, RELEASENAME, RELATINGINFO, ISCRITIC, ISWELCOMEPAGE, ISHASIMG, ISHASVIDEO, SORTID, TOPID, ISPAGE, MAXCHARPERPAGE, ISSIGN, HTMLURL, SITEID, INFOTYPE, ISSTATIC) VALUES ('"
					+ keyID + "'," //info.getKEYID()
					+ "'" + keyID + "'," 
					+ "'" + info.getTITLE() + "'," 
					+ "'" + info.getTITLESTYLE()+ "'," 
					+ "'" + "/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/"+info.getTITLEPICURL().substring(info.getTITLEPICURL().lastIndexOf("/") + 1) + "'," 
					+ "'" + "/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/"+info.getTITLEFILEURL().substring(info.getTITLEFILEURL().lastIndexOf("/")+1) + "'," 
					+ "'" + info.getINDEXTITLENUM() + "'," 
					+ "'" + info.getTITLEBRWORDNUM() + "'," 
					+ "'" + info.getBGTITLE()+ "'," 
					+ "'" + info.getSUBHEAD() + "'," 
					+ "'" + info.getINFOSUMMARY() + "'," 
					+ "'" + strColumnID+ "'," 
					+ "''," //" + info.getCOLUMNNAME() + "
					+ "''," //" + info.getDEPTID() + "
					+ "'',"//" + info.getDEPTNAME() + "
					+ "'" + info.getSHOWTYPE() + "'," 
					+ "'" + info.getTSTATE()+ "'," 
					+ "'" + info.getDOTNUM() + "'," 
					+ "'" + strMechanism + "'," 
					+ "'" + strDespatcher+ "'," 
					+ "''," //" + info.getAGENTIP() + "
					+ "''," //" + info.getAGENTID() + "
					+ "''," //" + info.getAGENTNAME()+ "
					+ "''," //" + info.getENDTIME() + "
					+ "''," //" + info.getRECORDTIME()+ "
					+ "''," //" + info.getENDEDITTIME()+ "
					+ "''," //" + info.getENDEDITORNAME()+ "
					+ "'" + DBService.getDateTime()+ "'," // info.getRELEASETIME() 发布时间
					+ "''," //" + info.getRELEASENAME() + "
					+ "'" + info.getRELATINGINFO() + "'," 
					+ "'" + info.getISCRITIC()+ "'," 
					+ "'" + info.getISWELCOMEPAGE() + "'," 
					+ "'" + info.getISHASIMG() + "'," 
					+ "'" + info.getISHASVIDEO()+ "'," 
					+ "'" + info.getSORTID() + "'," 
					+ "''," //" + info.getTOPID() + "
					+ "'" + info.getISPAGE() + "',"
					+ "'" + info.getMAXCHARPERPAGE() + "'," 
					+ "'" + info.getISSIGN() + "'," 
					+ "'',"//" + info.getHTMLURL() + "
					+ "'" + strSiteID + "'," 
					+ "'" + info.getINFOTYPE()+ "'," 
					+ "'" + info.getISSTATIC() + "')";
			//System.out.println("---strSQL---"+strSQL.replaceAll("/html/"+info.getWebc().getSiteID()+"/"+info.getCOLUMNID()+"/"+info.getKEYID()+"/", "/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/"));
			listdata.add(strSQL.replaceAll("/html/"+info.getWebc().getSiteID()+"/"+info.getCOLUMNID()+"/"+info.getKEYID()+"/", "/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/"));
			strSQL = "INSERT INTO "
					+ strSiteID
					+ "_WEBCONTENT (TID, SITEID, COLUMNID, WEBCONTENT ) VALUES ('"
					+ keyID + "'," + "'"
					+ strSiteID + "'," + "'"
					+ strColumnID + "',?)";
			dbservice.getUpdateClob(strSQL, info.getWebc().getWebContent().replaceAll("/html/"+info.getWebc().getSiteID()+"/"+info.getCOLUMNID()+"/"+info.getKEYID()+"/", "/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/"));
			// listdata.add(strSQL);
			if (!dbservice.getTransaction(listdata)) {
				return false;
			}
			// 检索WebContent中的图片和附件
			Document doc = Jsoup.parse(info.getWebc().getWebContent());
			// 当前页中的图片
			Elements srcLinks = doc.select("img[src]");
			String imagesPath = "";
			for (Element link : srcLinks) {
				// 剔除标签,只剩链接路径
				String imagesPaths = link.attr("src");
				String ht = imagesPaths.substring(0, 4);
				String htt = imagesPaths.substring(0, 1);
				if (!ht.equals("http") && htt.equals("/")) {
					imagesPath = imagesPaths.trim().replaceAll(ContextPath, "");
					imagesPath = imagesPath.substring(imagesPath.lastIndexOf("/") + 1);
				} else {
					imagesPath = "";
				}
				//System.out.println("---导入WebContent中的图片---" + imagesPath);
				if (!imagesPath.equals("")) {
					importCopy("/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/"+imagesPath, path, strInfoID,"/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/");
				}
			}
			// 提取所有的href连接
			String filePaths = "";
			Elements linehrefs = doc.select("a[href]");
			for (Element link : linehrefs) {
				filePaths = link.attr("href").trim().replaceAll(ContextPath, "");
				filePaths = filePaths.substring(filePaths.lastIndexOf("/") + 1);
				//System.out.println("---导入WebContent中的文件---" + filePaths);
				if (!filePaths.equals("")) {
					importCopy("/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/"+filePaths, path, strInfoID,"/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/");
				}
			}
			// 导入图片
			if (!info.getTITLEPICURL().equals("")) {
				importCopy(info.getTITLEPICURL(), path, strInfoID,"/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/");
			}
			// 导入附件
			if (!info.getTITLEFILEURL().equals("")) {
				importCopy(info.getTITLEFILEURL(), path, strInfoID,"/html/"+strSiteID+"/"+strColumnID+"/"+keyID+"/");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			// 删除文件
			new File(fileName).delete();
			new FileOperate().deletePath(filePath); 
		}
		return true;
	}

把文件和图片复制到指定文件夹

/**
	 * 导出
	 * @param urlPath 图片相对路径(数据库存储路径)
	 * @param path 项目绝对路径
	 * @param keyID 要导出信息的keyID
	 */
	private static void exportCopy(String urlPath, String path, String keyID) {
		String filename = urlPath.substring(urlPath.lastIndexOf("/") + 1);
		FileInputStream fi = null;
		FileOutputStream fo = null;
		FileChannel in = null;
		FileChannel out = null;
		try {
			fi = new FileInputStream(path + urlPath);
			fo = new FileOutputStream(path + "/exp/" + keyID + "/" + filename);
			in = fi.getChannel();// 得到对应的文件通道
			out = fo.getChannel();// 得到对应的文件通道
			in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				in.close();
				fo.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 导入
	 * @param urlPath 附件相对路径(xml存储路径)
	 * @param path 项目绝对路径
	 * @param keyID 要导出信息的keyID
	 * @param filepath 导入后路径
	 */
	private static void importCopy(String urlPath, String path, String keyID, String filepath) {
		// 生成目录
		File f = new File(path + filepath);
		if (!f.exists()) {
			f.mkdirs();
		}
		String filename = urlPath.substring(urlPath.lastIndexOf("/") + 1);
		FileInputStream fi = null;
		FileOutputStream fo = null;
		FileChannel in = null;
		FileChannel out = null;
		try {
			fi = new FileInputStream(path + "/imp/" + keyID + "/" + filename);
			fo = new FileOutputStream(path + filepath + filename);
			in = fi.getChannel();// 得到对应的文件通道
			out = fo.getChannel();// 得到对应的文件通道
			in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				in.close();
				fo.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值