激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringMVC實現文件的上傳和下載實例代碼

SpringMVC實現文件的上傳和下載實例代碼

2020-09-25 10:58錢春華 Java教程

本篇文章主要介紹了SpringMVC實現文件的上傳和下載實例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前些天一位江蘇經貿的學弟跟我留言問了我這樣一個問題:“用什么技術來實現一般網頁上文件的上傳和下載?是框架還是java中的io流”。我回復他說:“使用springmvc框架可以做到這一點,因為springmvc為文件的上傳提供了直接的支持,但需要依賴apache提供commons fileupload組件jar包。”鑒于這個問題,我上網也百度了一下,網上很多都是介紹的使用io流來實現文件的上傳和下載,也有說到框架的,但介紹的并不是很完整,今天小錢將和大家介紹使用springmvc框架來實現文件的上傳和下載,本文將做一個項目案例來完整的使用springmvc框架實現文件的上傳和下載。

文件上傳

文件上傳是項目開發中最常見的功能。為了能上傳文件,必須將表單的method設置為post,并將enctype設置為multipart/form-data。只有在這樣的情況下,瀏覽器才會把用戶選擇的文件以二進制數據發送給服務器。

一旦設置了enctype為multipart/form-data,瀏覽器即會采用二進制流的方式來處理表單數據,而對于文件上傳的處理則涉及在服務器端解析原始的http響應。在2003年,apache software foundation發布了開源的commons fileupload組件,其很快成為servlet/jsp程序員上傳文件的最佳選擇。

servlet3.0規范已經提供方法來處理文件上傳,但這種上傳需要在servlet中完成。而springmvc則提供了更簡單的封裝。
springmvc為文件上傳提供了直接的支持,這種支持是用即插即用的multipartresolver實現的。springmvc使用apache commons fileupload技術實現了一個multipartresolver實現類:commonsmultipartresolver。因此,springmvc的文件上傳還需要依賴apache commons fileupload的組件。

本項目作為測試案例,在此我就不創建maven項目了,我直接創建的是一個dynamic web project(動態的web項目),采用tomcat 8作為web服務器,我們需要在項目中引入以下jar包,如下圖:

SpringMVC實現文件的上傳和下載實例代碼

下面我們在webcontent/web-inf下創建一個content文件夾,用于放文件的上傳、下載等jsp文件,下面我們創建uploadform.jsp文件,演示springmvc的文件上傳:

?
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
<%@ 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>文件上傳</title>
</head>
<body>
  <h2>文件上傳</h2>
  <form action="upload" enctype="multipart/form-data" method="post">
    <table>
      <tr>
        <td>文件描述:</td>
        <td><input type="text" name="description"></td>
      </tr>
      <tr>
        <td>請選擇文件:</td>
        <td><input type="file" name="file"></td>
      </tr>
      <tr>
        <td><input type="submit" value="上傳"></td>
      </tr>
    </table>
  </form>
</body>
</html>

負責上傳文件的表單和一般表單有一些區別,負責上傳文件的表單的編碼類型必須是“multipart/form-data”。

我們再src下創建一個包“cn.edu.jseti.controller”,然后創建一個fileuploadcontroller類,用于實現文件的上傳和下載功能。以下是負責上傳文件的表單功能代碼:

?
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
//上傳文件會自動綁定到multipartfile中
   @requestmapping(value="/upload",method=requestmethod.post)
   public string upload(httpservletrequest request,
      @requestparam("description") string description,
      @requestparam("file") multipartfile file) throws exception {
 
    system.out.println(description);
    //如果文件不為空,寫入上傳路徑
    if(!file.isempty()) {
      //上傳文件路徑
      string path = request.getservletcontext().getrealpath("/images/");
      //上傳文件名
      string filename = file.getoriginalfilename();
      file filepath = new file(path,filename);
      //判斷路徑是否存在,如果不存在就創建一個
      if (!filepath.getparentfile().exists()) {
        filepath.getparentfile().mkdirs();
      }
      //將上傳文件保存到一個目標文件當中
      file.transferto(new file(path + file.separator + filename));
      return "success";
    } else {
      return "error";
    }
 
   }

springmvc會將上傳的文件綁定到multipartfile對象中。multipartfile提供了獲取上傳文件內容、文件名等方法。通過transferto()方法還可以將文件存儲到硬件中,multipartfile對象中的常用方法如下:

  1. byte[] getbytes():獲取文件數據
  2. string getcontenttype[]:獲取文件mime類型,如image/jpeg等
  3. inputstream getinputstream():獲取文件流
  4. string getname():獲取表單中文件組件的名字
  5. string getoriginalfilename():獲取上傳文件的原名
  6. long getsize():獲取文件的字節大小,單位為byte
  7. boolean isempty():是否有上傳文件
  8. void transferto(file dest):將上傳文件保存到一個目錄文件中

