Skip to content

Instantly share code, notes, and snippets.

@syshack
Created December 30, 2015 02:26
Show Gist options
  • Save syshack/ae0a3bcd8316ef0292e8 to your computer and use it in GitHub Desktop.
Save syshack/ae0a3bcd8316ef0292e8 to your computer and use it in GitHub Desktop.
package dcec.rdd;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.misc.BASE64Encoder;
public class TestCert extends HttpServlet {
/**
* Constructor of the object.
*/
public TestCert() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String type=request.getParameter("type");
//获得根证书的文件目录
String rootPath=this.getServletContext().getRealPath("/");
if(type.equals("root")){//表示安装根证书
rootPath+="cert\\dcec1D.cer";
}else if(type.equals("sign")){//表示安装数字签名证书
rootPath+="cert\\dcec1D.cer";
}
//读取跟证书
FileInputStream inUserCert=new FileInputStream(rootPath);//文件输入流,采用二进制的方式读取
int len=inUserCert.available();//获取证书总长度
byte[] userCert=new byte[len];//声明一个byte类型的数组,长度为证书的长度
inUserCert.read(userCert);//将证书读取到byte里
inUserCert.close();//关闭输入流
BASE64Encoder encoder=new BASE64Encoder();//声明一个编码器
String strCert=encoder.encode(userCert);//对证书进行编码
//对证书进行组装,使其可以为脚本安装所使用
strCert="-----BEGIN CERTIFICATE-----"+strCert;//添加证书标识的头部和尾部
strCert+="-----END CERTIFICATE-----";
//刚才读取的证书内容
response.setContentType("application/x-pkcs7-certificates");
response.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("苏州市数字城市工程研究中心有限公司","UTF-8"));
response.getOutputStream().write(userCert);
response.getOutputStream().flush();
response.getOutputStream().close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment