SpringMVC上传文件
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>
后台代码如下,如果是上传多文件,必须带有@RequestParam
@RequestMapping("/submitFile") public void submitFile(HttpServletRequest request,@RequestParam MultipartFile file){ try { BufferedInputStream bufferedInputStream=new BufferedInputStream(file.getInputStream()); String filePath="F:\\IDEAWeb\\MavenSpringMVC\\target\\test.txt"; BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(filePath)); byte[] buffer=new byte[1024]; int l=0; while ((l=bufferedInputStream.read(buffer))!=-1){ bufferedOutputStream.write(buffer,0,l); bufferedOutputStream.flush(); } bufferedOutputStream.close(); bufferedInputStream.close(); }catch (Exception ex){ ex.printStackTrace(); } } @RequestMapping("/submitFile1") public void submitFile1(HttpServletRequest request,@RequestParam MultipartFile[] files){ try { BufferedInputStream bufferedInputStream=new BufferedInputStream(files[0].getInputStream()); String filePath="F:\\IDEAWeb\\MavenSpringMVC\\target\\test.txt"; BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(filePath)); byte[] buffer=new byte[1024]; int l=0; while ((l=bufferedInputStream.read(buffer))!=-1){ bufferedOutputStream.write(buffer,0,l); bufferedOutputStream.flush(); } bufferedOutputStream.close(); bufferedInputStream.close(); }catch (Exception ex){ ex.printStackTrace(); } } @RequestMapping("/submitFile2") public void submitFile2(HttpServletRequest request){ try { MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request; MultipartFile multipartFile=multiRequest.getFile("file"); BufferedInputStream bufferedInputStream=new BufferedInputStream(multipartFile.getInputStream()); String filePath="F:\\IDEAWeb\\MavenSpringMVC\\target\\test.txt"; BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(filePath)); byte[] buffer=new byte[1024]; int l=0; while ((l=bufferedInputStream.read(buffer))!=-1){ bufferedOutputStream.write(buffer,0,l); bufferedOutputStream.flush(); } bufferedOutputStream.close(); bufferedInputStream.close(); }catch (Exception ex){ ex.printStackTrace(); } }