gpt4 book ai didi

java - Spring Boot 应用程序在 AWS Lambda 中运行时抛出 MultipartException

转载 作者:行者123 更新时间:2023-11-30 02:00:42 27 4
gpt4 key购买 nike

我刚刚尝试使用以下文章在 AWS Lambda 上运行 Spring Boot 应用程序:https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot

基本请求工作正常,但发送文件似乎会引起问题。在本地运行应用程序工作正常:

@RestController
@EnableWebMvc
public class PingController {
@RequestMapping(path = "/ping", method = RequestMethod.GET)
public Map<String, String> ping() {
Map<String, String> pong = new HashMap<>();
pong.put("pong", "Hello, World!");
return pong;
}

@RequestMapping(path = "/ping", method = RequestMethod.POST)
public Map<String, Long> ping(@RequestParam("file") MultipartFile file) {
Map<String, Long> pong = new HashMap<>();
pong.put("filesize", file.getSize());
return pong;
}
}

/ping 上的 GET 按预期返回有效消息:

{
"pong": "Hello, World!"
}

向/ping 发送包含图像的 POST 会导致以下异常:

{
"timestamp": 1540237302941,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.multipart.MultipartException",
"message": "Could not parse multipart servlet request; nested exception is java.lang.IllegalArgumentException: File path not allowed: /image.jpeg",
"path": "/ping"
}

将以下行添加到 application.properties 文件没有帮助:

spring.http.multipart.location=/tmp/

指定的资源是否存在并不重要。对/ping2(不存在)的 POST 会导致相同的异常。

来自 CloudWatch 的堆栈跟踪:

2018-10-22 19:47:21.701 ERROR 1 --- [ main] o.s.boot.web.support.ErrorPageFilter : Forwarding to error page from request [/ping] due to exception [Could not parse multipart servlet request; nested exception is java.lang.IllegalArgumentException: File path not allowed: /image.jpeg]

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalArgumentException: File path not allowed: /image.jpeg
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:112) ~[task/:na]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:86) ~[task/:na]
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:80) ~[task/:na]
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1104) ~[task/:na]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936) ~[task/:na]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[task/:na]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[task/:na]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[task/:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[task/:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[task/:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter.doFilter(FilterChainManager.java:351) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:84) [task/:na]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[task/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:84) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90) [task/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108) [task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:84) [task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.AwsLambdaServletContainerHandler.doFilter(AwsLambdaServletContainerHandler.java:207) [task/:na]
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.handleRequest(SpringBootLambdaContainerHandler.java:154) [task/:na]
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.handleRequest(SpringBootLambdaContainerHandler.java:52) [task/:na]
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:168) [task/:na]
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:200) [task/:na]
at com.example.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:31) [task/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:292) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64) [LambdaSandboxJava-1.0.jar:na]
at java.lang.Class.forName0(Native Method) [na:1.8.0_181]
at java.lang.Class.forName(Class.java:348) [na:1.8.0_181]
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94) [LambdaJavaRTEntry-1.0.jar:na]
Caused by: java.lang.IllegalArgumentException: File path not allowed: /image.jpeg
at com.amazonaws.serverless.proxy.internal.SecurityUtils.getValidFilePath(SecurityUtils.java:192) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest.getMultipartFormParametersMap(AwsProxyHttpServletRequest.java:714) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest.getParts(AwsProxyHttpServletRequest.java:305) ~[task/:na]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:93) ~[task/:na]
... 38 common frames omitted

之前是否有人遇到过该错误?我还需要配置其他东西吗?

最佳答案

这是亚马逊库中的一个错误,几天前已修复。更多信息请访问here 。只需确保使用 1.2 或更高版本:

<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-spring</artifactId>
<version>1.2</version>
</dependency>

关于java - Spring Boot 应用程序在 AWS Lambda 中运行时抛出 MultipartException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52936877/

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