我在我的应用中使用Google云端存储时出现问题,基本上我失败的地方是从此行检索GoogleCredential,这给了我一个NullPointerException =
GoogleCredential credential = new GoogleCredential.Builder()
我正在尝试获取Google应用程序默认凭据,但我没有使用Google Compute Engine,我更喜欢使用Google App Engine。
我正在使用这行代码来制作这个负责上传和下载文件的方法=
private static Storage getStorage() {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
try {
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(
new File(getProperties().getProperty(
PRIVATE_KEY_PATH_PROPERTY)))
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName(
getProperties().getProperty(APPLICATION_NAME_PROPERTY))
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
return storage;
}
在问题中添加更多内容,调试后我注意到给出我nullPointerException的行在这个方法中=
private static Properties getProperties() throws Exception {
if (properties == null) {
properties = new Properties();
InputStream stream = CloudStorage.class
.getResourceAsStream("/cloudstorage.properties");
try {
properties.load(stream);
} catch (IOException e) {
throw new RuntimeException(
"cloudstorage.properties must be present in classpath",
e);
} finally {
stream.close();
}
}
return properties;
}
特别是这行代码=
InputStream stream = CloudStorage.class
.getResourceAsStream("/cloudstorage.properties");