package com.sf.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sf.dto.CargoInfoDto;
import com.sf.dto.JsonRlsInfoDto;
import com.sf.dto.JsonWaybillDto;
import com.sf.dto.RlsInfoDto;
import com.sf.dto.WaybillDto;
import com.sf.util.Base64ImageTools;
import com.sf.util.MyJsonUtil;
import com.sf.util.XmlToJavaBeanUtil;
public class TestCallWaybillPrinterJsonToJava {
public static void main(String[] args) throws Exception {
String reqPathname = "D:\\JSONorder.txt";// json文件所在绝对路径
String jsonReq = "";// json字符串
TestCallWaybillPrinterJsonToJava.WayBillPrinterTools(reqPathname, jsonReq);
}
public static void WayBillPrinterTools(String reqPathname, String jsonReq) throws Exception {
/*********2联150 丰密运单**************/
/**
* 调用打印机 不弹出窗口 适用于批量打印【二联单】
*/
String url1 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm150mm&output=noAlertPrint";
/**
* 调用打印机 弹出窗口 可选择份数 适用于单张打印【二联单】
*/
String url2 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm150mm&output=print";
/**
* 直接输出图片的BASE64编码字符串 可以使用html标签直接转换成图片【二联单】
*/
String url3 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm150mm&output=image";
/*********3联210 丰密运单**************/
/**
* 调用打印机 不弹出窗口 适用于批量打印【三联单】
*/
String url4 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm210mm&output=noAlertPrint";
/**
* 调用打印机 弹出窗口 可选择份数 适用于单张打印【三联单】
*/
String url5 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm210mm&output=print";
/**
* 直接输出图片的BASE64编码字符串 可以使用html标签直接转换成图片【三联单】
*/
String url6 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm210mm&output=image";
/*********2联180 丰密运单**************/
/**
* 调用打印机 不弹出窗口 适用于批量打印【二联单】
*/
String url7 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm180mm&output=noAlertPrint";
/**
* 调用打印机 弹出窗口 可选择份数 适用于单张打印【二联单】
*/
String url8 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm180mm&output=print";
/**
* 直接输出图片的BASE64编码字符串 可以使用html标签直接转换成图片【二联单】
*/
String url9 = "http://localhost:4040/sf/waybill/print?type=FM_poster_100mm180mm&output=image";
//根据业务需求确定请求地址
String reqURL=url3;
// 电子面单顶部是否需要logo
boolean topLogo = true;// true 需要logo false 不需要logo
if (reqURL.contains("V2.0") && topLogo) {
reqURL = reqURL.replace("V2.0", "V2.1");
}
if (reqURL.contains("V3.0") && topLogo) {
reqURL = reqURL.replace("V3.0", "V3.1");
}
System.out.println(reqURL);
/** 注意 需要使用对应业务场景的url **/
URL myURL = new URL(reqURL);
// 其中127.0.0.1:4040为打印服务部署的地址(端口如未指定,默认为4040),
// type为模板类型(支持两联、三联,尺寸为100mm*150mm和100mm*210mm,type为poster_100mm150mm和poster_100mm210mm)
// A5 poster_100mm150mm A5 poster_100mm210mm
// output为输出类型,值为print或image,如不传,
// 默认为print(print 表示直接打印,image表示获取图片的BASE64编码字符串)
// V2.0/V3.0模板顶部是带logo的 V2.1/V3.1顶部不带logo
HttpURLConnection httpConn = (HttpURLConnection) myURL.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
httpConn.setRequestMethod("POST");
// httpConn.setRequestProperty("Content-Type",
// "application/json;charset=utf-8");
httpConn.setRequestProperty("Content-Type", "text/plain;charset=utf-8");
httpConn.setConnectTimeout(5000);
httpConn.setReadTimeout(3 * 5000);
// 获取xml转java请求报文对象
String jsonStr = null;
if (StringUtils.isNotBlank(reqPathname)) {
jsonStr = XmlToJavaBeanUtil.readFile(reqPathname);
} else if (StringUtils.isNotBlank(jsonReq)) {
jsonStr = jsonReq;
}
if (StringUtils.isBlank(jsonStr)) {
return;
}
ObjectMapper mapper = new ObjectMapper();
JsonWaybillDto waybillDto = mapper.readValue(jsonStr, JsonWaybillDto.class);
JsonNode node = mapper.readTree(jsonStr);
JsonNode node1= node.get("rlsInfoDtoList");
String node1Str=node1.toString();
// String name=node.get("twoDimensionCode").toString();
List<JsonRlsInfoDto> jsonRlsInfoList = mapper.readValue(node1Str,new TypeReference<List<JsonRlsInfoDto>>() { });
List<RlsInfoDto> rlsInfoList = new ArrayList<>();
for (JsonRlsInfoDto jsonRlsInfoDto : jsonRlsInfoList) {
RlsInfoDto rlsInfoDto=new RlsInfoDto();
rlsInfoDto.setAbFlag(jsonRlsInfoDto.getAbFlag());
rlsInfoDto.setCodingMapping(jsonRlsInfoDto.getCodingMapping());
rlsInfoDto.setCodingMappingOut(jsonRlsInfoDto.getCodingMappingOut());
rlsInfoDto.setDestRouteLabel(jsonRlsInfoDto.getDestRouteLabel());
rlsInfoDto.setDestTeamCode(jsonRlsInfoDto.getDestTeamCode());
rlsInfoDto.setPrintIcon(jsonRlsInfoDto.getPrintIcon());
rlsInfoDto.setProCode(jsonRlsInfoDto.getProCode());
rlsInfoDto.setProName(jsonRlsInfoDto.getProName());
rlsInfoDto.setQRCode(jsonRlsInfoDto.getTwoDimensionCode());
rlsInfoDto.setSourceTransferCode(jsonRlsInfoDto.getSourceTransferCode());
rlsInfoDto.setWaybillNo(jsonRlsInfoDto.getWaybillNo());
rlsInfoDto.setXbFlag(jsonRlsInfoDto.getXbFlag());
rlsInfoDto.setSourceTransferCode(jsonRlsInfoDto.getSourceTransferCode());
rlsInfoList.add(rlsInfoDto);
}
// 获取cargo对象
List<CargoInfoDto> cargoList = waybillDto.getCargoInfoDtoList();
//List<JsonRlsInfoDto> rlsInfoList = waybillDto.getRlsInfoDtoList();
List<WaybillDto> waybillDtoList = new ArrayList<WaybillDto>();
WaybillDto dto = new WaybillDto();
//电子面单顶部是否需要logo
dto.setIsPrintLogo("1");//1 需要logo 0 不需要logo
// 这个必填
dto.setAppId(waybillDto.getAppId());// 对应clientCode
dto.setAppKey(waybillDto.getAppKey());// 对应checkWord
String mailno = waybillDto.getMailNo();// 主运单号
if (StringUtils.isNotBlank(mailno) && mailno.split(",").length > 1) {
String[] mailNos = mailno.split(",");
StringBuilder sb = new StringBuilder();
for (String s : mailNos) {
sb.append(s);
sb.append(",");
}
int num = sb.lastIndexOf(",");
String newStr = sb.substring(0, num);
dto.setMailNo(newStr);// 子母单方式
} else if (StringUtils.isNotBlank(mailno)) {
dto.setMailNo(mailno);
}
// 签回单号 签单返回服务 会打印两份快单 其中第二份作为返寄的单
dto.setReturnTrackingNo(waybillDto.getReturnTrackingNo());
// 收件人信息
dto.setConsignerProvince(waybillDto.getConsignerProvince());
dto.setConsignerCity(waybillDto.getConsignerCity());
dto.setConsignerCounty(waybillDto.getConsignerCounty());
dto.setConsignerAddress(waybillDto.getConsignerAddress()); // 详细地址建议最多30个字
// 字段过长影响打印效果
dto.
顺丰丰桥面单打印.rar
5星 · 超过95%的资源 需积分: 50 194 浏览量
2020-09-16
09:34:56
上传
评论
收藏 14KB RAR 举报
不停留的风
- 粉丝: 176
- 资源: 5