springMVC文件上传和下载

导入jar包

在这里插入图片描述

配置xml

<!-- 配置CommonsMultipartResolver,用于实现文件上传,将其加入到springIOC容器中,  -->
		<!-- springmvc在初始化的时候会自动寻找id="multipartResolver",没有则会忽略该bean -->
		<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		    <!-- 指定默认的编码格式 -->
			<property name="defaultEncoding" value="UTF-8" />
			<!-- 指定允许上传的文件大小,单位Byte(1kb==1024字节),如果value里面放-1,则是无限制-->
			<property name="maxUploadSize" value="5120000" />
		</bean>

上传文件

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="fileupload" method="post" enctype="multipart/form-data">
	<input type="file" name="file" />
	<input type="submit" value="上传" />
	<br/>
	<c:forEach items="${images}" var="i">
	   <div><img src="${i}"></div>
	</c:forEach>
</form>
</body>
</html>
@RequestMapping(value = "/fileupload",method = RequestMethod.POST)
	public String  uploads(MultipartFile file,ServletRequest request) throws IllegalStateException, IOException {
		//获取文件上传路径
		String uploadaddress = request.getServletContext().getRealPath("upload");
		//获取文件名称
		String filename = file.getOriginalFilename();
		//截取文件后缀
		String newfilname = filename.substring(filename.lastIndexOf("."));
		//生成新的随机文件名称
		String newfileName = UUID.randomUUID() + newfilname;	
		//文件保存路径
		File savepath = new File(uploadaddress + "/" + newfileName);
		//将路径保存,在jsp页面显示
		request.setAttribute("message","upload/" + newfileName);
		//上传文件
		file.transferTo(savepath);
		return "fileupload";
	}

上传多个文件

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="fileupload" method="post" enctype="multipart/form-data">
	<input type="file" name="file" />
	<input type="file" name="file" />
	<input type="file" name="file" />
	<input type="submit" value="上传" />
	<br/>
	<c:forEach items="${images}" var="i">
	   <div><img src="${i}"></div>
	</c:forEach>
</form>
</body>
</html>
public String fileadd(MultipartFile file,ServletRequest request) throws IllegalStateException, IOException {
		//获取文件上传路径
		String uploadaddress = request.getServletContext().getRealPath("upload");
		//获取文件名称
		String filename = file.getOriginalFilename();
		//截取文件后缀
		String newfilname = filename.substring(filename.lastIndexOf("."));
		//生成新的随机文件名称
		String newfileName = UUID.randomUUID() + newfilname;	
		//文件保存路径
		File savepath = new File(uploadaddress + "/" + newfileName);
		//上传文件
		file.transferTo(savepath);
		String name="upload/"+newfileName;
		//返回文件路径
		return name;
	}
	@RequestMapping(value = "/fileupload",method = RequestMethod.POST)
	public String  uploads(@RequestParam("file")MultipartFile[] file,ServletRequest request) throws IllegalStateException, IOException {
		List<String> images = new ArrayList<String>();
		for (int i = 0; i < file.length; i++) {
		   //保存路径 
			images.add(fileadd(file[i],request));
		}
		request.setAttribute("images", images);
		return "fileupload";
	}

文件下载

@RequestMapping("/downFile")
	public ResponseEntity<byte[]> testdownFile(HttpSession session)throws IOException {
		ServletContext servletContext = session.getServletContext();
		//指定要下载的文件所在路径
		InputStream in = servletContext.getResourceAsStream("upload/1.gif");
		byte[] bytes = FileCopyUtils.copyToByteArray(in);
		HttpHeaders header = new HttpHeaders();
		header.add("Content-Disposition", "attachment;filename=1.gif");
		//使用Sring MVC框架的ResponseEntity对象封装返回下载数据
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, header, HttpStatus.OK);
	    return entity;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值