SpringMVC文件上传

运用SpringMVC的文件上传,我们只需要一些配置就可以了,很方便!

只需要一下几步就行:

1.新建xml文件,例如:a.xml 配置如下:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
default-lazy-init="true">  
<!--Controller类的路径-->
    <bean id="pickMaintainController" class="com.ly.controller.PickMaintainController"></bean>
</beans>

 2.在spring-application.xml中添加配置如下:

<!-- 支持文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
在web.xml中添加配置如下:
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:resources/codeifAction.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
</servlet>

 3.Controller类如下:红色标注为关键

@Controller
public class PickMaintainController {
@RequestMapping("batchUpload")
  public String batchUpload(Model model, @RequestParam(value = "file", required = false) MultipartFile file,  HttpServletRequest request) {
    //上传文件操作
    
  }
}

 4.jsp页面:注意name值file和 MultipartFile file一样的

<form id="" method="post" action="" enctype="multipart/form-data">
  <input type="file" id="file" name="file" />
</form>

想了想还把上传文件操作贴出来吧:不然你会苦恼的。。。

@RequestMapping("batchUpload")
  public String batchUpload(Model model, @RequestParam(value = "file", required = false) MultipartFile file,  HttpServletRequest request) {
    //上传文件操作
    String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();
		System.out.println(path);
		System.out.println(fileName);
		File targetFile = new File(path, fileName);
		if(!targetFile.exists()) {
			targetFile.mkdirs();
		}
		//如果文件存在,就删除
		deleteFiles(path+"\\"+fileName, fileName);
		//保存
		try{
			file.transferTo(targetFile);
		}catch(Exception e) {
			e.printStackTrace();
			System.out.println("文件保存失败!");
		}
		String filePath = request.getSession().getServletContext().getRealPath("upload")+"/"+fileName;
		
  }
    //删除指定路径下的文件
	 public static void deleteFiles(String path, String fileName){
	       File file = new File(path);
	       //1級文件刪除
	       if(!file.isDirectory() && file.getName().equals(fileName)){
	           file.delete();
	       }else if(file.isDirectory()){
	           //2級文件列表
	           String []filelist = file.list();
	           //获取新的二級路徑
	           for(int j=0;j<filelist.length;j++){
	               File filessFile= new File(path+"\\"+filelist[j]);
	               if(!filessFile.isDirectory() && file.getName().equals(fileName)){
	                   filessFile.delete();
	               }else if(filessFile.isDirectory()){
	                   //递归調用
	                   deleteFiles(path+"\\"+filelist[j], fileName);
	               }
	           }
	           file.delete();
	       }
	    }
    
}

 有问题请留言。。。。

 

posted @ 2017-07-24 17:32  懒得烧蛇吃  阅读(197)  评论(0编辑  收藏  举报