SpringMVC文件上传

SpringMVC文件上传

1.pom.xml导入jar
	<!-- 本地文件上传 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.6</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>

		<!-- 跨服务器上传 -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.10</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			<version>1.10</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-core</artifactId>
			<version>1.10</version>
		</dependency>
2.springContext.xml配置CommonsMultipartResolver
<!-- 文件上传配置 -->
	<bean id="multipartResolver"
	class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 上传文件的编码 -->
		<property name="defaultEncoding" value="UTF-8" />
		<!--上传文件的最大大小,单位为字节 -->
		<property name="maxUploadSize" value="10000000" />
	</bean>
3.前端jsp页面
  • input的type设置为file
  • form表单的method设为post
  • form表单的enctype设置为multipart/form-data,序列化以二进制的形式传输数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
	<h2>Hello World!</h2>
	<form action="fileload.do" method="post" enctype="multipart/form-data">
		<input type="file" name="upload" /> 
		<input type="submit" value="上传" />
	</form>
	<br> ${msg}
</body>
</html>
4.Handler接收处理
	//本地上传
	@RequestMapping("/fileload")
	public String fileUpLoad(HttpServletRequest request, MultipartFile upload) throws Exception {
		// TODO Auto-generated method stub
		//设置本地上传后的存储路径
		String path = "D://upload";
		
		//如果文件目录不存在,创建目录
		File f = new File(path);
		if (!f.exists()) {
			f.mkdir();
		}
		
		//获取文件名
		String filename = upload.getOriginalFilename();
		
		//为确保文件名不会相同,使用UUID进行拼接
		String uuid = UUID.randomUUID().toString().replace("-", "");
		
		//最终文件名
		String name = uuid + "_" + filename.substring(filename.lastIndexOf(File.separator) + 1);
		
		//为了避免上传病毒恶意攻击,可以判断上传文件类型
		if (name.endsWith(".jpg")||name.endsWith(".png")) {
			//写入文件
			upload.transferTo(new File(path, name));
			request.setAttribute("msg", "上传成功");
			return "forward:index.jsp";
		}else {
			request.setAttribute("msg", "上传失败");
			return "forward:index.jsp";
		}
	}


	// 跨服务器上传
	 @RequestMapping("/fileloadtest")
	public String fileUpLoad01(HttpServletRequest request, MultipartFile upload) throws Exception {
		// TODO Auto-generated method stub

		// 定义上传文件服务器路径
		String path = "http://localhost:8888/uploads/";
		//拼接文件名
		String filename = upload.getOriginalFilename();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String name = uuid + "_" + filename.substring(filename.lastIndexOf(File.separator) + 1);

		// 创建客户端的对象
		Client client = Client.create();

		// 和图片服务器进行连接
		WebResource webResource = client.resource(path + filename);

		// 上传文件
		webResource.put(upload.getBytes());

		return "success";
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章