您当前的位置: 首页 >  spring

星夜孤帆

暂无认证

  • 1浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC基础-12-文件上传

星夜孤帆 发布时间:2019-04-22 01:03:00 ,浏览量:1

pom.xml

  4.0.0
  com.monkey1024
  02mvc
  0.0.1-SNAPSHOT
  war
  
		
		  commons-io
		  commons-io
		  2.6
		
		
		  commons-fileupload
		  commons-fileupload
		  1.3.3
		
		
		   org.hibernate
		   hibernate-validator
		   6.0.9.Final
		
        
            junit
            junit
            3.8.1
            test
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
        

    
    
    	
        02mvc
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                    1.8
                    1.8
                    UTF-8
                
            
        
    
web.xml


	 
	
	
	
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
		
			
			encoding
			utf-8
		
		
		
		
			forceEncoding
			true
		
		
	
	
		characterEncodingFilter
		/*
	
	
	
		springMVC
		
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springmvc.xml
		
		1
	
	
		springMVC
		*.do
	
springmvc.xml


        
	
	
	
	
	
		
		
		
		
	
	

	
	
	


	

	
	
	
	
	
	
	



	
	
	
	
	
	
	
		
			
				
				error/myerror
			
		
		
		
		
		
		
	
	
	
	
	
    
	
	
	
	
	
	
	
	
	
		
	
	
	
	
		
		
	
	

	
	

 
UploadController.java
package com.monkey1024.controller;

import java.io.File;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/*
 * 文件上传
 */
@Controller
public class UploadController {
	
	//处理单个文件上传
	@RequestMapping("/upload1.do")
	public ModelAndView upload1(MultipartFile photo,HttpSession session) throws Exception{
		ModelAndView mv = new ModelAndView();
		//判断用户是否上传了文件
		if(!photo.isEmpty()){
			String path = session.getServletContext().getRealPath("/upload");
			
			//获取文件上传的名称
			String fileName = photo.getOriginalFilename();
			
			//限制文件上传的类型
			if("image/png".equals(photo.getContentType())){
				File file = new File(path,fileName);
				
				//完成文件上传
				photo.transferTo(file);
			}else{
				mv.addObject("msg","请选择png格式的图片上传");
				mv.setViewName("/upload_fail");
				return mv;
			}
		}else{
			mv.addObject("msg","请上传一张png格式的图片");
			mv.setViewName("/upload_fail");
			return mv;
		}
		//跳转到成功页面
		mv.setViewName("/upload_success");
		return mv;
	}
	
	//处理多个文件上传
	@RequestMapping("/upload2.do")
	public ModelAndView upload2(@RequestParam MultipartFile[] photos,HttpSession session) throws Exception{
		ModelAndView mv = new ModelAndView();
		
		//获取服务器中文件上传的路径
		String path = session.getServletContext().getRealPath("/upload");
		
		//遍历MultipartFile数组
		for(MultipartFile photo:photos){
			//判断用户是否上传了文件
			if(!photo.isEmpty()){
				
				//获取文件上传的名称
				String fileName = photo.getOriginalFilename();
				
				//限制文件上传的类型
				if("image/png".equals(photo.getContentType())){
					File file = new File(path,fileName);
					
					//完成文件上传
					photo.transferTo(file);
				}else{
					mv.addObject("msg","请选择png格式的图片上传");
					mv.setViewName("/upload_fail");
					return mv;
				}
			}else{
				mv.addObject("msg","请上传一张png格式的图片");
				mv.setViewName("/upload_fail");
				return mv;
			}
			
		}
		
		//跳转到成功页面
		mv.setViewName("/upload_success");
		return mv;
	}
}











upload.jsp




Insert title here


	
		图片1:
		图片2:
		图片3:
		
	

 
upload_fail.jsp




Insert title here


文件上传失败
${msg }

 

 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.0381s