【App Service】Java應(yīng)用上傳文件功能部署在App Service Windows上報(bào)錯(cuò) 413 Payload Too Large
問題描述
Java應(yīng)用開發(fā)了一個(gè)上傳文件的接口,部署在App Service ( Windows ) 環(huán)境上。在測(cè)試過程中,發(fā)現(xiàn)當(dāng)文件較大的時(shí)候就會(huì)遇見413 Payload Too Large的報(bào)錯(cuò)。

從請(qǐng)求的錯(cuò)誤返回消息看,這個(gè)429是由IIS服務(wù)返回的,并不是Java 應(yīng)用上的的返回。
server : Microsoft-IIS/10.0
x-powered-by : ASP.NET
根據(jù)這個(gè)信息,在網(wǎng)上查看 "IIS 413 Payload Too Large " ,得知IIS默認(rèn)的文件上傳大小不超過30MB,當(dāng)超過30MB后,就會(huì)返回413 Payload Too Large的錯(cuò)誤。解決方法就是在IIS中進(jìn)行配置,或者修改 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云環(huán)境上,并且部署項(xiàng)目的時(shí)候,并沒有包含Web.config文件,應(yīng)該如何配置呢?
問題解答
進(jìn)入App Service的高級(jí)工具頁面(Kudu: https://<app service name>.scm.chinacloudsites.cn/DebugConsole), 查看Java應(yīng)用war包所在的目錄 (c:\home\site\wwwroot)中,并沒有web.config文件

所以,就需要手動(dòng)創(chuàng)建web.config文件,然后在文件中寫入如下內(nèi)容:
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>
再次測(cè)試, 已經(jīng)可以上傳到大于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
當(dāng)在復(fù)雜的環(huán)境中面臨問題,格物之道需:濁而靜之徐清,安以動(dòng)之徐生。 云中,恰是如此!

浙公網(wǎng)安備 33010602011771號(hào)