LocalSignSample.java
2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.project.demo;
import com.junziqian.api.bean.ResultInfo;
import com.junziqian.api.request.DownCertRequest;
import com.junziqian.api.response.DownCertResponse;
import com.junziqian.api.util.LocalSignUtils;
import com.junziqian.api.util.StampUtil;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.File;
/**
* Created by wenbin on 5/22/2017
*/
public class LocalSignSample extends JunziqianClientInit {
public static void main(String[] args) throws Exception {
DownCertRequest request = new DownCertRequest();//初始请求对象
// request.setIdentityCard("500105198309201815");//企业
// request.setUserType(0);
// request.setName("文林果");
request.setSignImg("D:\\hand.png");
request.setIdentityCard("513401197806110632");//个人
request.setUserType(0);
request.setName("文斌");
String pageChapteJson = "[{\"chaptes\":[{\"offsetX\":0.31,\"offsetY\":0.72}],\"page\":0}," +
"{\"chaptes\":[{\"offsetX\":0.72,\"offsetY\":0.72}],\"page\":1}]";
request.setPageChapteJson(pageChapteJson);//设置签字位置
//下载证书
DownCertResponse response = getClient().down(request);
if(!response.isSuccess()){
System.out.println("申请证书失败");
return;
}
byte[] cert = response.getCert();
if(cert==null){
System.out.println("申请的证书为空");
return;
}
//打开本地文件
byte[] bytesPdf = StampUtil.getBytes(new File("D:\\abc.pdf"));
//本地签
ResultInfo<byte[]> result = LocalSignUtils.doSign(request.getIdentityCard(),
request.getUserType(),request.getName(),request.getPageChapteJson(),bytesPdf,cert,
request.getSignImg(),response.getCaPwd());
if(result.isSuccess()){
StampUtil.betyToFile("D:\\localSigned.pdf",result.getData());
String hash = DigestUtils.sha512Hex(result.getData());//得到hash
System.out.println("本地签署成功,签完文件hash:" + hash);
}else{
System.out.println("本地签署失败:" + result.getMsg());
System.out.println("失败详情:" + result.getException().toString());
}
}
}