本文為大家分享了springboot實(shí)現(xiàn)圖片上傳與顯示的具體代碼,供大家參考,具體內(nèi)容如下
springboot實(shí)現(xiàn)圖片上傳與顯示:demo地址
效果圖預(yù)覽
思路
- 一般情況下都是將用戶上傳的圖片放到服務(wù)器的某個文件夾中,然后將圖片在服務(wù)器中的路徑存入數(shù)據(jù)庫。本demo也是這樣做的。
- 由于用戶自己保存的圖片文件名可能跟其他用戶同名造成沖突,因此本demo選擇了使用uuid來生成隨機(jī)的文件名解決沖突。
- 但是本demo不涉及任何有關(guān)數(shù)據(jù)庫的操作,便于演示,就用原來的文件名。
步驟
pom相關(guān)依賴
- 基于spring boot當(dāng)然是繼承了spring boot這不用多說
- 具體依賴,主要是freemarker相關(guān)依賴為了展現(xiàn)頁面,習(xí)慣用jsp也可以添加jsp的依賴,只是為了展示頁面,這個不重要。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<dependencies> <!--freemarker模板視圖依賴--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-freemarker</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> |
application.properties相關(guān)配置
除了視圖模板相關(guān)的配置,重點(diǎn)是要配置一下文件上傳的內(nèi)存大小和文件上傳路徑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
server.port= 8102 ### freemarker 配置 spring.freemarker.allow-request-override= false #enable template caching.啟用模板緩存。 spring.freemarker.cache= false spring.freemarker.check-template-location= true spring.freemarker.charset=utf- 8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes= false spring.freemarker.expose-session-attributes= false spring.freemarker.expose-spring-macro-helpers= false #設(shè)置面板后綴 spring.freemarker.suffix=.ftl # 設(shè)置單個文件最大內(nèi)存 multipart.maxfilesize=50mb # 設(shè)置所有文件最大內(nèi)存 multipart.maxrequestsize=50mb # 自定義文件上傳路徑 web.upload-path=e:/develop/files/photos/ |
生成文件名
不準(zhǔn)備生成文件名的可以略過此步驟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.wu.demo.fileupload.demo.util; public class filenameutils { /** * 獲取文件后綴 * @param filename * @return */ public static string getsuffix(string filename){ return filename.substring(filename.lastindexof( "." )); } /** * 生成新的文件名 * @param fileoriginname 源文件名 * @return */ public static string getfilename(string fileoriginname){ return uuidutils.getuuid() + filenameutils.getsuffix(fileoriginname); } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
import java.util.uuid; /** * 生成文件名 */ public class uuidutils { public static string getuuid(){ return uuid.randomuuid().tostring().replace( "-" , "" ); } } |
文件上傳工具類
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
|
package com.wu.demo.fileupload.demo.util; import org.springframework.web.multipart.multipartfile; import java.io.file; import java.io.ioexception; /** * 文件上傳工具包 */ public class fileutils { /** * * @param file 文件 * @param path 文件存放路徑 * @param filename 源文件名 * @return */ public static boolean upload(multipartfile file, string path, string filename){ // 生成新的文件名 //string realpath = path + "/" + filenameutils.getfilename(filename); //使用原文件名 string realpath = path + "/" + filename; file dest = new file(realpath); //判斷文件父目錄是否存在 if (!dest.getparentfile().exists()){ dest.getparentfile().mkdir(); } try { //保存文件 file.transferto(dest); return true ; } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); return false ; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); return false ; } } } |
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
78
79
80
81
|
package com.wu.demo.fileupload.demo.controller; import com.wu.demo.fileupload.demo.util.fileutils; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.core.io.resourceloader; import org.springframework.http.responseentity; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.multipart.multipartfile; import javax.servlet.http.httpservletresponse; import java.util.map; @controller public class testcontroller { private final resourceloader resourceloader; @autowired public testcontroller(resourceloader resourceloader) { this .resourceloader = resourceloader; } @value ( "${web.upload-path}" ) private string path; /** * 跳轉(zhuǎn)到文件上傳頁面 * @return */ @requestmapping ( "test" ) public string toupload(){ return "test" ; } /** * * @param file 要上傳的文件 * @return */ @requestmapping ( "fileupload" ) public string upload( @requestparam ( "filename" ) multipartfile file, map<string, object> map){ // 要上傳的目標(biāo)文件存放路徑 string localpath = "e:/develop/files/photos" ; // 上傳成功或者失敗的提示 string msg = "" ; if (fileutils.upload(file, localpath, file.getoriginalfilename())){ // 上傳成功,給出頁面提示 msg = "上傳成功!" ; } else { msg = "上傳失敗!" ; } // 顯示圖片 map.put( "msg" , msg); map.put( "filename" , file.getoriginalfilename()); return "forward:/test" ; } /** * 顯示單張圖片 * @return */ @requestmapping ( "show" ) public responseentity showphotos(string filename){ try { // 由于是讀取本機(jī)的文件,file是一定要加上的, path是在application配置文件中的路徑 return responseentity.ok(resourceloader.getresource( "file:" + path + filename)); } catch (exception e) { return responseentity.notfound().build(); } } } |
頁面
頁面主要是from表單和下面的 <img src="/show?filename=${filename}" /> ,其余都是細(xì)節(jié)。
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
|
<!doctype html> <head> <meta charset= "utf-8" /> <title>圖片上傳demo</title> </head> <body> <h1 >圖片上傳demo</h1> <form action= "fileupload" method= "post" enctype= "multipart/form-data" > <p>選擇文件: <input type= "file" name= "filename" /></p> <p><input type= "submit" value= "提交" /></p> </form> <#--判斷是否上傳文件--> <# if msg??> <span>${msg}</span><br> <# else > <span>${msg!( "文件未上傳" )}</span><br> </# if > <#--顯示圖片,一定要在img中的src發(fā)請求給controller,否則直接跳轉(zhuǎn)是亂碼--> <# if filename??> <img src= "/show?filename=${filename}" style= "width: 200px" /> <# else > <img src= "/show" style= "width: 100px" /> </# if > </body> </html> |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_32106647/article/details/80519262