【App Service】Java应用上传文件功能部署在App Service Windows上报错 413 Payload Too Large

问题描述

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

image

从请求的错误返回消息看,这个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 配置页面

image

或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文件

image

 

所以,就需要手动创建web.config文件,然后在文件中写入如下内容:

web.config

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;configuration&gt;
&lt;system.web&gt;
    &lt;httpRuntime maxRequestLength="512000" /&gt; &lt;!-- Size in KB, e.g., 500 MB --&gt;
  &lt;/system.web&gt;
  &lt;system.webServer&gt;
    &lt;security&gt;
      &lt;requestFiltering&gt;
        &lt;!-- 允许最大上传 500MB --&gt;
        &lt;requestLimits maxAllowedContentLength="524288000" /&gt;
      &lt;/requestFiltering&gt;
    &lt;/security&gt;

  &lt;/system.webServer&gt;
&lt;/configuration&gt;

再次测试, 已经可以上传到大于30MB的文件了!

image

 

参考资料

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

 

正在加载评论...