我正在使用 spring boot 编写一个 azure 函数,我想读取一个文件形式的资源文件夹,但每次都得到空指针异常。请帮助我解决这个问题?要么我必须将文件放在 blob 存储中,要么在 azure 函数中有其他东西可以使用 java spring boot 应用程序读取文件?
下面是我的处理程序类:
公共类 FunctionHandler 扩展 AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("generateSignature")
public HttpResponseMessage execute(@HttpTrigger(name = "request", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException, SignatureException, IOException {
User user = request.getBody().filter((u -> u.getName() != null)).orElseGet(() -> new User(request
.getQueryParameters()
.getOrDefault("name", "<no name supplied> please provide a name as "
+ "either a query string parameter or in a POST body")));
context.getLogger().info("Greeting user name: " + user.getName());
try {
InputStream resourceInputStream = new FileInputStream(
FunctionHandler.class.getClassLoader().getResource("").getPath()
+ "../../src/main/java/com/tomtom/resources/private_key.der");
DataInputStream dis = new DataInputStream(resourceInputStream);
byte[] privateBytes = new byte[resourceInputStream.available()];
dis.readFully(privateBytes);
dis.close();
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privateBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec);
Signature s = Signature.getInstance("SHA256withRSA");
s.initSign(privateKey);
s.update(user.getName().getBytes("UTF-8"));
byte[] binarySignature = s.sign();
String signature = DatatypeConverter.printBase64Binary(binarySignature);
context.getLogger().info("sign is: " + signature);
} catch (IOException e) {
e.printStackTrace();
}
return request.createResponseBuilder(HttpStatus.OK).body(handleRequest(user, context))
.header("Content-Type", "application/json").build();
}
}