SSM文件上传

首先springmvc-servlet.xml中配置

<!-- 单文件上传 配置MultiparResolver用户上传文件,使用spring-->
	<bean id="multipartResolver"
	class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxInMemorySize" value="5000000"/>
		<property name="defaultEncoding" value="UTF-8"/>
	</bean>

form表单处理

 <form action="hr/filterSc" method="post" enctype="multipart/form-data">
        <label>文件上传</label>
        <input type="file" name="file">
        <input type="submit" value="提交">
    </form>

Controller中处理

	@RequestMapping("filterSc")
	public String filterSc(MultipartFile file, HttpServletRequest request) throws IllegalStateException, IOException {
	        // uploads文件夹位置
	        String rootPath = request.getServletContext().getRealPath("resources/uploads/");
	        // 原始名称
	        String originalFileName = file.getOriginalFilename();
	        // 新文件名
	        String newFileName = "sliver" + originalFileName.substring(originalFileName.lastIndexOf("."));
	        // 新文件
	        File newFile = new File(rootPath + File.separator + newFileName);
	        // 判断目标文件所在目录是否存在
	        if( !newFile.getParentFile().exists()) {
	            // 如果目标文件所在的目录不存在,则创建父目录
	            newFile.getParentFile().mkdirs();
	        }
	        System.out.println(newFile);
	        // 将内存中的数据写入磁盘
	        file.transferTo(newFile);
	        // 完整的url
	        String fileUrl =newFileName;
	        System.out.println(fileUrl);
		return "filterSc";
	}

测试
选择图片SSM文件上传
提交
在自己的tomcat中项目中,我的路径E:\Tomcat\apache-tomcat-7.0.94\webapps\HrWebTwo\resources\uploads\sliver.jpg
sliver.jpg
SSM文件上传
Over!!!