springmvc上下文中默認沒有裝配multipartresolver,因此默認情況下其不能處理文件上傳工作。如果想使用spring的文件上傳功能,則需要在上下文中配置multipartresolver。在springmvc-config.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
43
44
<?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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemalocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 
  <!-- spring可以自動去掃描base-pack下面的包或者子包下面的java文件,
    如果掃描到有spring的相關注解的類,則把這些類注冊為spring的bean -->
  <context:component-scan base-package="cn.edu.jseti.controller"/>
 
  <!-- 視圖解析器 -->
   <bean id="viewresolver"
     class="org.springframework.web.servlet.view.internalresourceviewresolver">
    <!-- 前綴 -->
    <property name="prefix">
      <value>/web-inf/content/</value>
    </property>
    <!-- 后綴 -->
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>
 
  <bean id="multipartresolver"
    class="org.springframework.web.multipart.commons.commonsmultipartresolver">
    <!-- 上傳文件大小上限,單位為字節(10mb) -->
    <property name="maxuploadsize">
      <value>10485760</value>
    </property>
    <!-- 請求的編碼格式,必須和jsp的pageencoding屬性一致,以便正確讀取表單的內容,默認為iso-8859-1 -->
    <property name="defaultencoding">
      <value>utf-8</value>
    </property>
  </bean>
 
</beans>

部署fileupload這個web應用,在瀏覽器中輸入如下url來測試應用: http://localhost:8088/fileupload/uploadform

輸入文件描述信息并選擇上傳文件,如下圖:

SpringMVC實現文件的上傳和下載實例代碼

點上傳按鈕,這是已將上傳的文件通過二進制保存到web服務器上去了,如下圖:

SpringMVC實現文件的上傳和下載實例代碼

使用對象接收上傳文件

上面我們通過案例演示了springmvc上傳文件,接下來,我們演示使用對象接收上傳文件。

在實際項目的開發中,很多時候上傳的文件會作為對象的屬性被保存。springmvc的處理也非常的簡單。

下面我們在content文件夾創建registerform.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
27
<%@ 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>用戶注冊</title>
</head>
<body>
  <h2>用戶注冊</h2>
  <form action="register" enctype="multipart/form-data" method="post">
    <table>
      <tr>
        <td>用戶名:</td>
        <td><input type="text" name="username"></td>
      </tr>
      <tr>
        <td>請上傳頭像:</td>
        <td><input type="file" name="image"></td>
      </tr>
      <tr>
        <td><input type="submit" value="注冊"></td>
      </tr>
    </table>
  </form>
</body>
</html>

我們在src下面創建一個名叫“cn.edu.jseti.domain”包,然后再創建一個user類,必須要實現序列化接口,如下案例代碼:

?
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
package cn.edu.jseti.domain;
import java.io.serializable;
 
import org.springframework.web.multipart.multipartfile;
 
/**
 * 博客:http://blog.csdn.net/qian_ch
 * @author cody
 * @version v1.0
 */
 
//域對象,實現序列化接口
public class user implements serializable{
 
  private string username;
  private multipartfile image;
 
  public user() {
    super();
  }
 
  public string getusername() {
    return username;
  }
 
  public void setusername(string username) {
    this.username = username;
  }
 
  public multipartfile getimage() {
    return image;
  }
 
  public void setimage(multipartfile image) {
    this.image = image;
  
 
}

我們在剛才創建的fileuploadcontroller類繼續寫用于接收文件的上傳和下載功能。以下是負責接收文件的表單功能代碼:

?
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
@requestmapping(value="/register")
 public string register(httpservletrequest request,
     @modelattribute user user,
     model model) throws exception {
  system.out.println(user.getusername());
  //如果文件不為空,寫入上傳路徑
  if(!user.getimage().isempty()) {
    //上傳文件路徑
    string path = request.getservletcontext().getrealpath("/images/");
    //上傳文件名
    string filename = user.getimage().getoriginalfilename();
    file filepath = new file(path,filename);
    //判斷路徑是否存在,如果不存在就創建一個
    if (!filepath.getparentfile().exists()) {
      filepath.getparentfile().mkdirs();
    }
    //將上傳文件保存到一個目標文件當中
    user.getimage().transferto(new file(path + file.separator + filename));
    //將用戶添加到model
    model.addattribute("user", user);
    return "userinfo";
  } else {
    return "error";
  }
}

在content文件夾下創建userinfo.jsp文件,該頁面主要是文件的下載頁面,如下jsp代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ 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>文件下載</title>
</head>
<body>
<h3>文件下載</h3>
<a href="download?filename=${requestscope.user.image.originalfilename}" rel="external nofollow" >
  ${requestscope.user.image.originalfilename }
</a>
</body>
</html>

在瀏覽器中輸入如下url來測試應用: http://localhost:8088/fileupload/registerform

輸入用戶名并上傳剛才上傳的文件。如下圖:

SpringMVC實現文件的上傳和下載實例代碼

單擊“注冊”按鈕上傳文件,然后就會跳轉到下載頁面。如下圖:

SpringMVC實現文件的上傳和下載實例代碼

文件下載

上面我們通過案例演示了使用對象接收上傳文件,接下來,我們演示springmvc的下載文件。

文件下載比較簡單,直接在頁面給出了一個超鏈接,該鏈接href的屬性等于要下載文件的文件名,就可以實現文件下載了。但是如果該文件的文件名為中文文件名,在某些早起的瀏覽器上就會導致下載失敗;如果使用最新的firefox、chrome、opera、safari則都可以正常下載文件名為中文的文件了。

springmvc提供了一個responseentity類型,使用它可以很方便地定義返回的httpheaders和httpstatus。以下代碼演示文件的下載功能:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@requestmapping(value="/download")
   public responseentity<byte[]> download(httpservletrequest request,
       @requestparam("filename") string filename,
       model model)throws exception {
    //下載文件路徑
    string path = request.getservletcontext().getrealpath("/images/");
    file file = new file(path + file.separator + filename);
    httpheaders headers = new httpheaders();
    //下載顯示的文件名,解決中文名稱亂碼問題
    string downloadfielname = new string(filename.getbytes("utf-8"),"iso-8859-1");
    //通知瀏覽器以attachment(下載方式)打開圖片
    headers.setcontentdispositionformdata("attachment", downloadfielname);
    //application/octet-stream : 二進制流數據(最常見的文件下載)。
    headers.setcontenttype(mediatype.application_octet_stream);
    return new responseentity<byte[]>(fileutils.readfiletobytearray(file), 
        headers, httpstatus.created);
   }

download處理方法接收頁面傳遞的文件名filename后,使用apache commons fileupload組件的fileutils讀取項目的上傳文件,并將其構建成responseentity對象返回客戶端下載。

使用responseentity對象,可以很方便的定義返回的httpheaders和httpstatus。上面代碼中的mediatype,代表的是internet media type,即互聯網媒體類型,也叫做mime類型。在http協議消息頭中,使用content-type來表示具體請求中的媒體類型信息。httpstatus類型代表的是http協議中的狀態。有關mediatype和httpstatus類可以參考springmvc的api文檔。

點擊下載頁面的超鏈接,顯示文件正在下載,如下圖所示:

SpringMVC實現文件的上傳和下載實例代碼

單擊“瀏覽”按鈕,可以選擇下載文件的保存路徑,然后單擊“確定”按鈕,文件就會順利的下載并保存。

本文中的一些功能案例代碼和配置文件不是很完整,下面附上完整代碼:

fileuploadcontroller類完整的代碼如下:

?
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cn.edu.jseti.controller;
 
import java.io.file;
 
import javax.servlet.http.httpservletrequest;
 
import org.apache.commons.io.fileutils;
import cn.edu.jseti.domain.user;
import org.springframework.http.httpheaders;
import org.springframework.http.httpstatus;
import org.springframework.http.mediatype;
import org.springframework.http.responseentity;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.multipart.multipartfile;
/**
 * @author cody
 * @version v1.0
 */
@controller
public class fileuploadcontroller {
  @requestmapping(value="/{formname}")
   public string loginform(@pathvariable string formname) {
    // 動態跳轉頁面
    return formname;
  }
 
  //上傳文件會自動綁定到multipartfile中
   @requestmapping(value="/upload",method=requestmethod.post)
   public string upload(httpservletrequest request,
      @requestparam("description") string description,
      @requestparam("file") multipartfile file) throws exception {
 
    system.out.println(description);
    //如果文件不為空,寫入上傳路徑
    if(!file.isempty()) {
      //上傳文件路徑
      string path = request.getservletcontext().getrealpath("/images/");
      //上傳文件名
      string filename = file.getoriginalfilename();
      file filepath = new file(path,filename);
      //判斷路徑是否存在,如果不存在就創建一個
      if (!filepath.getparentfile().exists()) {
        filepath.getparentfile().mkdirs();
      }
      //將上傳文件保存到一個目標文件當中
      file.transferto(new file(path + file.separator + filename));
      return "success";
    } else {
      return "error";
    }
 
   }
 
