• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
cnsdhzzl
博客园    首页    新随笔    联系   管理    订阅  订阅
springmvc多文件上传
@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";
    }
}

 

posted on 2016-11-26 16:26  cnsdhzzl  阅读(303)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3