springmvc的单文件和多文件上传和文件下载

匿名 (未验证) 提交于 2019-12-03 00:33:02

		<dependency> 			<groupId>commons-fileupload</groupId> 			<artifactId>commons-fileupload</artifactId> 			<version>1.3</version> 		</dependency>

SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置springmvc中的MultipartResolver:用于处理表单中的file

	<!-- 配置文件上传,如果没有使用文件上传可以不用配置 --> 	<bean name="multipartResolver" 		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 		<!-- 默认编码 --> 		<property name="defaultEncoding" value="utf-8" /> 		<!-- 文件大小最大值 --> 		<property name="maxUploadSize" value="10485760000" /> 		<!-- 内存中的最大值 --> 		<property name="maxInMemorySize" value="1024" /> 	</bean>

三.编写jsp页面(文件上传和文件下载页面


注意要在form标签中加上enctype="multipart/form-data"表示该表单是要处理文件的,这是最基本的东西,很多人会忘记然而当上传出错后则去找程序的错误,却忘了这一点。


四.编写控制类

1.多文件上传控制类

//文件上传 	private void saveFile(MultipartFile file) { 		if (!file.isEmpty()) { 			String rootPath = "C:\\Programme Software\\eclipse\\eclipse\\eclipse-workspace\\upload\\"; 			String fileName = file.getOriginalFilename(); 			File writeFile = new File(rootPath, fileName); 			try { 				file.transferTo(writeFile); 			} catch (IllegalStateException e) { 				e.printStackTrace(); 			} catch (IOException e) { 				e.printStackTrace(); 			} 		} 	} //文件上传 	@RequestMapping(value = "/uploads") 	public String Upload(MultipartFile[] files) throws Exception { 		for (MultipartFile file : files) { 			saveFile(file); 		} 		return "success"; 	}

通过MultipartFile的transferTo(File dest)这个方法来转存文件到指定的路径。

2.单文件上传控制类

	//单文件上传 	@RequestMapping(value = "/upload") 	public String Upload(MultipartFile file) throws Exception { 		if (!file.isEmpty()) { 			String rootPath = "C:\\Programme Software\\eclipse\\eclipse\\eclipse-workspace\\upload\\"; 			String fileName = file.getOriginalFilename(); 			File writeFile = new File(rootPath, fileName); 			try { 				file.transferTo(writeFile); 			} catch (IllegalStateException e) { 				e.printStackTrace(); 			} catch (IOException e) { 				e.printStackTrace(); 			} 		} 		return "success"; 	}

3.文件下载控制类

	//文件下载 	@RequestMapping("/download") 	public ResponseEntity<byte[]> DownloadFile(HttpServletRequest req, HttpServletResponse resp) 			throws ServletException, IOException { 		// 接受的是UTF-8 		req.setCharacterEncoding("utf-8"); 		// 获取项目根目录 		String path = "C:\\Programme Software\\eclipse\\eclipse\\eclipse-workspace\\upload\\a.txt"; 		// 获取文件名 		String filename = "a.txt"; 		File file = null; 		HttpHeaders headers = null; 		try { 			System.out.println(filename);// myfiles 			file = new File(path); 			// 请求头 			headers = new HttpHeaders(); 			String fileName1 = new String(filename.getBytes("UTF-8"), "iso-8859-1");// 解决文件名乱码 			// 通知浏览器以attachment(下载方式)打开图片 			headers.setContentDispositionFormData("attachment", fileName1); 			// application/octet-stream二进制流数据(最常见的文件下载)。 			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); 		} catch (Exception e) { 			throw new RuntimeException(e); 		} 		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); 	}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!