【发布时间】:2022-01-25 11:37:14
【问题描述】:
我正在尝试使用基于 Java 的 Azure 函数计算我通过 POST 请求提交的文件的 md5 校验和。但是,当我将文件作为请求的一部分作为字符串提交时,它包含一些不断变化的标头信息(不知道为什么),因此 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--
由于标头不同,生成的 md5 校验和也不匹配。有没有巧妙的方法来只剥离内容?
任何帮助将不胜感激。
【问题讨论】:
标签: java azure azure-functions md5