图片保存到SQL server数据库

        html
       <input id="uploaderInput" class="" accept="image/*" multiple="" type="file">

        var userImg = document.getElementById("uploaderInput").files
            var fd = new FormData();
            //判断是否选中了照片
            if (userImg.length > 0) {
                fd.append("userImg", userImg[0])//0(在选中多张时获取最新的那张)
            }
           
            $.ajax({
                url: "XGgerenziliao",//控制器路径
                type: "post",
                async: true,
                data: fd,//传参
                processData: false, // 不处理发送的数据,因为data值是Formdata对象,不需要对数据做处理
                contentType: false, // 不设置Content-type请求头
                success: function (retdata) {
                   //返回回调 retdata 是控制器的返回数据
                }
            })
        

控制器

  //声明一个byte[](字节型数组)来保存新增的图片
                byte[] imgFile = null;
                //判断图片文件是否为空
                if (userImg != null && userImg.ContentLength > 0)
                {
                    //初始化数组的长度,为节省空间,长度由实际上传的图片的长度决定
                    imgFile = new byte[userImg.ContentLength];
                    //读取该图片文件
                    //将图片转为流结束位置
                    //将流读取为byte[],参数:byte[],读取开始位置,读取字节数
                    userImg.InputStream.Read(imgFile, 0, userImg.ContentLength);
                    //imgFile 就是我们最终存入数据库中的数据
                }

数据库 保存为二进制数据
就会出现这样的数据

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,可以使用JDBC连接SQL Server数据库,并将图片以二进制数据的形式存储到数据库中。以下是一个简单的示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class SaveImageToSQLServer { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; FileInputStream fis = null; try { // 1. 加载SQL Server JDBC驱动程序 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2. 连接SQL Server数据库 String url = "jdbc:sqlserver://localhost:1433;databaseName=mydb"; String user = "sa"; String password = "mypassword"; conn = DriverManager.getConnection(url, user, password); // 3. 准备SQL语句 String sql = "INSERT INTO Images (name, data) VALUES (?,?)"; pstmt = conn.prepareStatement(sql); // 4. 读取图片文件 File imageFile = new File("C:\\image.jpg"); fis = new FileInputStream(imageFile); byte[] imageData = new byte[(int) imageFile.length()]; fis.read(imageData); // 5. 设置SQL语句参数 pstmt.setString(1, imageFile.getName()); pstmt.setBytes(2, imageData); // 6. 执行SQL语句 int rows = pstmt.executeUpdate(); System.out.println(rows + " rows affected"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 7. 关闭数据库连接和输入流 try { if (fis != null) { fis.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 以上代码将指定路径下的图片文件以二进制形式读取并插入到名为Images的数据库表中。其中,name列存储图片文件名,data列存储图片的二进制数据。可以根据实际需要修改表名、列名和SQL语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值