SpringMVC 通过commons-fileupload实现文件上传功能

2025-05-29 0 75

配置

web.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"

xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

xsi:schemalocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"

version="5.0">

<!--注册dispatcherservlet-->

<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:applicationcontext.xml</param-value>

</init-param>

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

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

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

</servlet-mapping>

</web-app>

springmvc配置文件 applicationcontext.xml

上传文件的核心配置类:commonsmultipartresolver,注意id="multipartresolver"不要写错

?

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
<?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/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

https://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

https://www.springframework.org/schema/mvc/spring-mvc.xsd">

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

<context:component-scan base-package="com.pro.controller"/>

<!--配置静态资源过滤-->

<mvc:default-servlet-handler/>

<!--配置注解驱动-->

<mvc:annotation-driven/>

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

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

<!--前缀-->

<property name="prefix" value="/web-inf/jsp/"/>

<!--后缀-->

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

</bean>

<!--springmvc文件上传配置-->

<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver">

<!--设置请求的编码格式, 必须和pageencoding的属性一致, 以便正确读取表单的值, 默认为iso-8859-1-->

<property name="defaultencoding" value="utf-8"/>

<!--上传文件的大小限制, 单位为字节 (10485760 = 10m)-->

<property name="maxuploadsize" value="10485760"/>

<property name="maxinmemorysize" value="40960"/>

</bean>

</beans>

文件上传 controller

上传实现一

?

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
package com.pro.controller;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.requestparam;

import org.springframework.web.bind.annotation.restcontroller;

import org.springframework.web.multipart.commons.commonsmultipartfile;

import javax.servlet.http.httpservletrequest;

import javax.servlet.http.httpservletresponse;

import java.io.*;

import java.net.urlencoder;

import java.util.hashmap;

import java.util.map;

@restcontroller

public class filecontroller {

/*

* 采用file.transferto 来保存上传的文件

*/

@requestmapping("/upload2")

public map fileupload2(@requestparam("file") commonsmultipartfile file, httpservletrequest request) throws ioexception {

//上传路径保存设置

string path = request.getservletcontext().getrealpath("/upload");

file realpath = new file(path);

if (!realpath.exists()){

realpath.mkdir();

}

//上传文件地址

system.out.println("上传文件保存地址 --> "+realpath);

//通过commonsmultipartfile的方法直接写文件(注意这个时候)

file.transferto(new file(realpath +"/"+ file.getoriginalfilename()));

map<object, object> hashmap = new hashmap<>();

hashmap.put("code", 0);

hashmap.put("msg", "上传成功");

return hashmap;

}

}

上传实现二

这里的文件名称没有使用 uuid组合名称 为了方便测试

?

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

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63
package com.pro.controller;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.requestparam;

import org.springframework.web.bind.annotation.restcontroller;

import org.springframework.web.multipart.commons.commonsmultipartfile;

import javax.servlet.http.httpservletrequest;

import javax.servlet.http.httpservletresponse;

import java.io.*;

import java.net.urlencoder;

import java.util.hashmap;

import java.util.map;

@restcontroller

public class filecontroller {

// @requestparam("file") 将 name=file 控件得到的文件封装成 commonsmultipartfile 对象

// 批量上传把 commonsmultipartfile 改为数组即可

@requestmapping("/upload")

public string upload(@requestparam("file") commonsmultipartfile file, httpservletrequest request) throws ioexception {

// 获取文件名称

string uploadfilename = file.getoriginalfilename();

// 如果文件名为空, 直接返回首页

if ("".equals(uploadfilename)) {

return "file upload error";

}

system.out.println("上传文件名 --> " + uploadfilename);

// 设置文件的保存位置

string path = request.getservletcontext().getrealpath("/upload");

// 判断路径是否存在

file realpath = new file(path);

if (!realpath.exists()) {

// 如果不存在就创建

realpath.mkdir();

}

system.out.println("文件保存路径 --> " + realpath);

// 获取文件输入流

inputstream is = file.getinputstream();

// 获取文件输出流

fileoutputstream os = new fileoutputstream(new file(realpath, uploadfilename));

// 缓冲区读写文件

byte[] buffer = new byte[1024];

int len;

while ((len = is.read(buffer)) != -1) {

os.write(buffer, 0, len);

os.flush();

}

// 关闭流

os.close();

is.close();

return "file upload success";

}

}

测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
<%@ page contenttype="text/html;charset=utf-8" language="java" %>

<html>

<head>

<title>$title$</title>

</head>

<body>

<form enctype="multipart/form-data" action="${pagecontext.request.contextpath}/upload2" method="post">

<input type="file" name="file">

<input type="submit" value="上传实现一">

</form>

<form enctype="multipart/form-data" action="${pagecontext.request.contextpath}/upload" method="post">

<input type="file" name="file">

<input type="submit" value="上传实现二">

</form>

</body>

</html>

依赖

核心依赖就是 commons-fileupload

?

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
<!--导入依赖-->

<dependencies>

<!--单元测试-->

<dependency>

<groupid>junit</groupid>

<artifactid>junit</artifactid>

<version>4.13</version>

</dependency>

<!--spring-->

<dependency>

<groupid>org.springframework</groupid>

<artifactid>spring-webmvc</artifactid>

<version>5.2.0.release</version>

</dependency>

<!--文件上传-->

<dependency>

<groupid>commons-fileupload</groupid>

<artifactid>commons-fileupload</artifactid>

<version>1.3.3</version>

</dependency>

<!--servlet-api导入高版本的-->

<dependency>

<groupid>javax.servlet</groupid>

<artifactid>javax.servlet-api</artifactid>

<version>4.0.1</version>

</dependency>

<!--jsp-->

<dependency>

<groupid>javax.servlet.jsp</groupid>

<artifactid>jsp-api</artifactid>

<version>2.2</version>

</dependency>

<!--jstl表达式-->

<dependency>

<groupid>javax.servlet</groupid>

<artifactid>jstl</artifactid>

<version>1.2</version>

</dependency>

</dependencies>

到此这篇关于springmvc 通过commons-fileupload实现文件上传的文章就介绍到这了,更多相关springmvc 实现文件上传内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 SpringMVC 通过commons-fileupload实现文件上传功能 https://www.kuaiidc.com/109439.html

相关文章

发表评论
暂无评论