springMVC进行文件上传、多文件上传、配置文件

本文详细介绍了如何在SpringMVC框架中配置文件上传,包括单文件和多文件上传。首先,配置文件存储路径为项目下的upload文件夹。接着,展示了Controller类中的文件上传处理方法以及对应的JSP页面设计。通过实例代码,读者可以学习到如何在实际项目中实现文件上传功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置文件放在springMVC.xml中

<!-- 文件上传: 配置multipartResolver -->
  <bean id="multipartResolver"
   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
      <property name="maxUploadSize" value="100242880"/>
      <property name="maxInMemorySize" value="40960"/>
    </bean>

在项目下创建upload文件夹用于放置上传到tomcat服务器的文件

文件上传controller类中的方法:

//文件上传的controller类
@RequestMapping("/testupload/upload2")
	public String upload2(HttpSession session , @RequestParam("upload") CommonsMultipartFile upload) throws IOException{
		ServletContext application = session.getServletContext();
        //获得tomcat下upload文件夹的路径
		String serverPath = application.getRealPath("upload");
		System.out.println(serverPath);
		uploadToServer(serverPath, upload);//调用命名方法为文件命名
		return "redirect:/users/up.jsp";
	}

//文件命名
private void uploadToServer(String serverPath , MultipartFile file) throws IllegalStateException, IOException{
		//得到图片名
		String fileName = file.getOriginalFilename();
		//重命名为全球唯一的名称
		String uuid = UUID.randomUUID().toString();
		String extendName = fileName.substring(fileName.lastIndexOf("."),fileName.length());
		String onlyName = uuid + extendName;
		file.transferTo(new File(serverPath,onlyName));
	}

jsp页面:

<h1>演示采用springMVC的API保存</h1>
	<form action="${pageContext.request.contextPath }/testupload/upload2"
		method="post" enctype="multipart/form-data">
		选择文件:<input type="file" name="upload" /><br /> <input type="submit"
			value="上传" />
	</form>

多文件上传:

jsp页面:

<h1>演示多文件上传</h1>
	<form action="${pageContext.request.contextPath }/testupload/upload3"
		method="post" enctype="multipart/form-data">
		选择文件1:<input type="file" name="upload" /><br /> 选择文件2:<input
			type="file" name="upload" /><br /> 选择文件3:<input type="file"
			name="file" /><br /> <input type="submit" value="上传" />
	</form>

controller类:

//多文件上传的方法
@RequestMapping("/testupload/upload3")
	public String upload3(HttpSession session , HttpRequest request) throws IOException{
		ServletContext application = session.getServletContext();
		//得到上传到服务器的图片路径
		String serverPath = application.getRealPath("upload");
		System.out.println(serverPath);
		if(request instanceof MultipartHttpServletRequest){
			MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
			List<MultipartFile> files = multipart.getFiles("upload");
			for (MultipartFile file : files) {
				uploadToServer(serverPath, file);
			}
			MultipartFile file = multipart.getFile("file");
			uploadToServer(serverPath, file);
		}
		return "redirect:/users/up.jsp";
	}

//重命名文件
private void uploadToServer(String serverPath , MultipartFile file) throws IllegalStateException, IOException{
		//得到图片名
		String fileName = file.getOriginalFilename();
		//重命名为全球唯一的名称
		String uuid = UUID.randomUUID().toString();
		String extendName = fileName.substring(fileName.lastIndexOf("."),fileName.length());
		String onlyName = uuid + extendName;
		file.transferTo(new File(serverPath,onlyName));
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值