栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

1、前台部分

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
 
 
 
   
  Title 
   
 
 
 


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

 
 
  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 t_user set U_about = #{base64} where u_name = '971171444' 
  

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/147344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号