Ⅰ 請進!!如何把繪制的圖片直接以二進制流存入資料庫(java)
怎樣在mysql中存儲比較大的圖片?
如果你想把二進制的數據,比如說圖片文件和HTML文件,直接保存在你的MySQL資料庫,那麼這篇文章就是為你而寫的!我將告訴你怎樣通過HTML表單來儲存這些文件,怎樣訪問和使用這些文件。
一、本文概述
本文的主要內容如下:
* 在MySQL中建立一個新的資料庫
* 一個怎樣儲存文件的例子程序
* 一個怎樣訪問文件的例子程序
二、在MySQL中建立一個新的database
首先,你必須在你的MySQL中建立一個新的資料庫,我們將會把那些二進制文件儲存在這個資料庫里。在例子中我會使用下列結構,為了建立資料庫,你必須做下列步驟:
1. 進入MySQL控制器
2. 輸入命令"create database binary_data;"
3. 輸入命令"use binary_data;"
輸入如下命令:
"CREATE TABLE binary_data ( id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,description CHAR(50), bin_data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50));" (不能斷行)
如果沒有意外,資料庫 和 表 應該建立好了。
三、一個怎樣儲存文件的例子程序
用這個例子你可以通過Html表單將文件傳輸到資料庫中。
store.php3
// store.php3 - by Florian Dittmer
?>
// 如果提交了表單,代碼將被執行:
if ($submit) {
// 連接到資料庫
// (你可能需要調整主機名,用戶名和密碼)
MYSQL_CONNECT( "localhost", "root", "password");
MySQL_select_db( "binary_data");
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
$result=MYSQL_QUERY( "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype)VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
$id= MySQL_insert_id();
print "This file has the following Database ID: $id";
MYSQL_CLOSE();
} else {
// 否則顯示儲存新數據的表單
?>
@MySQL_select_db( "binary_data");
$query = "select bin_data,filetype from binary_data where id=$id";
$result = @MYSQL_QUERY($query);
$data = @MYSQL_RESULT($result,0, "bin_data");
$type = @MYSQL_RESULT($result,0, "filetype");
Header( "Content-type: $type");
echo $data;
};
?>
程序必須知道要訪問那個文件, 你必須將ID作為一個參數。
例如: 一個文件在資料庫中的ID為2. 你可以這樣調用它: getdata.php3?id=2
如果你將圖片儲存在資料庫里, 你可以向調用圖片一樣調用它。
Example: 一個圖片文件在資料庫中的ID為3. 你可以這樣調用它:
五、怎樣儲存大於1MB的文件
如果你想儲存大於1MB的文件,你必須對你的程序、PHP設置、SQL設置進行許多修改。
下面幾條也許可以幫助你儲存小於24MB的文件:
1) 修改 store.php3,將 MAX_FILE_SIZE 的值改成 24000000。
2) 修改你的PHP設置,在一般情況下,PHP只允許小於2MB的文件,你必須將max_filesize(在php.ini中)的值改成24000000
3) 去掉MYSQL的數據包大小限制,在一般情況下 MYSQL 小於1 MB的數據包。
4) 你必須用以下參數重啟你的MYSQL :/usr/local/bin/safe_MySQLd -O key_buffer=16M -O table_cache=128 -O sort_buffer=4M -O record_buffer=1M -O max_allowed_packet=24M
5) 如果仍然出錯:可能是超時錯誤,如果你通過一個很慢的連接來儲存一個很大的文件,PHP預設的時間限制為30秒。你可以將max_execution_time(在php.ini中)的值改為-1
下面是一個老外寫的,可以讀
Saving Images in MySQL
Sometimes, it's more convenient to save images in a database than as files.
MySQL and PHP make it very easy to do this. In this article, I will describe
how to save images in a MySQL database and display them later on.
Setting up the database
The difference between any regular text or integer fields and a field that
needs to save an image is the amount of data that is needed to be held in the
field. MySQL uses special fields to hold large amounts of data. These fields
are known as blobs (blob).
Here is the BLOB definition from the MySQL site :
A BLOB is a binary large object that can hold a variable amount of data. The
four BLOB types TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB differ only in the
maximum length of the values they can hold
For more information about MySQL BLOBs check out
hapter/manual_Reference.html#BLOB
Use the next syntax to create a basic table that will hold the images:
CREATE TABLE Images (
PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Image BLOB
);
Setting the upload script
An example of a file upload front end can be seen at File Uploading by berber
(29/06/99). What we need now is the PHP script that will get the file and
insert it into MySQL. The next script does just that. In the script, I'm
assuming that the name of the file field is "icture".
<?
If($Picture != "none") {
$PSize = filesize($Picture);
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
unlink($Picture);
mysql_connect($host,$username,$password)
or die("Unable to connect to SQL server");
@mysql_select_db($db)
or die("Unable to select database");
mysql_query("INSERT INTO Images (Image) VALUES '($mysqlPicture')")
or die("Can't Perform Query");
}
else {
echo"You did not upload any picture";
}
?>
This is all that is needed to enter the image into the database. Note that in
some cases you might get an error when you try to insert the image into
MySQL. In such a case you should check the maximum packet size allowed by
your MySQL ver. It might be too small and you will see an error about this in
the MySQL error log.
What we did in the above file is :
1. Check if a file was uploaded with If($Picture != "none").
2. addslashes() to the picture stream to avoide errors in MySQL.
3. Delete the temporary file.
3. Connect to MySQL, choose the database and insert the image.
Displaying the Images
Now that we know how to get the images into the database we need to figure
out how to get them out and display them. This is more complicated than
getting them in but if you follow these steps you will have this up and
running in no time.
Since showing a picture requires a header to be sent, we seem to be in an
impossible situation in which we can only show one picture and than we can't
show anymore Since once the headers are sent we can't send any more headers.
This is the tricky part. To outsmart the system we use two files. The first
file is the HTML template that knows where we want to display the image(s).
It's a regular PHP file, which builds the HTML that contains the <IMG> tags,
as we want to display them. The second file is called to provide the actual
file stream from the database directly into the SRC property of the <IMG>
tag.
This is how a simple script of the first type should look like:
<HTML>
<BODY>
<?
mysql_connect($host,$username,$password)
or die("Unable to connect to SQL server");
@mysql_select_db($db)
or die("Unable to select database");
mysql_query("SELECT * FROM Images")
or die("Can't Perform Query");
While($row=mysql_fetch_object($result)) {
echo "<IMG SRC=\"SecondType.php3?PicNum=$row->icNum\">";
}
?>
</BODY>
</HTML>
While the HTML is being displayed, the SecondType.php3 file is called for
each image we want to display. The script is called with the Picture ID
(PicNum) which allows us to fetch the image and display it.
The SecondType.php3 file looks like this :
<?
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum")
or die("Can't perform Query");
$row=mysql_fetch_object($result);
Header( "Content-type: image/gif");
echo $row->Image;
?>
This is the whole theory behind images and MySQL. The scripts in this example
are the basics. You can now enhance these scripts to include thumbnails, set
the images in various positions, enhance the database table to hold an ALT
field, Check the width and height of the images before you insert them into
the database and keep that data in the table too etc...
Ⅱ 文件轉二進制流保存到資料庫中是不是比較節省資料庫空間
以二進制方式保存主要是考慮資料庫欄位存儲大小的問題,及方便性,不見得節資料庫空間。