spring mvc中的文件上传

在前台写一个类型为file的按钮,点击按钮选择要上传的文件。

//文件上传
    	if(file != null && file.getOriginalFilename() !=null && file.getOriginalFilename().length()>0) {			
    		String filepath=request.getSession().getServletContext().getRealPath("/");
    		String pic_path = filepath+"\\pic_file\\"; 
    		String wjmc = file.getOriginalFilename();
    		//扩展名
    		String extendsName = wjmc.substring(wjmc.lastIndexOf("."));
    		//文件名使用UUID是避免文件名重复
    		String newFilename = UUID.randomUUID().toString() + extendsName;
    		//文件
    		File uploadFile = new File(pic_path + newFilename);
    		file.transferTo(uploadFile);
    		model.addAttribute("filepath", "/file/" + newFilename);
    	}

猜你喜欢

转载自blog.csdn.net/cxc_happy111/article/details/80872930
今日推荐