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

服務(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 boot實現(xiàn)文件上傳功能

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

2021-05-11 15:28卡通人物nnn Java教程

這篇文章主要為大家詳細(xì)介紹了Spring boot實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了spring boot實現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下

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

?
1
2
3
4
5
<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
<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>

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

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
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
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就可以看到頁面了。

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

1.新增batchupload.jsp文件

?
1
2
3
4
5
6
7
8
9
10
<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
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)格上采取了隨性的方式,不建議大家模仿。

參考資料:multipartresolver實現(xiàn)文件上傳功能

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

1. multipartresolver也可以實現(xiàn)文件上傳功能。參考文章:原文鏈接:https://blog.csdn.net/woshizhangliang999/article/details/46711747

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品资源在线观看网站 | 亚洲羞羞裸色私人影院 | www.精品视频 | 99热精品在线观看 | 操破苍穹小说 | 日日综合 | 色噜噜 男人的天堂在线观看 | 日本人和黑人一级纶理片 | 失禁尿丝袜vk | 国产综合网站 | 国产精品成人免费 | 亚洲v日韩v欧美在线观看 | 臀控福利大臀的网站 | chinesemature老女人 | 亚洲欧美自偷自拍另类小说 | 亚州成人 | 国产日韩精品一区二区三区 | 国产精品刺激好大好爽视频 | 日本不卡在线视频高清免费 | 日韩毛片大全免费高清 | 亚洲精品老司机福利在线播放 | 女人肮脏的交易中文字幕未删减版 | 二次元美女互摸隐私互扒 | 九九热在线视频观看这里只有精品 | 成年性香蕉漫画在线观看 | 久久99热狠狠色一区二区 | 97色伦在线观看 | 欧美男同互吃gay老头 | 亚洲国产自拍在线 | 免费观看一区二区 | 人阁色第四影院在线观看 | 6080欧美一区二区三区四区 | 亚洲日韩精品欧美一区二区一 | 黑人好大好硬好深好爽想要h | 国产美女久久久久 | 精品视频在线免费播放 | 美女吃jj | 日本大片网 | 日本sss在线高清观看 | 撕开老师的丝袜白丝扒开粉嫩的小 | 精品欧美小视频在线观看 |