springmvc-servlet.xml中添加配置
<!-- 文件上传表单的视图解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxUploadSize" value="204800"/> </bean>
前端页面代码如下
<form action="/submitFile.do" method="post" enctype="multipart/form-data"> <label style="background-color: aqua">文件上传 <input name="file" type="file" onchange="filechange(this)" hidden/><input type="text"/></label> <br/> <input type="submit" value="上传"/> </form> <hr/> <form action="/submitFile1.do" method="post" enctype="multipart/form-data"> <label style="background-color: aqua">文件上传1 <input name="files" type="file" onchange="filechange(this)" multiple hidden/><input type="text"/></label> <br/> <input type="submit" value="上传"/> </form> <hr/> <form action="/submitFile2.do" method="post" enctype="multipart/form-data"> <label style="background-color: aqua">文件上传2 <input name="file" type="file" onchange="filechange(this)" hidden/><input type="text"/></label> <br/> <input type="submit" value="上传"/> </form> <script type="text/javascript"> function filechange(obj) { $(obj).next(":input").val($(obj).val()); } </script>