2014-11-08 1155 views
0

我使用java apns将通知推送到服务器上的ios设备,Java推送通知时需要一个.p12证书和密码。如何将p12文件转换为base64字符串?

ApnsService service = 
APNS.newService() 
.withCert("/path/to/certificate.p12", "MyCertPassword") 
.withSandboxDestination() 
.build(); 

我想将这种类型的.p12存储到我的数据库中,因为我的系统中有超过1个.p12文件。我们的服务器还允许第三方将他们的应用程序提交给我们的服务器。他们需要将他们的.p12文件提交给我们的服务器,因为他们想通过我们的服务器推送通知。我们不想将他们的.p12文件保存到我们服务器上的文件夹中,而是使用base64字符串保存数据库。

我在这里有一些问题: 我们该如何将.p12转换为base64字符串? 当我推送通知时,如何从base64字符串恢复.p12文件?
有没有更好的解决方案来获取和存储我的服务器端的.p2文件?

在此先感谢。

回答

0
private static String encodeFileToBase64Binary(String fileName) 
     throws IOException { 

    File file = new File(fileName); 
    byte[] bytes = loadFile(file); 
    byte[] encoded = Base64.encodeBase64(bytes); 
    String encodedString = new String(encoded); 

    return encodedString; 
} 
private static byte[] loadFile(File file) throws IOException { 
    InputStream is = new FileInputStream(file); 

    long length = file.length(); 
    if (length > Integer.MAX_VALUE) { 
     // File is too large 
    } 
    byte[] bytes = new byte[(int)length]; 

    int offset = 0; 
    int numRead = 0; 
    while (offset < bytes.length 
      && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
     offset += numRead; 
    } 

    if (offset < bytes.length) { 
     throw new IOException("Could not completely read file "+file.getName()); 
    } 

    is.close(); 
    return bytes; 
} 
相关问题