大家好,又见面了,我是你们的朋友全栈君。
推荐公众号
有彩蛋哦!!!(或者公众号内点击网赚获取彩蛋)
服务商
在网上兜兜转转没有找到免费的网络传真API,只好付费服务商bnetfax
代码
所需jar
链接:https://pan.baidu.com/s/11EYZ7_9nOY2eYt8urXNNsw
提取码:61un
发送代码
public static void main(String[] args) throws IOException, ServiceException {
收件人号码
String destnumbers = "********";
发送主题
String title = "代码测试发送传真";
FaxConfig faxConfig = FaxConfig.builder()
企业账号
.bnetaccount("********")
密码
.password("********")
发送者号码
.sendnumber("********")
用户账号
.useraccount("********")
区号
.areaid("********")
.build();
List<String> paths = Lists.newArrayList("test.pdf");
FaxResultDTO faxResultDTO = sendFax(faxConfig, title, destnumbers, paths);
log.info("faxResultDTO=[{}]", faxResultDTO);
}
public static FaxResultDTO sendFax(FaxConfig faxConfig, String title, String destnumbers, List<String> filePath) throws IOException, ServiceException {
Map<String, byte[]> fileBuffer = getFileBuffer(filePath);
//生成访问Soap传真服务接口的UserTest实例
SoapFaxClient soapFaxClient = new SoapFaxClient();
//通过UserTest.upload()把字节大小和文件名传过去以访问Soap传真服务接口
String xml = soapFaxClient.doSendFax(fileBuffer,
faxConfig.getSendnumber(), title, faxConfig.getUseraccount(), faxConfig.getBnetaccount(),
faxConfig.getPassword(), destnumbers, faxConfig.getAreaid());
//将发送与返回的xml包分开
String[] xmls = xml.split("@@");
// System.out.println("保存服务端返回的xml到request域中" + xmls[0]);
// System.out.println("保存客户端发送的xml到request域中" + xmls[1]);
//xml包解析
FaxResultDTO faxResultDTO = parseFaxResultXml(xmls[0]);
String resultCode = faxResultDTO.getResultCode();
String faxid = faxResultDTO.getFaxid();
String resultdesc = faxResultDTO.getResultdesc();
if (resultCode != null && (!"".equals(resultCode)) && (resultCode.equals("1000"))) {
log.info("faxid=[{}], success", faxid);
} else {
log.info("resultCode:[{}],resultdesc=[{}]", resultCode, resultdesc);
}
faxResultDTO.setRequestXml(xmls[1]);
faxResultDTO.setResponseXml(xmls[0]);
return faxResultDTO;
}
private static FaxResultDTO parseFaxResultXml(String xml) {
ServletClass xmlParser = new ServletClass();
String resultCode = null;
String faxid = null;
String hashcode = null;
String timestamp = null;
String resultdesc = null;
FaxResultDTO faxResultDTO = null;
try {
xmlParser.loadXml(xml);
resultCode = xmlParser.getSingleNodeValue("resultcode");
faxid = xmlParser.getSingleNodeValue("faxid");
hashcode = xmlParser.getSingleNodeValue("hashcode");
timestamp = xmlParser.getSingleNodeValue("timestamp");
resultdesc = xmlParser.getSingleNodeValue("resultdesc");
faxResultDTO = new FaxResultDTO(resultCode, faxid, hashcode, timestamp, resultdesc);
} catch (Exception e) {
log.error("解析fax-result-xml失败", e);
e.printStackTrace();
}
return faxResultDTO;
}
/** * 返回 key=文件名 value=文件byte数组 * @param path * @return * @throws IOException */
private static Map<String, byte[]> getFileBuffer(List<String> path) throws IOException {
Map<String, byte[]> map = Maps.newHashMap();
path.forEach(p -> {
File file1 = new File(p);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file1);
String filename = file1.getName();
//获取文件大小
int filelength = Integer.valueOf(String.valueOf(file1.length()));
//字节大小
byte[] buffer = new byte[filelength];
InputStream streamIn1 = fileInputStream;
streamIn1.read(buffer);
streamIn1.close();
map.put(filename, buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
return map;
}
客户端请求发送传真件接口
public String doSendFax(Map<String, byte[]> fileBuffer,
String sendnumber, String title, String useraccount, String bnetaccount, String password,
String destnumbers, String areaid) throws ServiceException, RemoteException {
设置请求访问Soap网络传真接口的url
String endPoint = "*****************";
String xml = null;
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endPoint);
设置请求访问的url及接口名称
call.setOperationName(new QName(endPoint, "SendFax"));
获取附件
Map<String, Object> fileNamesAndObjects = getFileNamesAndObjects(fileBuffer);
String[] filename = (String[]) fileNamesAndObjects.get("name");
Object[] objects = (Object[]) fileNamesAndObjects.get("object");
MD5 md5 = new MD5();
MD5加密password
password = MDES.encrypt(password);
final String KEY = "*******";
获取系统时间戳
long timestamp = System.currentTimeMillis();
生成哈希码 Md5(useraccount + timestamp + KEY)
StringBuilder inbuf = new StringBuilder(useraccount)
.append(bnetaccount)
.append(timestamp)
.append(KEY);
String hashcode = md5.getMD5ofStr(inbuf.toString());
StringBuffer xmlstring = new StringBuffer(
"<?xml version=\"1.0\" encoding=\"GB2312\" ?>")
.append("\n<root>")
.append("\n<useraccount>" + useraccount + "</useraccount>")
.append("\n<bnetaccount>" + bnetaccount + "</bnetaccount>")
.append("\n<areaid>" + areaid + "</areaid>")
.append("\n<password>" + password + "</password>")
.append("\n<destnumbers>" + destnumbers + "</destnumbers>")
.append("\n<sender>" + sendnumber + "</sender>")
.append("\n<title>" + title + "</title>")
// xmlstring.append("\n<piclevel>" + piclevel + "</piclevel>");
.append("\n<timestamp>" + timestamp + "</timestamp>")
.append("\n<hashcode>" + hashcode + "</hashcode>")
.append("\n<fifle>" + filename[0] + "," + filename[1] + ","
+ filename[2] + "," + filename[3] + "," + filename[4] + "," + filename[5]
+ "," + filename[6] + "," + filename[7] + "," + filename[8] + ","
+ filename[9] + "</fifle>")
.append("\n<file>" + 1 + "</file>")
.append("\n</root>");
log.info("FAX-发送xmL:[{}]", xmlstring.toString());
客户端调用Soap传真服务发送传真件接口
objects[0] = xmlstring.toString();
xml = (String) call.invoke(objects);
xml += "@@" + xmlstring.toString();
return xml;
}
private static Map<String, Object> getFileNamesAndObjects(Map<String, byte[]> fileBuffer) {
Map<String, Object> map = new HashMap<>();
String[] filename = new String[]{
"", "", "", "", "", "", "", "", "", ""};
Object[] objects = new Object[]{
"", new byte[0], new byte[0], new byte[0], new byte[0], new byte[0], new byte[0], new byte[0], new byte[0], new byte[0], new byte[0]};
int i = 0;
for (Map.Entry<String, byte[]> entry : fileBuffer.entrySet()) {
String key = entry.getKey();
byte[] value = entry.getValue();
filename[i] = key;
objects[i + 1] = value;
++i;
}
map.put("name", filename);
map.put("object", objects);
return map;
}
返回成功报文
<root>
<faxid>***********</faxid>
<resultcode>1000</resultcode>
<resultdesc>传输正常</resultdesc>
<timestamp>************</timestamp>
<hashcode>**********</hashcode>
</root>
回执接口
客户端请求查询传真回执接口
public String queryFaxResult(String faxId) {
设置请求访问Soap网络传真接口的url
String endPoint = "*************";
Call call = null;
String xml = null;
try {
Service service = new Service();
call = (Call) service.createCall();
call.setTargetEndpointAddress(endPoint);
设置请求访问的url及接口名称
call.setOperationName(new QName(endPoint, "queryFaxResult"));
MD5 md5 = new MD5();
final String KEY = "**********";
获取系统时间戳
long timestamp = System.currentTimeMillis();
生成哈希码 Md5(faxid + timestamp + KEY)
String hashcode = md5.getMD5ofStr(faxId + timestamp + KEY);
StringBuffer xmlstring = new StringBuffer(
"<?xml version=\"1.0\" encoding=\"GB2312\" ?>");
xmlstring.append("\n<root>");
xmlstring.append("\n<faxid>" + faxId + "</faxid>");
xmlstring.append("\n<timestamp>" + timestamp + "</timestamp>");
xmlstring.append("\n<hashcode>" + hashcode + "</hashcode>");
xmlstring.append("\n</root>");
//System.out.println("\n发送的XML:\n"+xmlstring.toString());
//客户端调用Soap传真服务查询传真回执接口
xml = (String) call.invoke(new Object[]{
xmlstring.toString()});
//为了同时返回发送和返回的xml包,将两个包用“@@”连接
xml += "@@" + xmlstring.toString();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("打印错误:\n" + ex.toString());
}
return xml;
}
回执接口返回报文
<root>
<xmlresult>
<flag>N</flag>
<desc></desc>
<times></times>
<caller></caller>
</xmlresult>
<resultcode>1000</resultcode>
<resultdesc>结果:总共需发送号码1个,已发送0个,未发送1个</resultdesc>
<hashcode>**************</hashcode>
<timestamp>*************</timestamp>
</root>@@<?xml version="1.0" encoding="GB2312" ?>
<root>
<faxid>********************</faxid>
<timestamp>*********</timestamp>
<hashcode>*************</hashcode>
</root>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/153403.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...