多文件上传

来源:互联网 发布:网络在线平交流平台 编辑:程序博客网 时间:2024/05/18 05:06
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body><h1>文件上传</h1><form action="/firstt" method="post" enctype="multipart/form-data">    文件 1  <input type="file" name="upload"/>    文件 2  <input type="file" name="upload"/>    文件 3  <input type="file" name="upload"/>    <input type="submit"/></form></body></html>
Controller:
@Controllerpublic class FirstController {    @RequestMapping("/firstt")    public String doFirst(MultipartFile[] upload, HttpSession session) {        for (MultipartFile item : upload) {            //判断用户是否选择文件            if (item.getSize()>0){                //获取到用户上传文件的名称                String childpath=item.getOriginalFilename();                //判断选择的文件格式                if (childpath.endsWith(".jpg")){                    //将相对路径转换成绝对路径                    String parentPath=session.getServletContext().getRealPath("/uploa");                    //file写入指定路径                    File filepath=new File(parentPath,childpath);                    try {                        item.transferTo(filepath);                    } catch (IOException e) {                        e.printStackTrace();                        return "/index.jsp";                    }                }else{                    return "/index.jsp";                }            }else{                return "/index.jsp";            }        }        return "/index.jsp";    }}
配置:
<!--扫描器--><context:component-scan base-package="wenjiansahngchuan"></context:component-scan><mvc:annotation-driven/><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  <property name="defaultEncoding" value="utf-8"></property>  <property name="maxUploadSize" value="5000000"></property></bean>

原创粉丝点击