Re: [請益] 部署到server後找不到檔案

看板Soft_Job作者 (有些事,有時候。。。)時間8年前 (2017/09/04 18:36), 8年前編輯推噓11(1215)
留言18則, 14人參與, 最新討論串2/2 (看更多)
※ 引述《qazsd (珍惜當下)》之銘言: : 藉助一下軟體版廣大的人氣,請問板上前輩們 : 小弟有Maven專案資料夾結構如下: : Project : ├src/main/java : │ └ com.abc.xyz : │ └Auth.java : └src/main/resources : └certs : └credential.p12 : Auth.java裡有段程式碼用來驗證: : ===================================================================== : File file = new File(classLoader.getResource("cert.p12").getFile()); : String filePath = file.getAbsolutePath(); : GoogleCredential credential = new GoogleCredential.Builder() : .setTransport(httpTransport) : .setJsonFactory(JSON_FACTORY) : .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) : .setServiceAccountScopes(Collections.singleton( : CalendarScopes.CALENDAR_READONLY)) : .setServiceAccountPrivateKeyFromP12File(new File(filePath)) : .setServiceAccountUser(SERVICE_ACCOUNT_USER) : .build(); : ===================================================================== : 將該專案檔的jar部署到Server上後 : 會出現找不到credential檔案的錯誤訊息 : 想請問credential檔案應該放在專案或server哪裡才能被找到? : 又該怎麼指定路徑呢? 你無法直接存取包在 zip 內的檔案,File 物件不是這樣給你用的。 有 2 個選擇,1 個是把它解開來,存成檔案,另一個是轉成 private key, 再呼叫另 method http://bit.ly/2wxy7qH public GoogleCredential.Builder setServiceAccountPrivateKey(PrivateKey serviceAccountPrivateKey) ----------------------------------------------------------------------------- 那麼,可以寫成這樣的 method,直接由 classloader 把 resource 抓出來, 轉成 PrivateKey 給它吃: public static PrivateKey loadKeyFromPkcs12() throws Exception { char[] password = "notasecret".toCharArray(); try (InputStream input = Auth.class.getResourceAsStream( "/certs/credential.p12")) { KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(input, password); return (PrivateKey) ks.getKey("privatekey", password); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.21.157.223 ※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1504521383.A.702.html ※ 編輯: qrtt1 (211.21.157.223), 09/04/2017 18:36:52

09/04 19:00, , 1F
有神快拜!
09/04 19:00, 1F

09/04 20:15, , 2F
這不過是reflection應用,還神咧笑死狼,一堆菜逼巴
09/04 20:15, 2F

09/04 20:19, , 3F
佛心還不神嗎?
09/04 20:19, 3F

09/04 21:04, , 4F
先感謝qrtt1大大!!
09/04 21:04, 4F

09/04 21:16, , 5F
佛心推
09/04 21:16, 5F

09/04 22:30, , 6F
總比只會酸的強^^
09/04 22:30, 6F

09/04 22:30, , 7F
09/04 22:30, 7F

09/04 22:31, , 8F
超神
09/04 22:31, 8F

09/04 22:33, , 9F
本板會分享會幫助別人的 就是神一般的存在
09/04 22:33, 9F

09/04 22:34, , 10F
神永遠不嫌多 酸酸一個都嫌太多
09/04 22:34, 10F

09/04 22:35, , 11F
真的QQ 每次有位JSON大大發教學文 都會被特定酸給酸
09/04 22:35, 11F

09/04 23:46, , 12F
推2樓
09/04 23:46, 12F

09/05 00:07, , 13F
把主管捧成神比較實際,很多人當面卻開不了口
09/05 00:07, 13F

09/05 09:29, , 14F
推有心
09/05 09:29, 14F

09/05 10:29, , 15F
haha → femlro: 神永遠不嫌多 酸酸一個都嫌太多
09/05 10:29, 15F

09/06 07:24, , 16F
分享是好事 不需要酸
09/06 07:24, 16F

09/06 19:38, , 17F
推一個
09/06 19:38, 17F

09/07 08:18, , 18F
09/07 08:18, 18F
文章代碼(AID): #1PhIodS2 (Soft_Job)
文章代碼(AID): #1PhIodS2 (Soft_Job)