springMvc 上传实例

今天学习了springMVC 的上传,在此贴一下代码~~

1、创建 Javaweb 项目

2、引入相应的 jar 包

3、web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMvc</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

4、sping-mvc.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.java"/>

    <!-- 视图解析器 -->
	<bean id="viewResolver"	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 上传配置 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="UTF-8"/>  
	    <property name="maxUploadSize" value="10000000"/>
	</bean>

</beans>


5、创建上传类

package com.java.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {

	@RequestMapping("/upload")
	public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{
		String filePath=request.getServletContext().getRealPath("/");
		System.out.println(filePath);
		file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename()));
		return "redirect:success.html";
	}
	
	@RequestMapping("/upload2")
	public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception{
		String filePath=request.getServletContext().getRealPath("/");
		System.out.println(filePath);
		for(MultipartFile file:files){
			file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename()));			
		}
		return "redirect:success.html";
	}
}


6、写两个 jsp 页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
	<table>
		<tr>
			<th colspan="2">上传文件</th>
		</tr>
		<tr>
			<td>文件一</td>
			<td>
				<input type="file" name="file1"/>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="上传文件"/>
			</td>
		</tr>
	</table>
</form>
<br><br>
<form action="upload2.do" method="post" enctype="multipart/form-data">
	<table>
		<tr>
			<th colspan="2">上传文件</th>
		</tr>
		<tr>
			<td>文件一</td>
			<td>
				<input type="file" name="file"/>
			</td>
		</tr>
		<tr>
			<td>文件二</td>
			<td>
				<input type="file" name="file"/>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="上传文件"/>
			</td>
		</tr>
	</table>
</form>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
上传成功!
</body>
</html>


到此结束啦~~


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC是一个基于Java的开源框架,用于构建Web应用程序。它提供了一种模型-视图-控制器(MVC)的架构模式,用于将应用程序的不同部分分离开来,以实现更好的可维护性和可扩展性。 下面是一个简单的Spring MVC上传下载的实例: 1. 配置文件: 首先,在Spring MVC的配置文件中添加以下配置: ```xml <!-- 配置文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> <!-- 设置最大上传文件大小为10MB --> </bean> ``` 2. 控制器类: 创建一个控制器类,用于处理上传和下载请求。以下是一个示例: ```java @Controller public class FileController { @RequestMapping(value = "/upload", method = RequestMethod.POST) public String uploadFile(@RequestParam("file") MultipartFile file) { // 处理文件上传逻辑 if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // 保存文件到服务器或者其他操作 return "redirect:/success"; // 上传成功后跳转到成功页面 } catch (IOException e) { e.printStackTrace(); } } return "redirect:/error"; // 上传失败后跳转到错误页面 } @RequestMapping(value = "/download", method = RequestMethod.GET) public ResponseEntity<Resource> downloadFile() { // 处理文件下载逻辑 // 构建文件资源对象 Resource fileResource = new FileSystemResource("path/to/file"); // 设置响应头 HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.txt"); // 返回文件资源和响应头 return ResponseEntity.ok() .headers(headers) .contentLength(fileResource.contentLength()) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(fileResource); } } ``` 3. 前端页面: 在前端页面中添加上传和下载的表单和按钮。以下是一个简单的示例: ```html <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上传" /> </form> <a href="/download">下载文件</a> ``` 这是一个简单的Spring MVC上传下载的实例。你可以根据实际需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值