好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Springmvc实现文件上传

本文实例为大家分享了springmvc实现 文件上传 的具体代码,供大家参考,具体内容如下

1.环境搭建:

在maven的pom.xml文件中导入两个依赖

1)测试数据mons-fileupload
2)测试数据mons-io

在resources目录下的springmvc.xml文件中配置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

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

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

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

     xmlns:context= "http://HdhCmsTestspringframework.org/schema/context"

     xmlns:mvc= "http://HdhCmsTestspringframework.org/schema/mvc"

     xsi:schemalocation="

     http: //HdhCmsTestspringframework.org/schema/context

     http: //HdhCmsTestspringframework.org/schema/context/spring-context.xsd

     http: //HdhCmsTestspringframework.org/schema/mvc

     http: //HdhCmsTestspringframework.org/schema/mvc/spring-mvc.xsd

     http: //HdhCmsTestspringframework.org/schema/beans

     http: //HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd">

   <!--包扫描-->

   <context:component-scan base- package = "cn.itcast" ></context:component-scan>

   <!--配置multipartresolver,注意:id名称固定为multipartresolver-->

   <bean id= "multipartresolver" class = "org.springframework.web.multipart测试数据mons测试数据monsmultipartresolver" >

 

   </bean>

 

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

  

   <!--让静态资源不经过过滤器-->

   <mvc:resources mapping= "/js/**" location= "/js/" ></mvc:resources>

 

   <!--视图解析器给controller中返回的逻辑视图名加上前缀和后缀-->

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

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

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

   </bean>

</beans>

2.编写前台测试jsp

?

1

2

3

4

5

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

     上传的文件:<input type= "file" name= "upload" ><br/>

     密钥:<input type= "text" name= "password" ><br/>

     <input type= "submit" value= "提交" >

  </form>

注意页面三要素:

      1).表单提交方式必须为post

      2).表单中必须有file域,即type="file"

      3).表单中enctype="multipart/form-data"

3.编写后台测试代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@controller

@requestmapping ( "/test" )

public class fileuploadcontroller {

   @requestmapping ( "/file" )

   public string testfileupload(httpservletrequest request, multipartfile upload) throws ioexception {

      //upload是表单中文件name属性值,必须保持一致

     system.out.println( "testfileupload..." );

     string realpath = request.getsession().getservletcontext().getrealpath( "/uploads" );

     file file = new file(realpath);

     if (!file.exists()){

       file.mkdirs(); //创建文件夹

     }

     string filename = upload.getoriginalfilename(); //获取文件名

     string name = upload.getname(); //获取表单中的name属性值 即upload

 

     string uuid = uuid.randomuuid().tostring().replaceall( "-" , "" ); //生成uuid避免文件名重复导致冲突覆盖

     filename=uuid+ "_" +filename;

     upload.transferto( new file(file,filename));

     return "forward:success.jsp" ;

}

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

原文链接:https://HdhCmsTestcnblogs测试数据/georgeJavaEE/archive/2018/10/13/9782358.html

查看更多关于Springmvc实现文件上传的详细内容...

  阅读:22次