【App Service】Java应用上传文件功能部署在App Service Windows上报错 413 Payload Too Large
问题描述
Java应用开发了一个上传文件的接口,部署在App Service ( Windows ) 环境上。在测试过程中,发现当文件较大的时候就会遇见413 Payload Too Large的报错。

从请求的错误返回消息看,这个429是由IIS服务返回的,并不是Java 应用上的的返回。
server : Microsoft-IIS/10.0
x-powered-by : ASP.NET
根据这个信息,在网上查看 "IIS 413 Payload Too Large " ,得知IIS默认的文件上传大小不超过30MB,当超过30MB后,就会返回413 Payload Too Large的错误。解决方法就是在IIS中进行配置,或者修改 web.config 中的 requestLimits maxAllowedContentLength 值。
IIS 配置页面

或web.config
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200" /> <!-- 50 MB and can be adjusted based on the need-->
</requestFiltering>
</security>
</system.webServer>
</configuration>
但是,这是在Azure App Service云环境上,并且部署项目的时候,并没有包含Web.config文件,应该如何配置呢?
问题解答
进入App Service的高级工具页面(Kudu: https://<app service name>.scm.chinacloudsites.cn/DebugConsole), 查看Java应用war包所在的目录 (c:\home\site\wwwroot)中,并没有web.config文件

所以,就需要手动创建web.config文件,然后在文件中写入如下内容:
web.config
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.web> <httpRuntime maxRequestLength="512000" /> <!-- Size in KB, e.g., 500 MB --> </system.web> <system.webServer> <security> <requestFiltering> <!-- 允许最大上传 500MB --> <requestLimits maxAllowedContentLength="524288000" /> </requestFiltering> </security> </system.webServer> </configuration>
再次测试, 已经可以上传到大于30MB的文件了!

参考资料
Understanding and Resolving the HTTP 413 (Request Entity Too Large) in IIS : https://techcommunity.microsoft.com/blog/iis-support-blog/understanding-and-resolving-the-http-413-request-entity-too-large-in-iis/4227944
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

浙公网安备 33010602011771号