好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于SpringBoot实现图片上传与显示

本文为大家分享了springboot实现 图片上传 与显示的具体代码,供大家参考,具体内容如下

springboot实现图片上传与显示: demo地址

效果图预览

思路

一般情况下都是将用户上传的图片放到服务器的某个文件夹中,然后将图片在服务器中的路径存入数据库。本demo也是这样做的。 由于用户自己保存的图片文件名可能跟其他用户同名造成冲突,因此本demo选择了使用uuid来生成随机的文件名解决冲突。 但是本demo不涉及任何有关数据库的操作,便于演示,就用原来的文件名。

步骤

pom相关依赖

基于spring boot当然是继承了spring boot这不用多说 具体依赖,主要是freemarker相关依赖为了展现页面,习惯用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相关配置

除了视图模板相关的配置,重点是要配置一下文件上传的内存大小和文件上传路径

?

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

#设置面板后缀

spring.freemarker.suffix=.ftl

 

# 设置单个文件最大内存

multipart.maxfilesize=50mb

# 设置所有文件最大内存

multipart.maxrequestsize=50mb

# 自定义文件上传路径

web.upload-path=e:/develop/files/photos/

生成文件名

不准备生成文件名的可以略过此步骤

?

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;

 

   /**

    * 跳转到文件上传页面

    * @return

    */

   @requestmapping ( "test" )

   public string toupload(){

     return "test" ;

   }

 

   /**

    *

    * @param file 要上传的文件

    * @return

    */

   @requestmapping ( "fileupload" )

   public string upload( @requestparam ( "filename" ) multipartfile file, map<string, object> map){

 

     // 要上传的目标文件存放路径

     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 {

       // 由于是读取本机的文件,file是一定要加上的, path是在application配置文件中的路径

       return responseentity.ok(resourceloader.getresource( "file:" + path + filename));

     } catch (exception e) {

       return responseentity.notfound().build();

     }

   }

 

}

页面

页面主要是from表单和下面的 <img src="/show?filename=${filename}" /> ,其余都是细节。

?

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发请求给controller,否则直接跳转是乱码-->

<# if filename??>

<img src= "/show?filename=${filename}" style= "width: 200px" />

<# else >

<img src= "/show" style= "width: 100px" />

</# if >

</body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_32106647/article/details/80519262

查看更多关于基于SpringBoot实现图片上传与显示的详细内容...

  阅读:51次