在SpringMVC框架下实现文件的上传和下载示例

2025-05-29 0 18

在eclipse中的javaEE环境下:导入必要的架包

web.xml的配置文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35
<?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">

<!-- 配置SpringMVC的DispatcherServlet -->

<servlet>

<servlet-name>springDispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springDispatcherServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!-- 配置 HiddenHttpMethodFilter: 把 POST 请求转为 DELETE、PUT 请求 -->

<filter>

<filter-name>HiddenHttpMethodFilter</filter-name>

<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>HiddenHttpMethodFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

spring的bean的配置文件springmvc.xml;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42
<?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:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

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-4.0.xsd">

<!-- 配置自动扫描的包 -->

<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>

<!-- 配置视图解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<!--

default-servlet-handler 将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler,

它会对进入 DispatcherServlet 的请求进行筛查, 如果发现是没有经过映射的请求, 就将该请求交由 WEB 应用服务器默认的

Servlet 处理. 如果不是静态资源的请求,才由 DispatcherServlet 继续处理

一般 WEB 应用服务器默认的 Servlet 的名称都是 default.

若所使用的 WEB 服务器的默认 Servlet 名称不是 default,则需要通过 default-servlet-name 属性显式指定

-->

<mvc:default-servlet-handler/>

<!-- 一般都会配置这个 <mvc:annotation-driven ></mvc:annotation-driven>,

由于。。。requestmapping请求实现不了,使用这个,会使requestmapping请求一定实现

-->

<mvc:annotation-driven ></mvc:annotation-driven>

  <!-- 配置 MultipartResolver ,即配置文件上传的属性-->

  <bean id="multipartResolver"

    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- 默认的字符编码 -->

    <property name="defaultEncoding" value="UTF-8"></property>

    <!-- 上传文件的大小 ,最大上传大小-->

    <property name="maxUploadSize" value="1024000"></property>

  </bean>

</beans>

handler类方法:实现文件上传下载的方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37
@Controller

public class SpringMVCTest {

@Autowired

private EmployeeDao employeeDao;

//实现文件的下载

//需要说明的是文件的上传和下载不需要其他配置

@RequestMapping("testResponseEntity")

public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{

byte[] body=null;

ServletContext servletContext=session.getServletContext();

///files/abc.txt:所要下载文件的地址

InputStream in=servletContext.getResourceAsStream("/files/abc.txt");

body=new byte[in.available()];

in.read(body);

HttpHeaders headers=new HttpHeaders();

//响应头的名字和响应头的值

headers.add("Content-Disposition", "attachment;filename=abc.txt");

HttpStatus statusCode=HttpStatus.OK;

ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body, headers, statusCode);

return response;

}

  //文件上传,

    @RequestMapping("/testFileUpload")

    public String testFileUpload(@RequestParam("desc") String desc,

      @RequestParam("file") MultipartFile file) throws IOException{

      System.out.println("desc:"+desc);

      System.out.println("OriginalFilename"+file.getOriginalFilename());

      System.out.println("InputStream"+file.getInputStream());

      return "success";

  }

}

jsp页面:index.jsp:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<center>

  <!-- 文件上传的表单 -->

  <form action="testFileUpload" method="post" enctype="multipart/form-data">

    File:<input type="file" name="file"/>

    Desc:<input type="text" name="desc"/>

    <input type="submit" value="Submit"/>

  </form>

<br><br>

<!-- 文件的下载 -->

<a href="testResponseEntity" rel="external nofollow" >Test ResponseEntity</a>

</center>

</body>

</html>

success.jsp页面:显示文件上传成功

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h3>Success page</h3>

</body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/lxnlxn/p/5938861.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 在SpringMVC框架下实现文件的上传和下载示例 https://www.kuaiidc.com/118746.html

相关文章

发表评论
暂无评论