調(diào)用java API發(fā)送傳真
環(huán)境要求:windows xp系統(tǒng),jdk 1.4(不要太高或太低,不然會(huì)發(fā)生與dll不匹配),運(yùn)行java程序的機(jī)器需要有貓及驅(qū)動(dòng)(一般的貓都會(huì)支持fax功能),jacob版本1.9(最好使用這個(gè)版本,其他版本會(huì)報(bào)錯(cuò)),xp本身能夠通過貓發(fā)送傳真(確認(rèn)一下環(huán)境可以發(fā)送傳真即可)
實(shí)現(xiàn)功能:java jni調(diào)用本地jacob.dll,jacob.dll中封裝好的接口調(diào)用本地服務(wù)(如excel、outlook、vbscript等)faxserver.faxserver.1,實(shí)現(xiàn)服務(wù)器端發(fā)送傳真。
步驟:
1 將jacob.dll文件拷貝到windows/system32下
2 創(chuàng)建java項(xiàng)目,將jacob.jar,jacob.dll放到項(xiàng)目lib路徑下,同時(shí)將他們也放到j(luò)dk的lib路徑下
3 編寫程序(可參考jacob官方的api文檔 http://www.5a520.cn ),如下:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
public class faxtest {
public void sendFax(String filename,Sring faxnumber) {
ActiveXComponent objFax = new ActiveXComponent("FaxServer.FaxServer.1");//這個(gè)名字一般要與注冊(cè)表里fax服務(wù)名匹配對(duì)了
Dispatch faxObject = objFax.getObject();
Dispatch.call(faxObject, "Connect", "");
Dispatch doc = Dispatch.call(faxObject, "CreateDocument", filename)
.toDispatch();
Dispatch.put(doc, "RecipientName", "someone");
Dispatch.put(doc, "FaxNumber", faxnumber); //注意電話號(hào)碼的格式
Dispatch.put(doc, "DisplayName", "zhupan");
Dispatch.call(doc, "Send");
Dispatch.call(faxObject, "DisConnect");
}
public static void main(String[] args) {
try {
faxtest faxDocumentProperties = new faxtest();
faxDocumentProperties.sendFax(" http://www.bt285.cn /WW.doc","028886666");
System.out.print("ok fax transfer successfully !");
} catch (Exception e) {
System.out.println(e);
}
}
}
4 調(diào)試,如果報(bào)錯(cuò)"no progid"異常,一般問題都是jdk與dll不匹配,或者傳真服務(wù)名稱(FaxServer.FaxServer.1)不匹配。
posted on 2009-03-10 16:27 wang9354 閱讀(2420) 評(píng)論(0) 編輯 收藏