温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用ssm框架如何实现将上传的图片保存到本地数据库中

发布时间:2020-11-11 16:24:10 来源:亿速云 阅读:275 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关利用ssm框架如何实现将上传的图片保存到本地数据库中,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1、前台部分

<%@ 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

@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

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

<update id="base64Test" parameterType="String"> 
  update t_user set U_ABOUT = #{base64} where u_name = '971171444' 
 </update> 

看完上述内容,你们对利用ssm框架如何实现将上传的图片保存到本地数据库中有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI