jSsignature 数字签名,保存图片

这篇博客介绍了如何使用Java进行SVG到PNG的转换,并详细讲解了在JavaScript中实现数字签名的过程,同时提供了相关的jsp页面示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取页面传递字符

String data = req.getParameter("data");
String imageData=data.substring(data.indexOf(",")+2, data.length()-2);
//System.out.println(imageData.indexOf("\\"));
//String s="\\";
//imageData=imageData.replaceAll(s, " ");
imageData=imageData.replace('\\', ' ');
imageData=imageData.replaceAll(" \"", "\"");
//保存特定的目录下面 读取配置文件的路径
StringBuffer realPathDir = new StringBuffer();
realPathDir.append(PropertiesUtil.getProperty("SaveFilePath")+Constant.PACT.CAND_SIGNATURE);
String path = realPathDir.toString();
String filename=fix+candId+".png";
String imgPath = SvgPngConverter.convertToPng(imageData,path,path+"/"+filename);



SvgPngConverter.java

package com.qk365.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qk365.join.comp.action.CanditorMobileController;

/**
* 将svg转换为png格式的图片
*
*
*/
public abstract class SvgPngConverter {
private static Logger logger = LoggerFactory.getLogger(SvgPngConverter.class);
/**
* 将svg字符串转换为png
*
* @param svgCode svg代码
* @param pngFilePath 保存的路径
* @throws TranscoderException svg代码异常
* @throws IOException io错误
*/
public static String convertToPng(String svgCode, String filePah,String pngFilePath) throws IOException,
TranscoderException {

File fileFloder = new File(filePah);
if(!fileFloder.exists()){
fileFloder.mkdir();
}

//File file = new File(pngFilePath);
FileOutputStream outputStream = null;
try {
// file.createNewFile();
outputStream = new FileOutputStream(pngFilePath);
convertToPng(svgCode, outputStream);
return pngFilePath;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
logger.error("outputStream.close e:",e);
return null;
}
}
}
}

