本文實例為大家分享了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