最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)數(shù)據(jù)庫圖片上傳功能詳解

 更新時(shí)間:2025年03月14日 10:18:40   作者:Blue的成長(zhǎng)日記  
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)數(shù)據(jù)庫圖片上傳功能,包含從數(shù)據(jù)庫拿圖片傳遞前端渲染,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1、前言

我們使用數(shù)據(jù)庫經(jīng)常傳遞字符串、數(shù)字,但是很少在數(shù)據(jù)庫存儲(chǔ)圖片、Word文件,我也去網(wǎng)上找了找其他人的文章,只能說這類型的少的可憐,不是收費(fèi),就是講的亂七八糟。既然如此,那么我將為需要這方面知識(shí)點(diǎn)的朋友寫下這篇文章。本篇文章我們從以下幾個(gè)方面:

1、數(shù)據(jù)庫搭建

2、后端實(shí)現(xiàn),將圖片存儲(chǔ)進(jìn)數(shù)據(jù)庫

3、后端實(shí)現(xiàn),從數(shù)據(jù)庫取出圖片給前端

4、前端拿到后端傳遞的json信息渲染到網(wǎng)頁

廢話不多說,直接開始!

2、數(shù)據(jù)庫搭建 

本次數(shù)據(jù)庫我們選擇比較經(jīng)典的Mysql(只是因?yàn)槲抑粫?huì)這個(gè)),mysql提供一個(gè)字段類型叫做

blob,對(duì)于這個(gè)字段類型,我就不過多詳細(xì)敘述,csdn博客上有,各位可以去看看。

建表語句:

create table test2(
    id int auto_increment primary key ,
    name varchar(100) comment '名稱',
    photo blob comment '照片'
)

 3、后端實(shí)現(xiàn)將圖片存儲(chǔ)進(jìn)數(shù)據(jù)庫

思想:

實(shí)際上我們可以通過字節(jié)流的形式,將圖片轉(zhuǎn)成二進(jìn)制,然后將其保存在數(shù)據(jù)庫里面。我們按照以下步驟:

1、找到圖片位置

2、圖片轉(zhuǎn)為Fileinputstream流

3、存儲(chǔ)數(shù)據(jù)庫

找到圖片位置(如下圖操作)

圖片轉(zhuǎn)為Fileinputstream流的工具類(可直接copy)

package com.example.jishedemo2.img;
 
import java.io.*;
 
public class imgeuntil {
    /**
     * @author Administrator
     *
     */
 
 
        // 讀取本地圖片獲取輸入流
        public static FileInputStream readImage(String path) throws IOException {
            return new FileInputStream(new File(path));
        }
 
