weixin.popular.bean.xmlmessage.XMLMessage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weixin-popular Show documentation
Show all versions of weixin-popular Show documentation
The weixin-popular is a JAVA SDK for weixin. Weixin web url is https://mp.weixin.qq.com.
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);
}
}
}