将数据库中的图像(个人资料图片)保存为二进制 [] 或字符串 (url)?
Save an image (profile picture) within a database as binary[] or string (url)?
将数据库中的图像(个人资料图片)保存为字节 [] 或字符串 (url)?
有什么优点和缺点?
使用blob在数据库中保存图像文件这里是java中的代码
将 table 创建为
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
我在这里主要使用代码希望我的工作能让你开心如果你喜欢我的回答
你绝对应该更喜欢 URL 而不是 byte[]。
主要原因是,并非每种语言在将图像存储到字节数组时都具有相同的位模式。例如 java 的字节是有符号的(范围为 -128 到 127),而 C# 的字节是无符号的(范围为 0 到 255)。这意味着,如果您使用 C# 将图像存储在字节数组中,则将字节数组重新转换回图像的平台必须是 c#(除非您不想处理低级转换)
另一方面,A URL 是一个 URI,除了标识 Web 资源之外。正如您从名称中可以理解的那样,它 Uniform Resource Locator
与平台无关。用户可以在任何类型的平台中使用此信息。
将数据库中的图像(个人资料图片)保存为字节 [] 或字符串 (url)? 有什么优点和缺点?
使用blob在数据库中保存图像文件这里是java中的代码 将 table 创建为
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
我在这里主要使用代码希望我的工作能让你开心如果你喜欢我的回答
你绝对应该更喜欢 URL 而不是 byte[]。
主要原因是,并非每种语言在将图像存储到字节数组时都具有相同的位模式。例如 java 的字节是有符号的(范围为 -128 到 127),而 C# 的字节是无符号的(范围为 0 到 255)。这意味着,如果您使用 C# 将图像存储在字节数组中,则将字节数组重新转换回图像的平台必须是 c#(除非您不想处理低级转换)
另一方面,A URL 是一个 URI,除了标识 Web 资源之外。正如您从名称中可以理解的那样,它 Uniform Resource Locator
与平台无关。用户可以在任何类型的平台中使用此信息。