springmvc 文件上传
Posted yuby
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springmvc 文件上传相关的知识,希望对你有一定的参考价值。
1.通过commons-fileupload来实现:导入jar包commons-fileupload和commons-io
2.配置springmvc:配置解析器:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="10485760000"/> <property name="maxInMemorySize" value="40960"/> </bean>
3.编写上传处理器代码:
@RequestMapping("/upload") public String fileupload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException //获取文件名 // file.getOriginalFilename(); String path ="/fileupload"; InputStream is = file.getInputStream(); OutputStream os = new FileOutputStream(new File(path,file.getOriginalFilename())); int len=0; byte[] buffer = new byte[400]; while ((len = is.read(buffer)) != -1) os.write(buffer,0,len); os.close(); is.close(); return "index.jsp";
4.jsp页面:
<form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" value=""/><br/> <input type="submit" value="submit"> </form>
以上是关于springmvc 文件上传的主要内容,如果未能解决你的问题,请参考以下文章