import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class TestCSS {
//待产生的CSS文件的名字
private final static String add = "http://192.168.12.53/font/1.html";
private final static String font = "Trangine";
private static String path = "D:\\css\\"; //输出的文档
private static String addM;
private static ComputeMD5 computeMd5;
public static void main(String[] args) throws IOException{
computeMd5 = new ComputeMD5();
addM = computeMd5.MD5Encode(add+"?="+font);
System.out.println(addM);
CreateCss(path + addM + ".css");
}
//构造对应的css文件,文件名为addM
/**
* pathname is the name of the css file, just append the suffix of "css" to it
* pathname = path + filename + ".css"
* @throws IOException
* */
public static void CreateCss(String pathname) throws IOException{
File filename = new File(pathname);
if(!filename.exists()){//judge whether the file is created successful or not
filename.createNewFile();
}
/**
* write content into the file
* */
try{
FileOutputStream fos = null;
fos = new FileOutputStream(filename);
PrintWriter pw = new PrintWriter(fos); //writer handler
System.out.println("running successfully!");
pw.write("@face-font {"+"\r\n"); //return and line feed
pw.append(" "+"font-family: " + "'" + font + "';" +"\r\n" ); //font-family: 'boyang'; control the font
pw.append(" src: url('" + path + font + ".eot " +"');" + "\r\n"); // the font for IE
pw.append(" src: local('" + font + ".ttf" + "'), url('" + path + font + ".ttf" + ".);" + "\r\n");
pw.append("}");
pw.flush(); // flush into the file
pw.close();
fos.close(); //close the file
}catch(IOException e){
throw e;
}
}
}
对应的产生的结果是这样的,代码中也有一个产生MD5值得函数,从(1)中可以直接引入进来。
结果如下:
项目中需要使用一些相关的技术,仅仅是记录下来为自己以后做参考。
(1)java计算网址和里面对应的关键字结合起来,对应的MD5加密的值,下面是代码
Java计算字符串MD5值:
import java.security.MessageDigest;
public class ComputeMD5 {
private final static String[] hexDigits = {
"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"};
/** *//**
* 转换字节数组为16进制字串
* @param b 字节数组
* @return 16进制字串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString=new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString=byteArrayToHexString(md.digest(resultString.getBytes()));
}
catch (Exception ex) {
}
return resultString;
}
}
引用的时候只需要使用如下方式:
computeMd5 = new ComputeMD5();
addM = computeMd5.MD5Encode("abcdefg");
这样就可以了,得到的addM就是对对应的字符串加密的MD5值了
(2)使用Java产生指定的css文件:
Java产生CSS文件代码:
@face-font {
font-family: 'Trangine';
src: url('D:\css\Trangine.eot ');
src: local('Trangine.ttf'), url('D:\css\Trangine.ttf.);
}
上面就是对应的函数及其产生的结果。 未完待续.....