旺旺 QQ的聊天记录(含图片)保存到自己的服务器

其实这个功能的总思路是 包所有的QQ聊天记录和旺旺的聊天记录先拷贝到word,让word把图片进行base64的加密,切记word保存时候的格式是mht的

比如我保存了一份聊天记录,用notepad++打开


可以看到在<!--StartFragment-->
          <!--EndFragment-->标签内容才是你拷贝的内容图片后面的src是content-Location后面的字符


后面我们就是解析了

public String newAddServeRecord() {
		InputStreamReader r = null;
		BufferedReader br = null;
		try {
			String uploadPath = Tools.getStrutsProp().getValue("uploadPath")
					+ "recordImgs/";
			File recf = new File(uploadPath);
			recf.mkdirs();
			String urlPath = Tools.getStrutsProp().getValue("urlPath")
					+ "recordImgs/";

			r = new InputStreamReader(new FileInputStream(mhtFile), "UTF-8");
			br = new BufferedReader(r);
			
			String result = "";
			Map<String, String> imgMap = new HashMap<String, String>();
			
			String startFragment = "<!--StartFragment-->";
			String endFragment = "<!--EndFragment-->";
			String base64 = "Content-Transfer-Encoding: base64";
			String location = "Content-Location: ";

			String str = null;
			while ((str = br.readLine()) != null) {
				if (str.indexOf(startFragment) != -1) {

					if (str.indexOf(endFragment) != -1) {
						result += str.substring(str.indexOf(startFragment)
								+ startFragment.length(),
								str.indexOf(endFragment));
					} else {
						result += str.substring(str.indexOf(startFragment)
								+ startFragment.length(), str.length());
					}
				}

				if (str.indexOf(endFragment) != -1) {
					result += str.substring(0, str.indexOf(endFragment));
				}

				if (str.indexOf(base64) != -1) {
					String locationPath = br.readLine();
					locationPath = locationPath.replace(location, "");

					br.readLine();
					String base64Img = br.readLine();
					imgMap.put(locationPath, base64Img);
				}

			}

			result = result.replaceAll("=20", " ").replaceAll("=3D", "=")
					.replaceAll("<IMG", "<img");

			for (String key : imgMap.keySet()) {
				byte[] imgByte = Base64.decode(imgMap.get(key));

				String fileName = Tools.getDateToSEC() + Tools.getThree()
						+ key.substring(key.lastIndexOf("."));

				FileOutputStream imgOut = new FileOutputStream(uploadPath
						+ fileName);
				imgOut.write(imgByte);
				imgOut.close();
				result = result.replace(key, urlPath + fileName);
			}
			
			// 写入数据库
			ServeRecord sr = new ServeRecord();
			sr.setDate(new Date());
			sr.setContent(result);
			sr.setServeCustomer(((Admin) getSession().get(Constant.ADMIN))
					.getName());
			sr.setCustomerId(customer.getId() + "");
			serveRecordDao.insert(sr);
			state = "okrecord";
		} catch (Exception e) {
			logger.error("CustomerManagerAction.newAddServeRecord", e);
			return ERROR;
		} finally {
			try {
				if (null != br) {
					br.close();
				}
				if (null != r) {
					r.close();
				}
			} catch (IOException e) {
				logger.error(
						"CustomerManagerAction.newAddServeRecord[close] error!",
						e);
				return ERROR;
			}
		}
		return SUCCESS;
	}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值