1、html

        因为是平时练习,这个html就弄的很简单。

<!-- 设置表单的类型,是上传文件 enctype="multipart/form-data" 请求方式: 数据量比较大 POST  -->
	<form action="/springMVC_my/file/upload2.do" method="POST" enctype="multipart/form-data">
		
		选择文件
			<input type="file" name="file" />		
			<input type="submit" value="上传" />
		
	</form>


2、Controller

        这几个方法都是我平时练习,所以还没来得及完善。

        在那到前台传过来的文件后,应该首先进行一个判断,判断收到的文件是否为空(即用户有没有选择文件)。如果不加判断,则会在用户没有选择文件而直接点击上传时出现异常。

    方法一:

        这个方法发路径是获取系统的路径,区别是不能再自己想要的地方创建文件夹。

//文件上传
@Controller   
@RequestMapping(value="/file/")  //设置根路径
public class FileController {

	@RequestMapping(value="upload") //设置路径
	// MultipartFile file 为获取上传的文件
	public String upload(@RequestParam MultipartFile file){
		System.out.println("进来了,文件为:"+file);
		String fileName = file.getOriginalFilename();	//获得上传文件的名字(带格式名)
		System.out.println("文件名为:"+fileName);
		//Thread.currentThread().getContextClassLoader():意为根据当前线程,获得服务器的根目录
		//getResource("."):意为获得类加载的位置
		//getPath(): 找根路径的资源,转换为地址
		String path = Thread.currentThread().getContextClassLoader().getResource(".").getPath();//获得服务器路径		
		//File.separator:为获得系统的路径斜杠;windows为“\”,Linux为“/”
		String newFilePath =  path + File.separator + fileName; //获得新文件的路径
		File newfile = new File(newFilePath); 	//在新的文件路径里,创建新文件(此文件现在为空白的文件)
		try {
			file.transferTo(newfile); //将上传文件写入新建文件中
		}  catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(path);
		return "upload-success.html";		
	}
}

    方法二:(推荐)

          这个方法可以在系统的根路径下新建一个新的文件夹,来管理用户上传的文件。这里是在根路径下新建了一个upload文件夹。

//文件上传
@Controller   
@RequestMapping(value="/file/")  //设置根路径
public class FileController {@RequestMapping(value="upload2") //设置路径
	// MultipartFile file 为获取上传的文件
	public String upload2(@RequestParam MultipartFile file){	
		System.out.println("进来了,文件为:"+file);
		String fileName = file.getOriginalFilename();	//获得上传文件的名字(带格式名)
		System.out.println("文件名为:"+fileName);
		//Thread.currentThread().getContextClassLoader():意为根据当前线程,获得服务器的根目录
		//getResource("."):意为获得类加载的位置
		//getPath(): 找根路径的资源,转换为地址
		String path = Thread.currentThread().getContextClassLoader().getResource(".").getPath();//获得服务器路径
	
		//File.separator:为获得系统的路径斜杠;windows为“\”,Linux为“/”
		String newFilePath =  path + File.separator; //根路径加斜杠
		newFilePath += ("upload"+File.separator);	 //根路径后接需要保存文件夹的名称,后接斜杠
		newFilePath += fileName;					 //最后在接上文件名,获得新文件的路径
			
		File newfile = new File(newFilePath); 	//在新的文件路径里,创建新文件(此文件现在为空白的文件)
		File parent = newfile.getParentFile();	//获得新创建文件的父路径
		if(!parent.exists()){	//判断新建文件的父路径是否存在
			parent.mkdirs();	//如果不存在则创建该文件夹
		}	
		try {
			file.transferTo(newfile); //将上传文件写入新建文件中
		}  catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(path);
		return "upload-success.html";		
	}
}

   方法三:

        这个方法可以在想要的任何地方新建文件夹,但我个人并不推荐这种方法。因为这种方法在自己电脑上跑可能是没有问题,但是项目在其他部署的时候,可能对方没有设置的这个路劲,因而报错。

@Controller   //转换为bean
@RequestMapping(value="/file/")  //设置根路径
public class FileController {

	// 不建议使用这种方法,因为本项目在其他的电脑上部署,则会没有这个文件
	//并且一般项目都不会让客户进行权限以外的操作
	//创建自定义文件夹,上传的文件保存到该文件夹中  
	@RequestMapping(value="upload3") //设置路径
	// MultipartFile file 为获取上传的文件
	public String upload3(@RequestParam MultipartFile file){
		System.out.println("进来了,文件为:"+file);
		String fileName = file.getOriginalFilename();	//获得上传文件的名字(带格式名)
		System.out.println("文件名为:"+fileName);	
		String path  = "C:\\Users\\Administrator\\Desktop\\upload\\niubi";	
		File file2 = new File(path);  
		if(!file2.exists()){    //判断该路径下是否有该文件夹	
			file2.mkdirs();		//如果在该路径下没有该目录,则在该路径下创建目录
		}	
		//File.separator:为获得系统的路径斜杠;windows为“\”,Linux为“/”
		String newFilePath =  file2 + File.separator + fileName; //获得新文件的路径
		File newfile = new File(newFilePath); 	//在新的文件路径里,创建新文件(此文件现在为空白的文件)
		try {
			file.transferTo(newfile);   //将上传文件写入新建文件中
		}  catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(path);
		return "upload-success.html";		
	}
}

注:在方法最后return的时候我是直接写了html名,因为我新建了一个和根目录名一样的文件夹,所以才能这么写。如果没有新建文件夹,则应该写成return "/upload-success.html";


3、application.xml

    上传文件的配置

<!-- 上传文件 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8" />
		<!-- 最大上传大小 104857600 字节 ~ 100M -->
		<property name="maxUploadSize" value="104857600" />
		<!-- 最大上传服务器的内存大小 -->
		<property name="maxInMemorySize" value="4096" />
	</bean>