我将图像转换为二进制文件并将其以BLOB格式保存在Mysql数据库中。 现在,我试图显示所有不同MIME类型的图像。 我已经尝试了许多次,并提出了不同的建议(stackoverflow),还搜索了其他网站。但是,仍然无法显示图像。
为此,我将LAMP服务器与Core PHP(PHP 7.1)和MySql 5.6一起用作数据库。 要将图像转换为二进制:
java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2291)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2038)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1533)
at com.Common.Handles.Td_Mmm_Data.getConnection(Td_Mmm_Data.java:71)
at com.TD.TableOperations.asdf.asdfghj(TBLO_ORDERS.java:141)
at poe.P4_DBDataAnalyzers.Engines.asdfghjk.getAllCsRecos(UpdateAllcs.java:230)
at poe.P4_DBDataAnalyzers.Engines.asdfghjk.UpdateCsRecos(UpdateAllcs.java:52)
at Alpha.EnginesManager.asdfghjk(EnginesManager.java:132)
at Alpha.PreOpenEngines.main(PreOpenEngines.java:19)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2165)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2090)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:256)
at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2301)
at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2287)
... 8 more
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1916)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1845)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2106)
... 22 more
Exception in thread "main" java.lang.NullPointerException
at com.Common.Handles.Td_Mmm_Data.closeConnection(Td_Mmm_Data.java:96)
at com.TD.TableOperations.qwerqwer.GetAllEntriesAsList(TBLO_ORDERS.java:150)
at poe.P4_DBDataAnalyzers.Engines.qwerqwerqwerweqr.getAllCsRecos(UpdateAllcsRecosManager.java:230)
at poe.P4_DBDataAnalyzers.Engines.qwerqewrqewwqerqwerqwer.Updatecs(UpdateAllCs.java:52)
at Alpha.EnginesManager.qwerweqqwerqwer(EnginesManager.java:132)
at Alpha.PreOpenEngines.main(PreOpenEngines.java:19)
要将此二进制文件存储在DB中:
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
要通过屏幕显示图像访问数据库中的数据:
$filename=array("file.jpg","file 22.png","file 11.png","file 1.jpg");
foreach ($filename as $value) {
$pos = strrpos($value, '.');
$extensions= substr($value, $pos+1);
if($extensions=='jpg'){
$ext='image/jpg';
$imageData= data_uri('./downloads/'.$value,$ext);
}
if($extensions=='png'){
$ext='image/png';
$imageData= data_uri('./downloads/'.$value,$ext);
}
$sql1="INSERT INTO image(name,binary_image,extension)
VALUES('".$value."','".$imageData."','".$ext."') ";
$rt=$conn->query($sql1);
}
包含二进制数据的损坏图像
答案 0 :(得分:0)
您需要为每个图像循环:
<?php
$sql="SELECT * FROM image";
$res=$conn->query($sql);
if($res->num_rows >0){
while($row=$res->fetch_assoc()){
$filenam= $row['name'];
$binaryData= explode(',',$row['binary_image']);
$extens= $row['extension'];
for ($i = 0; $i < count($binaryData); $i++){
echo '<img src="data:image/jpeg;base64,'.$binaryData[$i].'" alt="photo"><br>';
}
}
}
?>