只要在 application.properties文件设置以下参数:(假设上传的文件上限为100MB)
#设置文件上传的大小限制
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
如果还不行的话,那还需要修改web服务器(tomcat)的配置,如果是tomcat需要修改 conf/server.xml文件,如下:
<
Connector port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxPostSize="-1"
/>
增加多一个 maxPostSize="-1" 表示不限制post请求的参数大小,如果不设置默认是2MB
注意:有些版本的tomcat默认是0表示不限制,有些版本是-1表示不限制(我用的是Tomcat9),所以最好是设置具体的数字,
例如:要限制最大20MB,可以设置: maxPostSize="20097152" (注意单位是字节)
当然后台也需要代码配合(针对大文件分块处理)
//把base64转文件(分段转码,针对大文件必须要用这个方式来把base64字符串转为文件) public static boolean getMultiBase64ToFile(String filebase64str,String filename) { try { File file = new File(filename); // 文件如果有则删除 if(file.exists()){ file.delete(); } //int size = configBean.getBillCutSize() * RANGE_UNIT; // 每次读取大小 int size = 4*RANGE_UNIT; // 每次读取大小 int beginIndex = 0; int endIndex = size; int length = filebase64str.length(); // 文件小,直接解码保存 if(length < size){ byte[] data = Base64.getDecoder().decode(filebase64str.getBytes("UTF-8")); FileUtils.writeByteArrayToFile(file, data); // 该方法会自动创建文件 MyLog.info("小文件一次保存成功......"); }else{ MyLog.info("文件过大,分批保存......"); // 文件过大,分批保存 while(endIndex < length){ String tmpStr = filebase64str.substring(beginIndex,endIndex); byte[] data = Base64.getDecoder().decode(tmpStr.getBytes("UTF-8")); FileUtils.writeByteArrayToFile(file, data,true); // 该方法会自动创建文件 beginIndex += size; endIndex += size; } // 剩下的 String lastStr = filebase64str.substring(beginIndex,length); byte[] lastData = Base64.getDecoder().decode(lastStr.getBytes("UTF-8")); FileUtils.writeByteArrayToFile(file, lastData,true); // 该方法会自动创建文件 MyLog.info("文件分段保存成功......"); } } catch (Exception e) { MyLog.info("把base64转文件发生异常"); } return Boolean.TRUE; }
最后如果还不行,则需要修改Tomcat的java内存设置: