首页 > 编程语言 > 详细

springmvc多文件上传

时间:2016-11-26 17:54:28      阅读:209      评论:0      收藏:0      [点我收藏+]
@Controller
public class MultiController {

    // 处理器方法
    @RequestMapping(value = "/two.do")
    public String doFirst(HttpSession session,
            @RequestParam MultipartFile[] uploadFile) throws Exception {
        for (MultipartFile item : uploadFile) {
            // 1.获取文件名称
            String filename = item.getOriginalFilename();
            // 6.必须选择上传文件
            if (item.getSize() > 0) {
                // 5.限制文件类型
                if (filename.endsWith("jpg") || filename.endsWith("png")
                        || filename.endsWith("txt")) {
                    // 2.获取保存的前路径
                    String rootPath = session.getServletContext().getRealPath(
                            "upload");
                    // 3.拼接路径
                    File realPath = new File(rootPath, filename);
                    // 4.保存
                    item.transferTo(realPath);
                } else {
                    return "/error.jsp";
                }
            } else {
                return "/error.jsp";
            }
        }
        return "/two.jsp";
    }
}

 

springmvc多文件上传

原文:http://www.cnblogs.com/cnsdhzzl/p/6104565.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!