国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

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

Spring實(shí)現(xiàn)文件上傳功能

2020-08-01 15:35Rollen Holt Java教程

本篇文章主要介紹了Spring實(shí)現(xiàn)文件上傳功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本篇文章,我們要來做一個Spring的文件上傳功能:

1. 創(chuàng)建一個Maven的web工程,然后配置pom.xml文件,增加依賴:

?
1
2
3
4
5
6
7
8
9
<dependency>
 
  <groupId>org.springframework.boot</groupId>
 
  <artifactId>spring-boot-starter-web</artifactId>
 
  <version>1.0.2.RELEASE</version>
 
</dependency>

2.在webapp目錄下的index.jsp文件中輸入一個表單:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
 
<body>
 
<form method="POST" enctype="multipart/form-data"
 
   action="/upload">
 
  File to upload: <input type="file" name="file"><br /> Name: <input
 
    type="text" name="name"><br /> <br /> <input type="submit"
 
                           value="Upload"> Press here to upload the file!
 
</form>
 
</body>
 
</html>

這個表單就是我們模擬的上傳頁面。

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
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
import java.io.BufferedOutputStream;
 
import java.io.File;
 
import java.io.FileOutputStream;
 
 
 
import org.springframework.stereotype.Controller;
 
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.bind.annotation.ResponseBody;
 
import org.springframework.web.multipart.MultipartFile;
 
 
 
@Controller
 
public class FileUploadController {
 
 
 
  @RequestMapping(value="/upload", method=RequestMethod.GET)
 
  public @ResponseBody String provideUploadInfo() {
 
    return "You can upload a file by posting to this same URL.";
 
  }
 
 
 
  @RequestMapping(value="/upload", method=RequestMethod.POST)
 
  public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
 
      @RequestParam("file") MultipartFile file){
 
    if (!file.isEmpty()) {
 
      try {
 
        byte[] bytes = file.getBytes();
 
        BufferedOutputStream stream =
 
            new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
 
        stream.write(bytes);
 
        stream.close();
 
        return "You successfully uploaded " + name + " into " + name + "-uploaded !";
 
      } catch (Exception e) {
 
        return "You failed to upload " + name + " => " + e.getMessage();
 
      }
 
    } else {
 
      return "You failed to upload " + name + " because the file was empty.";
 
    }
 
  }
 
 
 
}

4. 然后我們對上傳的文件做一些限制,同時編寫main方法來啟動這個web :

?
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
import org.springframework.boot.SpringApplication;
 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 
import org.springframework.boot.context.embedded.MultiPartConfigFactory;
 
import org.springframework.context.annotation.Bean;
 
import org.springframework.context.annotation.ComponentScan;
 
import org.springframework.context.annotation.Configuration;
 
 
 
import javax.servlet.MultipartConfigElement;
 
 
 
@Configuration
 
@ComponentScan
 
@EnableAutoConfiguration
 
public class Application {
 
 
 
  @Bean
 
  public MultipartConfigElement multipartConfigElement() {
 
    MultiPartConfigFactory factory = new MultiPartConfigFactory();
 
    factory.setMaxFileSize("128KB");
 
    factory.setMaxRequestSize("128KB");
 
    return factory.createMultipartConfig();
 
  }
 
 
 
  public static void main(String[] args) {
 
    SpringApplication.run(Application.class, args);
 
  }
 
}

5. 然后訪問http://localhost:8080/upload就可以看到頁面了。

上面的例子是實(shí)現(xiàn)的是單個文件上傳的功能,假定我們現(xiàn)在要實(shí)現(xiàn)文件批量上傳的功能的話,我們只需要簡單的修改一下上面的代碼就行,考慮到篇幅的問題,下面只是貼出和上面不同的代碼,沒有貼出的說明和上面一樣。:

1.  新增batchUpload.jsp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
 
<body>
 
<form method="POST" enctype="multipart/form-data"
 
   action="/batch/upload">
 
  File to upload: <input type="file" name="file"><br />
 
  File to upload: <input type="file" name="file"><br />
 
  <input type="submit" value="Upload"> Press here to upload the file!
 
</form>
 
</body>
 
</html>

2. 新增BatchFileUploadController.java文件:

?
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
import org.springframework.stereotype.Controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
 
import org.springframework.web.bind.annotation.RequestMethod;
 
import org.springframework.web.bind.annotation.ResponseBody;
 
import org.springframework.web.multipart.MultipartFile;
 
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
 
 
import javax.servlet.http.HttpServletRequest;
 
import java.io.BufferedOutputStream;
 
import java.io.File;
 
import java.io.FileOutputStream;
 
import java.util.List;
 
 
 
/**
 
 * Created by wenchao.ren on 2014/4/26.
 
 */
 
 
 
@Controller
 
public class BatchFileUploadController {
 
 
 
  @RequestMapping(value="/batch/upload", method= RequestMethod.POST)
 
  public @ResponseBody
 
  String handleFileUpload(HttpServletRequest request){
 
    List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
 
    for (int i =0; i< files.size(); ++i) {
 
      MultipartFile file = files.get(i);
 
      String name = file.getName();
 
      if (!file.isEmpty()) {
 
        try {
 
          byte[] bytes = file.getBytes();
 
          BufferedOutputStream stream =
 
              new BufferedOutputStream(new FileOutputStream(new File(name + i)));
 
          stream.write(bytes);
 
          stream.close();
 
        } catch (Exception e) {
 
          return "You failed to upload " + name + " => " + e.getMessage();
 
        }
 
      } else {
 
        return "You failed to upload " + name + " because the file was empty.";
 
      }
 
    }
 
    return "upload successful";
 
  }
 
}

這樣一個簡單的批量上傳文件的功能就ok了,是不是很簡單啊。 

注意:上面的代碼只是為了演示而已,所以編碼風(fēng)格上采取了隨性的方式,不建議大家模仿。

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

原文鏈接:http://www.cnblogs.com/rollenholt/p/3693087.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产高清视频一区二区 | 国产欧美在线观看 | 国产精品久久久久久久久久久久冷 | 99久久婷婷国产精品综合 | 男人天堂网站 | 夜夜av | 欧洲一级视频 | 久久一区| 中文字幕三区 | 精品在线一区二区三区 | av免费观看网站 | 色婷婷一区 | 亚洲欧美国产另类 | 亚洲一区二区三区在线播放 | av在线一区二区三区 | 欧美福利一区 | 亚洲伦理 | 国产精品久久久久久久久久三级 | 精品久久精品 | 欧美一区久久 | 亚洲视频免费在线观看 | 欧美日韩久久精品 | 黄色小视频在线观看 | 久久在线视频 | 国产精品久久久久免费 | 国产精品视屏 | 久久综合久色欧美综合狠狠 | 久久久久一区 | 婷婷精品久久久久久久久久不卡 | 天天综合网久久综合网 | 欧美日韩亚洲视频 | 久久影音先锋 | 亚洲不卡视频 | 成人免费激情视频 | 国产黄色网址在线观看 | 日韩有码在线播放 | 日韩一区二区三区在线视频 | 日韩欧美一级精品久久 | 日韩视频―中文字幕 | 三级国产网站 | 午夜网址 |