摘自:http://blog.youkuaiyun.com/myyate/archive/2008/01/17/2049701.aspx
word 另存为单个网页文件mht,做为导出word模版,需要对替换项进行编码转化
public class WordEncoder {
/**
* 把给定的str转换为10进制表示的unicode,格式为:目前只是用于mht模板的转码
*
* @param str
* @return
*/
public static String encode2HtmlUnicode(String str) {
if (str == null)
return "";
StringBuffer sb = new StringBuffer(str.length() * 2);
for (int i = 0; i < str.length(); i++) {
sb.append(encode2HtmlUnicode(str.charAt(i)));
}
return sb.toString();
}
public static String encode2HtmlUnicode(char character) {
if (character > 255) {
return "&#" + (character & 0xffff) + ";";
} else {
return String.valueOf(character);
}
}
public static String encode2HtmlUnicode(Character character) {
if (character == null)
return null;
return encode2HtmlUnicode(character.charValue());
}
public static void encode2HtmlUnicode(String[] value) {
if (value == null || value.length < 1)
return;
for (int i = 0; i < value.length; i++) {
value[i] = encode2HtmlUnicode(value[i]);
}
}
static String replaceNullString(String str)
{
if(str == null)
{
str="";
}
return str;
}
//
static Object encodeStringAndCharacter(Object value) {
if(value instanceof String) {
return replaceNullString(encode2HtmlUnicode((String) value));
} else if(value instanceof Character) {
return replaceNullString(encode2HtmlUnicode((Character) value));
}
return null;
}
/*static void encodeExceptStringAndCharacter(Object value) throws Exception {
if(value instanceof String[]) {
encode2HtmlUnicode((String[]) value);
} else if(value instanceof List) {
encode2HtmlUnicode((List) value);
} else if(value instanceof Form) {
encode2HtmlUnicode((Form) value);
} else if(value instanceof Form[]) {
encode2HtmlUnicode((Form[]) value);
}
}*/
static void encode2HtmlUnicode(List value) throws Exception {
if(value == null || value.size() < 1) return;
for(int i = 0; i < value.size(); i ++) {
Object ele = value.get(i);
Object result = encodeStringAndCharacter(ele);
if(result != null) {
value.set(i, result);
} else {
//encodeExceptStringAndCharacter(ele);
}
}
}
/*static void encode2HtmlUnicode(Form[] value) throws Exception {
if(value == null) return;
for(int i = 0; i < value.length; i ++) {
encode2HtmlUnicode(value[i]);
}
}*/
/* static void encode2HtmlUnicode(Form value) throws Exception {
if(value == null) return;
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(value.getClass());
if(pds == null) return;
for(int i = 0; i < pds.length; i ++) {
PropertyDescriptor pd = pds[i];
Object fieldValue = PropertyUtils.getProperty(value, pd.getName());
Class fieldType = pd.getPropertyType();
if(String.class.isAssignableFrom(fieldType)) {
PropertyUtils.setProperty(value,
pd.getName(),
StringUtils.replaceNullString(EncodeUtils.encode2HtmlUnicode((String) fieldValue)));
} else if(List.class.isAssignableFrom(fieldType)) {
encode2HtmlUnicode((List) fieldValue);
} else if(Form.class.isAssignableFrom(fieldType)) {
encode2HtmlUnicode((Form) fieldValue);
} else if(Form[].class.isAssignableFrom(fieldType)) {
encode2HtmlUnicode((Form[]) fieldValue);
}
}
}*/
static BASE64Encoder base64Encoder = new BASE64Encoder();
/**
* 把给定的二进制流变成base64码
* @param in
* @return
* @throws IOException
*/
public static synchronized String base64Encode(InputStream in) throws IOException {
byte[] img = new byte[in.available()];
in.read(img);
return base64Encode(img);
}
public static synchronized String base64Encode(String in) throws IOException {
return base64Encode(in.getBytes());
}
public static synchronized String base64Encode(byte[] in) throws IOException {
return base64Encoder.encode(in);
}
}