spring上传单个文件示例

首先在pom.xml中加入:

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

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>


在servlet-context.xml里边输入:

	<!-- file uploads starts -->
	<beans:bean id="multipartResolver"	class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<beans:property name="maxUploadSize" value="100000000"/>
	</beans:bean>
	<!-- file uploads ends -->

jsp文件:

<form id="loginform" name="loginform" method="POST"
	enctype="multipart/form-data" action="uploadvideo">

	上传视频文件<input name="videoFile" type="file" value="浏览" />
	<br>
	<input	type="submit" value="上传文件" />
	<br>
</form>

后台spring controller处理文件:

	@RequestMapping(value = "/user/uploadvideo")
	public String uploadfile(@RequestParam(value = "videoFile") MultipartFile videofile,
	HttpServletRequest request, HttpServletResponse response,
	Model model) throws Exception
	{
		if( videofile.isEmpty())
		{
			model.addAttribute("errcode", "请上传有效video文件!");
			return "user/renderuploadvideo";
		}
		
		System.out.println(videofile.getOriginalFilename());
		
		File file = new File("E:\\test\\uploadvideo\\" + videofile.getOriginalFilename());
		FileUtils.writeByteArrayToFile(file, videofile.getBytes());
		
		return "user/renderuploadvideo";
         }

这样就可以上传单个文件了

你可能感兴趣的:(spring上传单个文件示例)