All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weixin.popular.bean.xmlmessage.XMLMessage Maven / Gradle / Ivy

Go to download

The weixin-popular is a JAVA SDK for weixin. Weixin web url is https://mp.weixin.qq.com.

There is a newer version: 2.8.32
Show newest version
package weixin.popular.bean.xmlmessage;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;

public abstract class XMLMessage {

	private String toUserName;
	private String fromUserName;
	private String msgType;

	protected XMLMessage(String toUserName, String fromUserName, String msgType) {
		super();
		this.toUserName = toUserName;
		this.fromUserName = fromUserName;
		this.msgType = msgType;
	}

	/**
	 * 子类自定义XML
	 * @return XML
	 */
	public abstract String subXML();

	public String toXML(){
		StringBuilder sb = new StringBuilder();
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append(""+System.currentTimeMillis()/1000+"");
		sb.append("");
		sb.append(subXML());
		sb.append("");
		return sb.toString();
	}

	public boolean outputStreamWrite(OutputStream outputStream){
		try {
			outputStream.write(toXML().getBytes("utf-8"));
			outputStream.flush();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean outputStreamWrite(OutputStream outputStream,WXBizMsgCrypt bizMsgCrypt){
		if(bizMsgCrypt != null){
			try {
				String outputStr = bizMsgCrypt.encryptMsg(toXML(), System.currentTimeMillis()+"",UUID.randomUUID().toString());
				outputStream.write(outputStr.getBytes("utf-8"));
				outputStream.flush();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			} catch (AesException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}else{
			return outputStreamWrite(outputStream);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy