一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - SpringMVC 通過commons-fileupload實(shí)現(xiàn)文件上傳功能

SpringMVC 通過commons-fileupload實(shí)現(xiàn)文件上傳功能

2021-08-02 10:56jiawei3998 Java教程

這篇文章主要介紹了SpringMVC 通過commons-fileupload實(shí)現(xiàn)文件上傳,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

配置

 

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">
 <!--注冊(cè)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"不要寫錯(cuò)

?
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">
 
 <!--配置自動(dòng)掃描controller包-->
 <context:component-scan base-package="com.pro.controller"/>
 <!--配置靜態(tài)資源過濾-->
 <mvc:default-servlet-handler/>
 <!--配置注解驅(qū)動(dòng)-->
 <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">
  <!--設(shè)置請(qǐng)求的編碼格式, 必須和pageencoding的屬性一致, 以便正確讀取表單的值, 默認(rèn)為iso-8859-1-->
  <property name="defaultencoding" value="utf-8"/>
  <!--上傳文件的大小限制, 單位為字節(jié) (10485760 = 10m)-->
  <property name="maxuploadsize" value="10485760"/>
  <property name="maxinmemorysize" value="40960"/>
 </bean>
</beans>

文件上傳 controller

 

上傳實(shí)現(xiàn)一

?
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 {
 
    //上傳路徑保存設(shè)置
    string path = request.getservletcontext().getrealpath("/upload");
    file realpath = new file(path);
    if (!realpath.exists()){
      realpath.mkdir();
    }
    //上傳文件地址
    system.out.println("上傳文件保存地址 --> "+realpath);
 
    //通過commonsmultipartfile的方法直接寫文件(注意這個(gè)時(shí)候)
    file.transferto(new file(realpath +"/"+ file.getoriginalfilename()));
 
    map<object, object> hashmap = new hashmap<>();
    hashmap.put("code", 0);
    hashmap.put("msg", "上傳成功");
 
    return hashmap;
  }
}

上傳實(shí)現(xiàn)二

這里的文件名稱沒有使用 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 對(duì)象
  // 批量上傳把 commonsmultipartfile 改為數(shù)組即可
  @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);
 
 
    // 設(shè)置文件的保存位置
    string path = request.getservletcontext().getrealpath("/upload");
    // 判斷路徑是否存在
    file realpath = new file(path);
    if (!realpath.exists()) {
      // 如果不存在就創(chuàng)建
      realpath.mkdir();
    }
 
    system.out.println("文件保存路徑 --> " + realpath);
 
    // 獲取文件輸入流
    inputstream is = file.getinputstream();
    // 獲取文件輸出流
    fileoutputstream os = new fileoutputstream(new file(realpath, uploadfilename));
 
    // 緩沖區(qū)讀寫文件
    byte[] buffer = new byte[1024];
    int len;
    while ((len = is.read(buffer)) != -1) {
      os.write(buffer, 0, len);
      os.flush();
    }
 
    // 關(guān)閉流
    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="上傳實(shí)現(xiàn)一">
  </form>
  
  
  <form enctype="multipart/form-data" action="${pagecontext.request.contextpath}/upload" method="post">
   <input type="file" name="file">
   <input type="submit" value="上傳實(shí)現(xiàn)二">
  </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
<!--導(dǎo)入依賴-->
<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導(dǎo)入高版本的-->
  <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表達(dá)式-->
  <dependency>
    <groupid>javax.servlet</groupid>
    <artifactid>jstl</artifactid>
    <version>1.2</version>
  </dependency>
</dependencies>

到此這篇關(guān)于springmvc 通過commons-fileupload實(shí)現(xiàn)文件上傳的文章就介紹到這了,更多相關(guān)springmvc 實(shí)現(xiàn)文件上傳內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩免费视频播播 | 双性小说肉 | 亚洲国产成人在线 | 男人都懂www深夜免费网站 | 3d动漫免费| 毛片网站免费观看 | 亚洲日韩精品欧美一区二区 | 天堂网www中文天堂在线 | 99久久国产综合精麻豆 | 美女福利视频网站 | 国产成人免费在线观看 | 天天做天天爽天天谢 | 免费超级乱淫视频播放性 | 波多野结中文字幕在线69视频 | 亚洲国产综合精品 | 男人的天堂久久精品激情a 男人的天堂va | 日本一区二区视频免费播放 | 无套内谢大学生A片 | 四虎黄色网址 | 色综色天天综合网 | 久久黄视频 | 日本春菜花在线中文字幕 | 亚洲午夜精品久久久久 | 91视频免费观看网站 | 欧美影院天天5g天天爽 | 日本人成年视频在线观看 | 男人狂躁女人下半身 | 色婷婷综合久久久中文字幕 | 欧美成人momandson | 校草让我脱了内裤给全班看 | av中文字幕网免费观看 | 亚洲国产欧美在线人成aaaa20 | 青青成人 | 动漫美女胸被狂揉扒开吃奶动态图 | 好大好硬好深好爽想要吃奶 | 国产一区二区视频在线播放 | 99视频观看 | 高h视频免费观看 | 精品亚洲一区二区三区在线播放 | 亚洲性色永久网址 | yellow视频免费观看播放 |