【问题标题】:Save an image (profile picture) within a database as binary[] or string (url)?将数据库中的图像(个人资料图片)保存为二进制 [] 或字符串(url)?
【发布时间】:2026-01-23 00:35:01
【问题描述】:

将数据库中的图像(个人资料图片)保存为字节 [] 或字符串 (url)? 有什么优点和缺点?

【问题讨论】:

    标签: database string image web-applications bytearray


    【解决方案1】:

    您绝对应该更喜欢 URL 而不是 byte[]。

    主要原因是,在将图像存储到字节数组时,并非每种语言都有相同的位模式。例如 java 的字节是有符号的(范围 -128 到 127),而 C# 的字节是无符号的(范围是 0 到 255)。这意味着,如果您使用 C# 将图像存储在字节数组中,则将字节数组重新转换回图像的平台必须是 c#(除非您不想处理低级转换)

    另一方面,URL 是一个 URI,它除了标识 Web 资源外。正如您可以从名称中理解的那样,它的Uniform Resource Locator 与平台无关。用户可以在任何类型的平台上使用此信息。

    【讨论】:

      【解决方案2】:

      使用blob将图像文件保存在数据库中这里是java中的代码 创建表为

      CREATE TABLE save_image (             
                id int(5) NOT NULL auto_increment,  
                name varchar(25) default NULL,      
                city varchar(20) default NULL,      
                image blob,                         
                Phone varchar(15) default NULL,     
                PRIMARY KEY  (`id`)                   
                    );
      

      用户保存图片的java文件

      import java.sql.*;
      import java.io.*;
      class SaveImageToDatabase {
      public static void main(String[] args) throws SQLException {
      // declare a connection by using Connection interface 
      Connection connection = null;
      /* Create string of connection url within specified format with machine 
      name, port number and database name. Here machine name id localhost 
      and database name is mahendra. */
      String connectionURL = "jdbc:mysql://localhost:3306/mahendra";
      /*declare a resultSet that works as a table resulted by execute a specified 
      sql query. */
      ResultSet rs = null;
      // Declare prepare statement.
      PreparedStatement psmnt = null;
      // declare FileInputStream object to store binary stream of given image.
      FileInputStream fis;
      try {
      // Load JDBC driver "com.mysql.jdbc.Driver"
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      /* Create a connection by using getConnection() method that takes 
      parameters of string type connection url, user name and password to 
      connect to database. */
      connection = DriverManager.getConnection(connectionURL, "root", "root");
      // create a file object for image by specifying full path of image as  parameter         
      File image = new File("C:/image.jpg");
      /* prepareStatement() is used for create statement object that is 
      used for sending sql statements to the specified database. */
      psmnt = connection.prepareStatement
      ("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)");
      psmnt.setString(1,"SmashCode");
      psmnt.setString(2,"GPU");
      psmnt.setString(4,"127001");
      fis = new FileInputStream(image);
      psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
      /* executeUpdate() method execute specified sql query. Here this query 
      insert data and image from specified address. */ 
      int s = psmnt.executeUpdate();
       if(s>0) {
      System.out.println("Uploaded successfully !");
       }
       else {
       System.out.println("unsucessfull to upload image.");
        }
       }
       // catch if found any exception during rum time.
       catch (Exception ex) {
       System.out.println("Found some error : "+ex);
        }
        finally {
          // close all the connections.
            connection.close();
             psmnt.close();
             }
              }
               }
      

      你可以检索图片,你可以在这里参考这个网站here

      我在这里主要使用代码,希望我的工作能让你开心,如果你喜欢我的回答,请点赞

      【讨论】: