码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC实现文件上传实例

时间:2020-06-05 10:30:29      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:获取   可见   pos   lan   port   dmi   sem   row   vax   

  文件的上传与下载基本上是web项目中会用到的技术,在web学习中我们用到的是 Apache fileupload这个组件来实现上传,在springmvc中对它进行了封装,让我们使用起来比较方便,但是底层还是由Apache fileupload来实现的。springmvc中由MultipartFile接口来实现文件上传。

 

1.创建web工程,搭建SpringMVC运行环境。另外再导入两个jar包     文件上传jar

 

技术图片

2. 创建前端jsp页面

  • input的type设置为file

  • form表单的method设为post,

  • form表单的enctype设置为multipart/form-data,以二进制的形式传输数据。

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="${pageContext.request.contextPath}/fileupload/upload" enctype="multipart/form-data" method="post">
11         <input type="file" name="file"><br>
12           <input type="submit" value="上传">
13       </form>
14 </body>
15 </html>

 

3.创建FileuploadController

     使用MultipartFile对象作为参数,接收前端发送过来的文件,将文件写入本地文件中,就完成了上传操作

 1 package com.springmvc.fileupload;
 2 
 3 import java.io.File;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.multipart.MultipartFile;
12 
13 /**
14  * Springmvc 文件上传
15  * 
16  * @author Administrator
17  *
18  */
19 
20 @Controller
21 @RequestMapping(value = "/fileupload")
22 public class FileUploadController {
23     // 上传页面
24     @RequestMapping(value = "/upload", method = RequestMethod.POST)
25     public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
26         // 判断文件是否为空
27         if (file.isEmpty()) {
28             return "failed";
29         }
30         // 获取文件存储路径(绝对路径)
31         String path = request.getServletContext().getRealPath("/WEB-INF/file");
32         System.out.println(path);
33         // 获取文件名称
34         String fileName = file.getOriginalFilename();
35         // 创建文件实例
36         File f = new File(path, fileName);
37         // 判断文件目录是否存在
38         if (!f.getParentFile().exists()) {
39             // 创建目录
40             f.getParentFile().mkdirs();
41             System.out.println("文件已创建");
42         }
43         // 写入文件
44         file.transferTo(f);
45         return "filesuccess";
46     }
47 
48     @RequestMapping(value = "/uploadPage")
49     public String upload() {
50 
51         return "fileupload";
52     }
53 
54 }

 

4. springmvc.xml配置CommonsMultipartResolver

1     <bean id="multipartResolver"
2         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
3         <!--上传文件的最大大小,单位为字节 -->
4         <property name="maxUploadSize" value="17367648787"></property>
5         <!-- 上传文件的编码 -->
6         <property name="defaultEncoding" value="UTF-8"></property>
7     </bean>

5.测试结果(上传成功)

技术图片

 

注意:

(一)对于数据的请求方式:get和post,首先比较两者最常见的不同:

       ①get一般用于向服务器请求获取数据,请求参数存放在URL中,并在地址栏可见,而post是向服务器提交数据,数据放置在容器(HTML HEADER)内且不可见;

       ②get方式提交的数据最多只能有1024字节,而post则没有此限制;

(二)spring的RequestMethod.GET和RequestMethod.POST,对于spring接口的method的两种定义,在访问时这两种方式的效果有不同:

      ①将一个method定义成RequestMethod.GET时,可以直接通过地址访问,这非常方便我们在开发的时候调用到我们的接口并进行测试; 

      ②同样的接口,将其method更改为RequestMethod.POST时,你会发现接口在地址栏访问不了了,只有向服务器发起一个POST请求时(例:ajax实例:如何使用json+ajax的方法实现类似前端特效tab切换效果)才起作用

 

Spring MVC实现文件上传实例

标签:获取   可见   pos   lan   port   dmi   sem   row   vax   

原文地址:https://www.cnblogs.com/ysource/p/13048031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!