   @requestmapping(value="/register")
   public string register(httpservletrequest request,
       @modelattribute user user,
       model model) throws exception {
    system.out.println(user.getusername());
    //如果文件不為空,寫入上傳路徑
    if(!user.getimage().isempty()) {
      //上傳文件路徑
      string path = request.getservletcontext().getrealpath("/images/");
      //上傳文件名
      string filename = user.getimage().getoriginalfilename();
      file filepath = new file(path,filename);
      //判斷路徑是否存在,如果不存在就創建一個
      if (!filepath.getparentfile().exists()) {
        filepath.getparentfile().mkdirs();
      }
      //將上傳文件保存到一個目標文件當中
      user.getimage().transferto(new file(path + file.separator + filename));
      //將用戶添加到model
      model.addattribute("user", user);
      return "userinfo";
    } else {
      return "error";
    }
  }
 
   @requestmapping(value="/download")
   public responseentity<byte[]> download(httpservletrequest request,
       @requestparam("filename") string filename,
       model model)throws exception {
    //下載文件路徑
    string path = request.getservletcontext().getrealpath("/images/");
    file file = new file(path + file.separator + filename);
    httpheaders headers = new httpheaders();
    //下載顯示的文件名,解決中文名稱亂碼問題
    string downloadfielname = new string(filename.getbytes("utf-8"),"iso-8859-1");
    //通知瀏覽器以attachment(下載方式)打開圖片
    headers.setcontentdispositionformdata("attachment", downloadfielname);
    //application/octet-stream : 二進制流數據(最常見的文件下載)。
    headers.setcontenttype(mediatype.application_octet_stream);
    return new responseentity<byte[]>(fileutils.readfiletobytearray(file), 
        headers, httpstatus.created);
   }
 
}

文件上傳成功的jsp代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>測試文件上傳</title>
</head>
<body>
  恭喜,您的上傳文件成功!
</body>
</html>

文件上傳失敗的jsp代碼:

?
1
2
3
4
5
6
7
8
9
10
11
<%@ 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>測試文件上傳</title>
</head>
<body>
  上傳文件失敗!
</body>
</html>

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
36
37
38
<?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>multipartfiletest</display-name>
 
 <!-- 定義spring mvc的前端控制器 -->
 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>
    org.springframework.web.servlet.dispatcherservlet
  </servlet-class>
  <init-param>
   <param-name>contextconfiglocation</param-name>
   <param-value>/web-inf/springmvc-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <!-- 讓spring mvc的前端控制器攔截所有請求 -->
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- 編碼過濾器 -->
 <filter>
    <filter-name>characterencodingfilter</filter-name>
    <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
 </filter>
  <filter-mapping>
    <filter-name>characterencodingfilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

ok,以上就是完整的使用springmvc框架實現了文件的上傳和下載。本中所有的案例都是本人親自測試,如有寫的不對,歡迎朋友們留言一起交流,謝謝!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家

原文鏈接:http://blog.csdn.net/qian_ch/article/details/69258465

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产亚洲精品久久久久久久久久 | 久久经典 | 国产一区二区久久精品 | 国产高潮失禁喷水爽到抽搐视频 | 蜜桃麻豆视频 | 久久亚洲一区二区三区成人国产 | 福利在线国产 | 精品成人av一区二区在线播放 | 嗯哈~不行好大h双性 | 第一区免费在线观看 | 日本一区免费看 | 亚洲第一男人天堂 | 一级免费在线视频 | 国产精品影视 | 欧美人一级淫片a免费播放 久久99精品久久久久久园产越南 | 欧美在线成人影院 | 99re色 | 素人视频在线观看免费 | 狠狠干五月天 | 欧美一级高清免费 | 欧美成人精品一区二区男人小说 | 草莓福利社区在线 | 久久草草影视免费网 | 成年人在线免费播放视频 | 日本免费一区二区三区四区 | 国产日产精品一区二区三区四区 | 成人h精品动漫一区二区三区 | 欧美精品久久久久久久久老牛影院 | 午夜精品在线视频 | 一级免费在线 | 成人在线视频一区 | 一级黄色毛片a | 色综合视频 | 久久国产精品久久久久久电车 | 亚洲乱码精品久久久久 | 黑人一区二区 | 精品国产一区二区三区四区阿崩 | 最新国产毛片 | 91一区二区三区久久久久国产乱 | 最新中文字幕在线视频 | 久久9999久久 |