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

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

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

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - SpringMVC上傳文件的兩種方法

SpringMVC上傳文件的兩種方法

2021-02-07 16:50殘缺的孤獨(dú) Java教程

這篇文章主要為大家詳細(xì)介紹了SpringMVC上傳文件的兩種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在該示例中,闡述了SpringMVC如何上傳文件
1、上傳頁(yè)面upload.jsp

?
1
2
3
4
5
6
<body>
  <form action="/TestSpringMVC3/data/uploadfile" enctype="multipart/form-data" method="post">
    file:<input type="file" name="file"><br>
    <input type="submit" value="upload file">
  </form>
</body>

2、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
<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  <!--
    使Spring支持自動(dòng)檢測(cè)組件,如注解的Controller
  -->
  <context:component-scan base-package="cn.com.yy.controller"/>
   
  <!-- 開(kāi)啟注解配置 -->
  <mvc:annotation-driven/>
     
  <!-- 支持JSP JSTL的解析器 -->
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/"/>
    <property name="suffix" value=".jsp"/>
   </bean>
    
   <!-- 配置文件上傳解析器 -->
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8"/>
    <property name="maxUploadSize" value="10485760000"/>
    <property name="maxInMemorySize" value="40960"/>
   </bean>
</beans>

主要是添加了文件上傳的解析器,配置了默認(rèn)編碼,最大的上傳大小以及緩存大小等參數(shù)。

3、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
42
43
44
45
46
47
package cn.com.yy.controller;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
@Controller
@RequestMapping("/data")
public class FileUploadController {
   
  /**
   * method1:通過(guò)參數(shù)CommonsMultipartFile進(jìn)行解析
   * @RequestParam("file")中的file對(duì)應(yīng)于upload.jsp中的file類(lèi)型的name對(duì)應(yīng)的名稱(chēng)
   * @param file
   * @return
   * @throws IOException
   */
  @RequestMapping(value="/uploadfile")
  public String upload1(@RequestParam("file") CommonsMultipartFile file) throws IOException{
    //獲取文件名稱(chēng)
    String fileName = file.getOriginalFilename();
    //寫(xiě)入本地磁盤(pán)
    InputStream is = file.getInputStream();
    byte[] bs = new byte[1024];
    int len;
    OutputStream os = new FileOutputStream(new File("D:/temp/" + fileName));
    while ((len = is.read(bs)) != -1) {
      os.write(bs, 0, len);
    }
    os.close();
    is.close();
    return "upload_success";
  }
   
  @RequestMapping("/upload")
  public String toPage(){
    return "upload";
  }
}

4、返回頁(yè)面upload_success.jsp

?
1
2
3
<body>
  upload file success!!
</body>

5、測(cè)試

訪問(wèn)  http://localhost:8080/TestSpringMVC3/data/upload  跳轉(zhuǎn)到上傳頁(yè)面

SpringMVC上傳文件的兩種方法    

選擇文件上傳

SpringMVC上傳文件的兩種方法                    

點(diǎn)擊上傳會(huì)跳轉(zhuǎn)到上傳成功頁(yè)面。

上述方法只是簡(jiǎn)單的講解了SpringMVC如何上傳文件。    

第二種方法:使用SpringMVC封裝的方法進(jì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
/**
   * 使用SpringMVC封裝好的方法進(jìn)行文件上傳
   * @param request
   * @param response
   * @throws IllegalStateException
   * @throws IOException
   */
  @RequestMapping("/uploadfile2")
  public void upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
    //獲取解析器
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
    //判斷是否是文件
    if(resolver.isMultipart(request)){
      //進(jìn)行轉(zhuǎn)換
      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
      //獲取所有文件名稱(chēng)
      Iterator<String> it = multiRequest.getFileNames();
      while(it.hasNext()){
        //根據(jù)文件名稱(chēng)取文件
        MultipartFile file = multiRequest.getFile(it.next());
        String fileName = file.getOriginalFilename();
        String localPath = "D:/temp/" + fileName;
        File newFile = new File(localPath);
        //上傳的文件寫(xiě)入到指定的文件中
        file.transferTo(newFile);
      }
    }
  }

該方法上傳文件效率更優(yōu)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/yyywyr/article/details/44571353

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 玖玖视频精品 | 国产亚洲精品网站 | 欧美一区二区三区四区五区动图 | 高清在线观看av | 精品亚洲一区二区三区 | 欧美一级美国一级 | 9999久久| 99爱视频在线 | 蜜桃免费在线 | 久久精品国产99久久6动漫亮点 | 久草在线观看福利 | 欧美国产日韩在线 | 97中文字幕第一一一页 | 中文字幕在线第二页 | 久久欧美亚洲另类专区91大神 | 免费a级毛片大学生免费观看 | 天天操综 | 久久久精品精品 | 女人叉开腿让男人桶 | 在线观看视频毛片 | 中文字幕伦乱 | 欧美黑大粗硬毛片视频 | 性视频久久 | 性插视频| 最污网站 | 欧美精品网址 | av免费不卡国产观看 | 欧美a视频在线观看 | 欧美成人做爰高潮片免费视频 | 91在线看黄 | 精品一区二区三区日本 | 国产成人免费高清激情视频 | 中文字幕在线资源 | 一级片国产片 | 久久色伦理资源站 | 日本最新免费二区三区 | 激情av在线 | 久久精国 | 五月婷婷天堂 | 日本在线免费观看视频 | 国产精品久久久久久久亚洲按摩 |