【问题标题】:Download link encodedKeyStore下载链接codedKeyStore
【发布时间】:2013-03-26 20:19:33
【问题描述】:

我已经通过 API 生成了一个 PKCS12 密钥库,但是进程的返回是一个 KeyStore 对象。我需要发送,在客户端发送申请时直接发送到浏览器下载。

我该怎么做?

我正在使用 java 和 jboss 5AS。

【问题讨论】:

    标签: jsf-2 download


    【解决方案1】:

    您可以使用KeyStore#store() 将其写入OutputStream

    keyStore.store(outputStream, password);
    

    基本上就是这样。 OutputStream 可能是 HTTP 响应之一。有关如何在 JSF 中提供文件下载的通用启动示例,其中您需要集成此行,请访问此答案:How to provide a file download from a JSF backing bean? 使用 content typeapplication/x-pkcs12

    【讨论】:

    • BalusC,非常感谢,我按照你的提示和链接,一切正常,我会将代码发布给人们也可以使用它!
    【解决方案2】:

    代码如下:

    public void cadastrar () throws Exception
    {       
        byte[] encodedKeyStore = controlador.cadastrar(certificadoModel);
    
        java.security.KeyStore keyStore = java.security.KeyStore.getInstance("PKCS12");
        keyStore.load(new ByteArrayInputStream(encodedKeyStore), certificadoModel.getPassword().toCharArray());
    
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
    
        ec.responseReset(); 
        ec.setResponseContentType("application/x-pkcs12"); 
        //ec.setResponseContentLength(contentLength); 
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + certificadoModel.getUsername() + ".p12" + "\""); 
    
        OutputStream output = ec.getResponseOutputStream();
        keyStore.store(output, certificadoModel.getPassword().toCharArray());
    
        fc.responseComplete(); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 2012-05-24
      • 2011-01-06
      相关资源
      最近更新 更多