public class SalesConfirmationUploadThread extends Thread {
private boolean result = false;
private final SalesConfirmationPipedInputStream input;
private long orderId;
private String pin;
private JingdongStorageService jss;
private static final String CONTENTTYPE = "application/pdf";
private boolean sealFlag = false;
private final FileOutputStream fos;
private String pdfFile;
private static String PFXFILE;
private static String PFXPASS;
private static String SEALIMAGE;
private static int PAGENO;
private static int PAGEX;
private static int PAGEY;
private static final Logger log = LoggerFactory.getLogger(SalesConfirmationUploadThread.class);
//初始化电子印章类所用参数
static {
Properties properties = new Properties();
try {
URL sealPfx = SalesConfirmationUploadThread.class.getResource("/test.pfx");
URL sealGif = SalesConfirmationUploadThread.class.getResource("/sealjdpic.gif");
if(sealPfx!=null){
PFXFILE = sealPfx.getPath();
}
if(sealPfx!=null){
SEALIMAGE = sealGif.getPath();
}
properties.load(SalesConfirmationUploadThread.class.getResourceAsStream("/project.properties"));
String retPfxPass = properties.getProperty("pxf.pass");
String pageNo = properties.getProperty("pageno");
String pageX = properties.getProperty("pagex");
String pageY = properties.getProperty("pagey");
if (StringUtils.isNotBlank(retPfxPass)) {
PFXPASS = retPfxPass;
}
if(StringUtils.isNotBlank(pageNo)){
PAGENO = Integer.parseInt(pageNo);
}
if(StringUtils.isNotBlank(pageX)){
PAGEX = Integer.parseInt(pageX);
}
if(StringUtils.isNotBlank(pageY)){
PAGEY = Integer.parseInt(pageY);
}
} catch (Exception e) {
log.error("init seal param error is [{}] ",e);
} finally {
log.error("pfxPass={"+PFXPASS+"},sealPfx={"+PFXFILE+"},sealGif={"+SEALIMAGE+"},pageNo={"+PAGENO+"},pageX={"+PAGEX+"},pageY={"+PAGEY+"}");
}
}
public SalesConfirmationUploadThread(final SalesConfirmationPipedInputStream input,final FileOutputStream fos,final String pdfFile,long orderId, String pin, JingdongStorageService jss){
this.pdfFile =pdfFile;
this.fos = fos;
this.input = input;
this.orderId = orderId;
this.pin = pin;
this.jss = jss;
}
@Override
public void run() {
CallerInfo callerInfo = Profiler.registerInfo("com.jd.auctionplat.soa.threads.SalesConfirmationUploadThread", true, true);
if(input == null || input.getLength() <= 0){
log.error("根据订单号:" + orderId + "和用户pin:" + pin + "上传成交确认书时获取的管道输入流为空");
return;
}
log.error("-----开始上传根据订单号:" + orderId + "和用户pin:" + pin + "生成的成交确认书,长度为:" + input.getLength() + "----" );
if(jss == null){
log.error("----上传成交确认书时jss为空----");
}
//将成交确认书读入到服务器
File file = null;
try{
byte[] buffer = new byte[2*1024];
int len=0;
while((len=input.read(buffer))!=-1){
fos.write(buffer,0,len);
}
}catch(Exception e){
log.error("根据订单号:" + orderId + "和用户pin:" + pin + "上传成交确认书到服务器时出错", e);
}finally {
try{
if(null != fos){
fos.close();
}
}catch (IOException e){
log.error("关闭文件输出流时出错", e);
}
try {
if(null != input){
input.close();
}
} catch (IOException e) {
log.error("关闭管道输入流时出错", e);
}
}
//调用工具类加盖印章
try{
UTCSeal s = new UTCSeal();
sealFlag = s.DoSealWithPfx(pdfFile,PAGENO,PAGEX,PAGEY,PFXFILE,PFXPASS,SEALIMAGE);
}catch (Exception e){
log.error("调用电子签章失败!",e);
}
try {
//盖章成功,上传云存储
if(sealFlag){
file = new File(pdfFile);
jss.bucket(SalesConfirmationBucketUtils.getBucket()).
object(SalesConfirmationBucketUtils.getSalesConfirmationPath(orderId, pin)).
entity(file).contentType(CONTENTTYPE).put();
result = true;
String url = SalesConfirmationBucketUtils.getSalesConfirmationPath(orderId, pin);
log.error("生成成交确认书的url :" +url);
}
} catch (Exception e) {
log.error("根据订单号:" + orderId + "和用户pin:" + pin + "上传成交确认书到云存储时出错", e);
}finally{
try {
//删除服务器中成交确认书
if(null!=file && file.exists()){
file.delete();
}
} catch (Exception e) {
log.error("根据订单号:" + orderId + "和用户pin:" + pin + "上传成交确认书时关闭管道输入流出错", e);
}
}
log.error("-----成交确认书上传完成,订单号为:" + orderId + ",用户pin为:" + pin + "----" );
Profiler.registerInfoEnd(callerInfo);
}
public boolean isResult() {
return result;
}
}