/**
* 将svgCode转换成png文件,直接输出到流中
*
* @param svgCode svg代码
* @param outputStream 输出流
* @throws TranscoderException 异常
* @throws IOException io异常
*/
public static void convertToPng(String svgCode, OutputStream outputStream)
throws TranscoderException, IOException {
try {
byte[] bytes = svgCode.getBytes("utf-8");
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
TranscoderOutput output = new TranscoderOutput(outputStream);
t.transcode(input, output);
outputStream.flush();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
// sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"><svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"401\" height=\"36\"><path fill=\"none\" stroke=\"#000000\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M 1 1 c 2.12 0 80.27 -1.58 121 0 c 20 0.78 38.73 3.98 59 7 c 37.69 5.61 71.69 12.77 109 18 c 16.27 2.28 31.51 2.25 48 4 c 12.78 1.35 24.53 3.99 37 5 c 8.35 0.67 25 0 25 0\"/></svg>");
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"><svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"456\" height=\"132\"><path fill=\"none\" stroke=\"#000000\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M 2 44 c -0.02 0.19 -1.72 7.99 -1 11 c 0.76 3.17 3.72 7.15 6 10 c 1.53 1.91 3.86 3.53 6 5 c 3.2 2.2 6.51 4.45 10 6 c 5.4 2.4 11.31 3.69 17 6 c 6.92 2.81 13.08 6.44 20 9 c 8.58 3.17 17.02 5.98 26 8 c 27.27 6.12 53.42 11.36 81 16 c 10.73 1.8 21.12 1.57 32 3 c 9.96 1.31 18.94 3.55 29 5 c 21.04 3.04 40.39 7 61 8 c 34.85 1.69 69.52 0.59 104 0 c 4.03 -0.07 7.97 -1.12 12 -2 c 3.77 -0.82 7.42 -1.75 11 -3 c 3.1 -1.08 6.13 -2.39 9 -4 c 5.5 -3.08 11.74 -6.06 16 -10 c 4.2 -3.89 7.74 -9.98 11 -15 c 0.94 -1.45 1.43 -3.29 2 -5 c 0.44 -1.31 1 -2.75 1 -4 c 0 -1.85 -0.12 -4.39 -1 -6 c -2.8 -5.14 -6.49 -11.97 -11 -16 c -10.32 -9.23 -23.9 -16.86 -36 -26 c -4.64 -3.5 -8.36 -7.61 -13 -11 c -7.86 -5.73 -15.83 -11.05 -24 -16 c -2.79 -1.69 -6.3 -2.38 -9 -4 c -2.14 -1.28 -3.91 -3.88 -6 -5 c -1.95 -1.05 -4.59 -1.4 -7 -2 c -1.69 -0.42 -3.44 -1 -5 -1 c -1.56 0 -3.66 0.19 -5 1 c -10.4 6.29 -22.47 14.1 -33 22 c -5.45 4.09 -9.76 10.12 -15 14 l -12 6\"/></svg>");
String m="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"><svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"456\" height=\"132\"><path fill=\"none\" stroke=\"#000000\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M 2 44 c -0.02 0.19 -1.72 7.99 -1 11 c 0.76 3.17 3.72 7.15 6 10 c 1.53 1.91 3.86 3.53 6 5 c 3.2 2.2 6.51 4.45 10 6 c 5.4 2.4 11.31 3.69 17 6 c 6.92 2.81 13.08 6.44 20 9 c 8.58 3.17 17.02 5.98 26 8 c 27.27 6.12 53.42 11.36 81 16 c 10.73 1.8 21.12 1.57 32 3 c 9.96 1.31 18.94 3.55 29 5 c 21.04 3.04 40.39 7 61 8 c 34.85 1.69 69.52 0.59 104 0 c 4.03 -0.07 7.97 -1.12 12 -2 c 3.77 -0.82 7.42 -1.75 11 -3 c 3.1 -1.08 6.13 -2.39 9 -4 c 5.5 -3.08 11.74 -6.06 16 -10 c 4.2 -3.89 7.74 -9.98 11 -15 c 0.94 -1.45 1.43 -3.29 2 -5 c 0.44 -1.31 1 -2.75 1 -4 c 0 -1.85 -0.12 -4.39 -1 -6 c -2.8 -5.14 -6.49 -11.97 -11 -16 c -10.32 -9.23 -23.9 -16.86 -36 -26 c -4.64 -3.5 -8.36 -7.61 -13 -11 c -7.86 -5.73 -15.83 -11.05 -24 -16 c -2.79 -1.69 -6.3 -2.38 -9 -4 c -2.14 -1.28 -3.91 -3.88 -6 -5 c -1.95 -1.05 -4.59 -1.4 -7 -2 c -1.69 -0.42 -3.44 -1 -5 -1 c -1.56 0 -3.66 0.19 -5 1 c -10.4 6.29 -22.47 14.1 -33 22 c -5.45 4.09 -9.76 10.12 -15 14 l -12 6\"/></svg>";
//<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="456" height="132"><path fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M 2 44 c -0.02 0.19 -1.72 7.99 -1 11 c 0.76 3.17 3.72 7.15 6 10 c 1.53 1.91 3.86 3.53 6 5 c 3.2 2.2 6.51 4.45 10 6 c 5.4 2.4 11.31 3.69 17 6 c 6.92 2.81 13.08 6.44 20 9 c 8.58 3.17 17.02 5.98 26 8 c 27.27 6.12 53.42 11.36 81 16 c 10.73 1.8 21.12 1.57 32 3 c 9.96 1.31 18.94 3.55 29 5 c 21.04 3.04 40.39 7 61 8 c 34.85 1.69 69.52 0.59 104 0 c 4.03 -0.07 7.97 -1.12 12 -2 c 3.77 -0.82 7.42 -1.75 11 -3 c 3.1 -1.08 6.13 -2.39 9 -4 c 5.5 -3.08 11.74 -6.06 16 -10 c 4.2 -3.89 7.74 -9.98 11 -15 c 0.94 -1.45 1.43 -3.29 2 -5 c 0.44 -1.31 1 -2.75 1 -4 c 0 -1.85 -0.12 -4.39 -1 -6 c -2.8 -5.14 -6.49 -11.97 -11 -16 c -10.32 -9.23 -23.9 -16.86 -36 -26 c -4.64 -3.5 -8.36 -7.61 -13 -11 c -7.86 -5.73 -15.83 -11.05 -24 -16 c -2.79 -1.69 -6.3 -2.38 -9 -4 c -2.14 -1.28 -3.91 -3.88 -6 -5 c -1.95 -1.05 -4.59 -1.4 -7 -2 c -1.69 -0.42 -3.44 -1 -5 -1 c -1.56 0 -3.66 0.19 -5 1 c -10.4 6.29 -22.47 14.1 -33 22 c -5.45 4.09 -9.76 10.12 -15 14 l -12 6"/></svg>
//<?xml version= "1.0 " encoding= "UTF-8 " standalone= "no "?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN " "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd "><svg xmlns= "http://www.w3.org/2000/svg " version= "1.1 " width= "452 " height= "68 "><path fill= "none " stroke= "#000000 " stroke-width= "2 " stroke-linecap= "round " stroke-linejoin= "round " d= "M 1 1 c 0.39 0.3 14.23 11.67 22 17 c 10.55 7.23 20.85 14.18 32 20 c 11.91 6.21 24.32 10.98 37 16 c 5.27 2.09 10.45 3.52 16 5 c 9.88 2.63 19.3 5.35 29 7 c 5.86 1 11.8 0.92 18 1 c 20.97 0.27 40.16 0.47 61 0 c 9.25 -0.21 17.89 -0.86 27 -2 c 9.91 -1.24 19.3 -2.79 29 -5 c 9.6 -2.19 18.99 -4.56 28 -8 c 9.27 -3.54 17.58 -8.96 27 -13 c 14.53 -6.23 32.65 -16.83 43 -17 c 5.23 -0.09 11.29 11.28 17 16 c 1.64 1.36 3.98 2.27 6 3 c 1.54 0.56 3.33 0.93 5 1 c 6.55 0.26 13.96 1.31 20 0 c 8.41 -1.83 17.84 -6.29 26 -10 l 7 -5 "/></svg>
/*try {
convertToPng(sb.toString(), "e:/hr.png");
} catch (Exception e) {
e.printStackTrace();
}*/
String s="=\\\"=";
System.out.println(s.indexOf("/"));
s=s.replaceAll("\\", "*");
System.out.println(s);
}
}



jsp页面demo见附件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值