ssm框架上传图片保存到本地和数据库示例

2025-05-29 0 96

本文介绍了ssm框架上传图片保存到本地和数据库示例,主要使用了Spring+SpringMVC+MyBatis框架,实现了ssm框架上传图片的实例,具体如下:

1、前台部分

?

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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Title</title>

<script src="resources/jquery/jquery-3.0.0.js"></script>

</head>

<body>

<img id="image"src=""/>

<br/>

<input type="file"onchange="selectImage(this);"/>

<br/>

<input type="button"onclick="uploadImage();"value="提交"/>

<script>

var image = '';

function selectImage(file){

if(!file.files || !file.files[0]){

return;

}

var reader = new FileReader();

reader.onload = function(evt){

document.getElementById('image').src = evt.target.result;

image = evt.target.result;

}

reader.readAsDataURL(file.files[0]);

}

function uploadImage(){

image = JSON.stringify(image)

$.ajax({

type:'POST',

url: '/blog/test',

data: {base64: image

},

async: false,

dataType: 'json',

success: function(data){

alert(data.success)

if(data.success){

alert('上传成功');

}else{

alert('上传失败');

}

},

error: function(err){

alert('网络故障');

}

});

}

</script>

<script src="jquery-1.11.1.min.js"></script>

</body>

</html>

2、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
@Inject

private IUserService userService;

@RequestMapping(value="test")

@ResponseBody

public ConsoleResult test(String base64){

// 自定义返回前台数据格式

ConsoleResult res = new ConsoleResult();

// 去掉base64数据头部data:image/png;base64,和尾部的” " “

String[] ww= base64.split(",");

base64 = ww[1];

String[] aa = base64.split("\\"");

base64 = aa[0];

try {

// 将图片插入数据库

userService.base64test(base64);

// 图片保存到本地

String path = "D:/asdfasdf.jpg";

Base64File file = new Base64File();

file.decoderBase64File(base64, path);

// 成功标识

res.setStatus(ConsoleResult.successStatus);

} catch (Exception e) {

res.setStatus(ConsoleResult.faultStatus);

}

return res;

}

3、base64

?

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
/**

* 将base64字符解码保存文件

*

* @param base64Code

* @param targetPath

* @throws Exception

*/

public static void decoderBase64File(String base64Code, String targetPath) {

byte[] buffer;

FileOutputStream out = null;

try {

buffer = new BASE64Decoder().decodeBuffer(base64Code);

out = new FileOutputStream(targetPath);

out.write(buffer);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

4、mapper.xml

?

1

2

3
<update id="base64Test" parameterType="String">

update t_user set U_ABOUT = #{base64} where u_name = '971171444'

</update>

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

原文链接:http://blog.csdn.net/xiaowei5246879/article/details/54962653

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ssm框架上传图片保存到本地和数据库示例 https://www.kuaiidc.com/118462.html

相关文章

发表评论
暂无评论