码迷,mamicode.com
首页 > Web开发 > 详细

Spriing mvc上传多文件

时间:2015-12-04 10:38:10      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

 

jsp页面

<form action="${pageContext.request.contextPath }/user/upload" method="post" enctype="multipart/form-data">
   	attach 1:<input type="file" name="attachs" /><br/>
   	attach 2:<input type="file" name="attachs"/><br/>
   	<button type="submit">提交</button>
</form>

 

controller.java 接收数据(代码如下)

@RequestMapping(value="/upload",method=RequestMethod.POST )
    public String upload(@RequestParam("attachs") MultipartFile[] attachs,HttpServletRequest req){
        String realPath=req.getSession().getServletContext().getRealPath("/resources/upload");//保存的路径
        System.out.println(attachs.length);
        System.out.println(realPath);
        for(MultipartFile attach:attachs){
            System.out.println(attach);
            if(!attach.isEmpty()){
                File file=new File(realPath+"/"+attach.getOriginalFilename());
                try {
            //底层数据io传输 attach.transferTo(file); }
catch (IllegalStateException | IOException e) { e.printStackTrace(); } } } return "welcome"; }


dispatcherServlet-servlet.xml  代码:

<!-- 配置MultipartResolver,用于上传文件,使用Spring的CommonsMultiparResolver -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000"/>
            <property name="defaultEncoding" value="UTF-8"/>
    </bean>

 

Spriing mvc上传多文件

标签:

原文地址:http://www.cnblogs.com/zk753159/p/5018318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!