        // 讀取表中圖片獲取輸出流
        public static void readBin2Image(InputStream in, String targetPath) {
            File file = new File(targetPath);
            String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
            if (!file.exists()) {
                new File(path).mkdir();
            }
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
                int len = 0;
                byte[] buf = new byte[1024];
                while ((len = in.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }
                fos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != fos) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

存儲(chǔ)數(shù)據(jù)庫

mapper層:

package com.example.jishedemo2.img;
 
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
 
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
 
@Mapper
public interface imagemapper {
 
 @Insert("insert into test2 values(null,#{name},#{photo})")
 void inser(String name,  FileInputStream photo);
 
}

service層:

package com.example.jishedemo2.img;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.FileInputStream;
import java.util.List;
 
@Service
public class imageservice implements imge{
 
    @Autowired
    private imagemapper imagemapper;
 
    @Override
    public void inser(String name, FileInputStream file) {
        imagemapper.inser(name,file);
    }
 
}

control層:

package com.example.jishedemo2.img;
 
import com.example.jishedemo2.dao.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.util.List;
 
@RestController
public class imgedemo {
    @Autowired
    private imageservice imageservice;
 
    // 將圖片插入數(shù)據(jù)庫
    @PostMapping("test3")
    public  void readImage2DB() throws IOException {
        String path = "D:\\wsstext.png";
        try {
            FileInputStream in = null;
            in = imgeuntil.readImage(path);
            imageservice.inser("測(cè)試",in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
   
}

沒什么好說的,如果你不會(huì)javaweb,這邊建議先去把javaweb學(xué)了。

4、后端實(shí)現(xiàn)從數(shù)據(jù)庫取出圖片給前端

(可直接看這個(gè),這個(gè)給的是所有代碼)

這一步,多了一點(diǎn),我們需要寫一個(gè)類與數(shù)據(jù)庫的表字段統(tǒng)一(dao層)

dao層:

package com.example.jishedemo2.img;
 
import java.io.FileInputStream;
import java.io.InputStream;
 
public class photo {
    int id;
    String name;
    byte[] photo;
 
    public photo() {
    }
 
    public photo(int id, String name, byte[] photo) {
        this.id = id;
        this.name = name;
        this.photo = photo;
    }
 
    /**
     * 獲取
     * @return id
     */
    public int getId() {
        return id;
    }
 
    /**
     * 設(shè)置
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }
 
    /**
     * 獲取
     * @return name
     */
    public String getName() {
        return name;
    }
 
    /**
     * 設(shè)置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
 
    /**
     * 獲取
     * @return photo
     */
    public byte[] getPhoto() {
        return photo;
    }
 
    /**
     * 設(shè)置
     * @param photo
     */
    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }
 
    public String toString() {
        return "photo{id = " + id + ", name = " + name + ", photo = " + photo + "}";
    }
}

mapper層:

package com.example.jishedemo2.img;
 
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
 
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
 
@Mapper
public interface imagemapper {
 
 @Insert("insert into test2 values(null,#{name},#{photo})")
 void inser(String name,  FileInputStream photo);
 
 @Select("select * from test2")
 List<photo> select();
 
 
}

service層:

package com.example.jishedemo2.img;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.FileInputStream;
import java.util.List;
 
@Service
public class imageservice implements imge{
 
    @Autowired
    private imagemapper imagemapper;
 
    @Override
    public void inser(String name, FileInputStream file) {
        imagemapper.inser(name,file);
    }
 
    @Override
    public List<photo> select() {
        return imagemapper.select();
    }
}

control層:

package com.example.jishedemo2.img;
 
import com.example.jishedemo2.dao.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.util.List;
 
@RestController
public class imgedemo {
    @Autowired
    private imageservice imageservice;
 
    // 將圖片插入數(shù)據(jù)庫
    @PostMapping("test3")
    public  void readImage2DB() throws IOException {
        String path = "D:\\wsstext.png";
        PreparedStatement ps = null;
        try {
            FileInputStream in = null;
            in = imgeuntil.readImage(path);
            imageservice.inser("測(cè)試",in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    //傳數(shù)據(jù)
    @PostMapping("test4")
    public Result select(){
 
        List<photo> photos = imageservice.select();
 
        return Result.success(photos);
    }
 
 
}

前端拿到效果: 

5、前端拿到后端傳遞的json信息渲染到網(wǎng)頁

 對(duì)于新手前端拿到photo,可能會(huì)很蒙蔽不知道這個(gè)是什么,我這里簡(jiǎn)要說一下:

解釋 :

這段文本是一個(gè)HTML (HyperText Markup Language) 編碼的字符串,它嵌入了一個(gè)Base64編碼的圖像數(shù)據(jù)(以data:image/png;base64,開頭的部分,但在這個(gè)示例中被截?cái)嗔耍艘恍〤SS (Cascading Style Sheets) 樣式和JavaScript(雖然直接看起來并不包含JavaScript代碼,但可能是在某處被引用或嵌入的)。

具體來說,這個(gè)字符串包含以下幾個(gè)部分:

Base64編碼的圖像數(shù)據(jù):這部分以data:image/png;base64,開頭,后跟一長(zhǎng)串字符,這些字符是圖像的二進(jìn)制數(shù)據(jù)經(jīng)過Base64編碼后的結(jié)果。

CSS樣式:字符串中包含了一些CSS樣式,如i標(biāo)簽的樣式定義(i { ... }),這些樣式可能用于控制HTML文檔中元素的顯示方式。但在這個(gè)示例中,CSS樣式是直接嵌入在HTML中的,并且由于格式和上下文的原因,可能不完整或難以直接應(yīng)用。

HTML結(jié)構(gòu):字符串中包含了HTML的基本結(jié)構(gòu),如<div><span>等標(biāo)簽,以及自定義的類或ID屬性(如class="..."id="..."),這些用于在CSS中引用并應(yīng)用樣式。

JavaScript的引用或嵌入:雖然直接在這個(gè)字符串中沒有看到JavaScript代碼,但通常HTML頁面會(huì)包含JavaScript代碼或引用外部JavaScript文件來控制頁面的行為。這個(gè)字符串可能只是HTML頁面的一部分,而JavaScript代碼可能位于其他位置。

特殊字符和注釋:字符串中還包含了一些特殊字符(如//開頭的注釋)和格式化字符(如換行符\n),這些在HTML和CSS中用于提高代碼的可讀性和可維護(hù)性。

綜上所述,這段文本是一個(gè)HTML編碼的字符串,它結(jié)合了Base64編碼的圖像數(shù)據(jù)、CSS樣式和HTML結(jié)構(gòu),可能還隱式地引用了JavaScript代碼。這種格式常用于在網(wǎng)頁中嵌入圖像、樣式和腳本,以實(shí)現(xiàn)豐富的視覺效果和交互功能。

如何實(shí)現(xiàn)渲染在網(wǎng)頁:

在前端網(wǎng)頁中嵌入使用Base64編碼的圖像字符串,可以直接將這個(gè)字符串作為<img>標(biāo)簽的src屬性的值。由于Base64編碼的圖像數(shù)據(jù)被封裝在data: URL中,瀏覽器可以直接解析這個(gè)URL并將其作為圖像內(nèi)容顯示在頁面上,而無需從外部服務(wù)器加載圖像。

以下是一個(gè)該字符串在前端網(wǎng)頁中嵌入圖像的示例:

<template>
   <div>  
    <img v-if="imageUrl" :src="imageUrl" alt="Image from backend" />  
  </div>  
</template>
 
<script>
import axios from 'axios'
export default {
  data() {
    return {
      imageUrl: null
    }
  },
  mounted(){
    
    axios.post("http://localhost:8080/test4").then((e) => {
      this.imageUrl= "data:image/png;base64," + e.data.data[0].photo;
    })
  
}

以上就是Java實(shí)現(xiàn)數(shù)據(jù)庫圖片上傳功能詳解的詳細(xì)內(nèi)容,更多關(guān)于Java數(shù)據(jù)庫圖片上傳的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java Http的基礎(chǔ)概念了解

    Java Http的基礎(chǔ)概念了解

    這篇文章主要介紹了Java Http的基礎(chǔ)概念,HTTP協(xié)議是建立在TCP協(xié)議之上的,這個(gè)程序是通過TCP編程來構(gòu)建一個(gè)簡(jiǎn)單的Http服務(wù)器,需要的朋友可以參考下
    2023-04-04
  • Spring配置文件使用占位符配置方式

    Spring配置文件使用占位符配置方式

    這篇文章主要介紹了Spring配置文件使用占位符配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • mybatis使用@mapkey獲取的結(jié)果的鍵(key)為null問題

    mybatis使用@mapkey獲取的結(jié)果的鍵(key)為null問題

    這篇文章主要介紹了mybatis使用@mapkey獲取的結(jié)果的鍵(key)為null問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java并發(fā)編程之關(guān)鍵字volatile知識(shí)總結(jié)

    Java并發(fā)編程之關(guān)鍵字volatile知識(shí)總結(jié)

    今天帶大家學(xué)習(xí)java的相關(guān)知識(shí),文章圍繞著Java關(guān)鍵字volatile展開,文中有非常詳細(xì)的知識(shí)總結(jié),需要的朋友可以參考下
    2021-06-06
  • java中Locks的使用詳解

    java中Locks的使用詳解

    這篇文章主要介紹了java中Locks的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Java中URL傳中文時(shí)亂碼的解決方法

    Java中URL傳中文時(shí)亂碼的解決方法

    為什么說亂碼是中國(guó)程序員無法避免的話題呢?這個(gè)主要是編碼機(jī)制上的原因,大家都知道中文和英文的編碼格式不一樣,解碼自然也不一樣!這篇文章就給大家分享了Java中URL傳中文時(shí)亂碼的解決方法,有需要的朋友們可以參考借鑒。
    2016-10-10
  • SpringBoot3.0整合chatGPT的完整步驟

    SpringBoot3.0整合chatGPT的完整步驟

    ChatGPT是OpenAI推出的一個(gè)語言模型系統(tǒng),它能夠?qū)崟r(shí)回答用戶提問,包括聊天、糾正語法錯(cuò)誤,甚至是寫代碼、寫劇本等,由于可玩性很高,迅速在全球范圍內(nèi)風(fēng)靡起來,下面這篇文章主要給大家介紹了關(guān)于SpringBoot3.0整合chatGPT的完整步驟,需要的朋友可以參考下
    2022-12-12
  • JavaWeb 實(shí)現(xiàn)多個(gè)文件壓縮下載功能

    JavaWeb 實(shí)現(xiàn)多個(gè)文件壓縮下載功能

    文件下載時(shí),我們可能需要一次下載多個(gè)文件,批量下載文件時(shí),需要將多個(gè)文件打包為zip,然后再下載。本文給大家分享實(shí)現(xiàn)思路及具體實(shí)現(xiàn)代碼,對(duì)javaweb實(shí)現(xiàn)文件壓縮下載功能感興趣的朋友一起學(xué)習(xí)吧
    2017-07-07
  • SpringBoot中項(xiàng)目結(jié)構(gòu)的項(xiàng)目實(shí)踐

    SpringBoot中項(xiàng)目結(jié)構(gòu)的項(xiàng)目實(shí)踐

    SpringBoot項(xiàng)目結(jié)構(gòu)遵循Maven或Gradle的標(biāo)準(zhǔn)目錄結(jié)構(gòu),融入了SpringBoot的特定約定,本文就來介紹一下SpringBoot中項(xiàng)目結(jié)構(gòu)的項(xiàng)目,感興趣的可以了解一下
    2025-03-03
  • Spring依賴注入與自動(dòng)裝配原理解析

    Spring依賴注入與自動(dòng)裝配原理解析

    本文講解Spring依賴注入核心知識(shí):闡釋 Bean、IoC 等概念,介紹三種注入方式(推薦構(gòu)造器注入),解析 @Autowired 自動(dòng)裝配原理、注解差異及循環(huán)依賴等問題的解決辦法,感興趣的朋友跟隨小編一起看看吧
    2026-02-02

最新評(píng)論

凤翔县| 无极县| 眉山市| 望江县| 陇川县| 华亭县| 栾川县| 定南县| 临潭县| 沙雅县| 呼和浩特市| 遂川县| 阳山县| 双峰县| 新昌县| 金门县| 六安市| 阿尔山市| 都昌县| 金华市| 东乡| 吉木乃县| 石首市| 阜阳市| 大兴区| 天祝| 江达县| 吐鲁番市| 万州区| 奉贤区| 青河县| 霞浦县| 山西省| 马关县| 洮南市| 乃东县| 山西省| 鄂伦春自治旗| 民乐县| 江北区| 宜宾县|