gpt4 book ai didi

java - Azure函数计算md5校验和

转载 作者:行者123 更新时间:2023-12-02 08:18:32 24 4
gpt4 key购买 nike

我正在尝试使用基于 Java 的 Azure 函数计算通过 POST 请求提交的文件的 md5 校验和。但是,当我将文件作为字符串作为请求的一部分提交时,它包含一些不断变化的 header 信息(不确定为什么),因此 md5 校验和随着每个请求不断变化。

public class Function {
/**
* This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash:
* 1. curl -d "HTTP Body" {your host}/api/HttpExample
* 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
* @throws IOException
*/
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) throws IOException {
context.getLogger().info("Java HTTP trigger processed a request.");

// Parse query parameter
final String content = request.getBody().get();
context.getLogger().info(content);

byte[] md5Bytes = DigestUtils.md5(content);
String md5 = Base64.getEncoder().encodeToString(md5Bytes);


if (md5 == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name in the request body").build();
} else {
return request.createResponseBuilder(HttpStatus.OK).body(md5).build();
}
}

}

下面是上面的日志记录语句针对两个不同请求(针对同一 tmp.txt 文件)的输出:

[2022-01-25T11:25:50.663Z] ----------------------------657892529889811456400753
[2022-01-25T11:25:50.664Z] Content-Disposition: form-data; name="file"; filename="tmp.txt"
[2022-01-25T11:25:50.664Z] Content-Type: text/plain
[2022-01-25T11:25:50.665Z]
[2022-01-25T11:25:50.665Z] abd
[2022-01-25T11:25:50.666Z] ----------------------------657892529889811456400753--

[2022-01-25T11:26:05.836Z] ----------------------------005234239999372220970885
[2022-01-25T11:26:05.837Z] Content-Disposition: form-data; name="file"; filename="tmp.txt"
[2022-01-25T11:26:05.838Z] Content-Type: text/plain
[2022-01-25T11:26:05.838Z]
[2022-01-25T11:26:05.838Z] abd
[2022-01-25T11:26:05.839Z] ----------------------------005234239999372220970885--

由于 header 不同,生成的 md5 校验和也不匹配。有没有一种聪明的方法来只删除内容?

任何帮助将不胜感激。

最佳答案

我找到了问题的解决方案:我从 Azure 逻辑应用调用该函数。因此,首先转换为字符串,然后将此字符串传递给函数应用程序就可以了:

byte[] b = File.ReadAllBytes(@"filepath");
string s = Convert.ToBase64String(b);

关于java - Azure函数计算md5校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70847977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com