2014-11-08 1154 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; 
} 
相關問題