查看: 1299|回复: 8
|
如何把图片储藏在java Database里?
[复制链接]
|
|
我是用access做database...
谢谢帮忙!! |
|
|
|
|
|
|
|
发表于 27-1-2005 04:24 PM
|
显示全部楼层
有两个方法..
1)用OLE FIELD -> 把图片当成OLE OBJECT来IMPORT.
2)用普通的TEXT -> 把图片放在一个FOLDER里,DATABASE只是储存图片的PATH.
方法(2)会比较快而且你的DATABASE会比较快.. |
|
|
|
|
|
|
|
楼主 |
发表于 27-1-2005 04:48 PM
|
显示全部楼层
方法2。。
不是很明白。。。。请问你可以说清楚一些吗?
我的系统好像做IC 卡以,所以要select the photo and store it.。但是不知道怎么写code...
谢谢。! |
|
|
|
|
|
|
|
发表于 27-1-2005 09:00 PM
|
显示全部楼层
其实很简单。。当你STORE的时候只要把照片都放在同一个FOLDER里面。。
然后把它的PATH(比如说 ../picture/my_picture.jpg) STORE 在 TEXT 的 FIELD 里。。
读取的时候大概是这样。。(我用PHP做例子。。)
<?
echo "<img src=$imgdir>" // $imgdir是那个照片的PATH。(从TEXT FIELD中读取。。)
?>
到时那个PROGRAM就会自动读取那个照片了。。
这样你的DATABASE就会很小,很快。。 |
|
|
|
|
|
|
|
发表于 28-1-2005 01:25 PM
|
显示全部楼层
lonely2 于 27-1-2005 02:32 PM 说 :
我是用access做database...
谢谢帮忙!!
我觉得用 path 来 link image 不是很好, 因为这会 indirectly create a dependency between your image, path and database
不如把image 存放到database 里,这样会比较 portable
Write to dabatase
=================
File file = new File("Image.jpg");
FileInputStream fi = new FileInputStream(file);
InputStream in = (InputStream)new BufferedInputStream(fi);
String str = "INSERT INTO testtest(image)VALUES(?)";
PreparedStatement statement = connection.prepareStatement(str);
statement.setMaxFieldSize(0);
statement.setBinaryStream(1,in,(in.available()));
statement.executeUpdate();
Read from database
=============
Statement statement = connection.createStatement();
ResultSet resultSet= statement.executeQuery("SELECT image from testtes");
Blob blob = resultSet.getBlob("image");
InputStream is = blob.getBinaryStream();
FileOutputStream fileOI = new FileOutputStream("location/imageName.jpg");
byte[] buffer = new byte[1024];
int byteRead =0;
while((byteRead=is.read(buffer))!=-1)
fileOI.write(buffer,0,byteRead);
fileOI.flush();
fileOI.close();
is.close();
Note: It is just an idea how to write and read a blob data format from database, this code is not yet tested!
Good Luck |
|
|
|
|
|
|
|
发表于 28-1-2005 06:41 PM
|
显示全部楼层
黑木头 于 28-1-2005 01:25 PM 说 :
我觉得用 path 来 link image 不是很好, 因为这会 indirectly create a dependency between your image, path and database
不如把image 存放到database 里,这样会比较 portable
Write to dabatase
...
今天又学到东西了。
以前都没有想过将image convert 成binary format, 然后save binary value to DB。 |
|
|
|
|
|
|
|
发表于 28-1-2005 09:02 PM
|
显示全部楼层
这样的话资料库不会负担很大吗??
我一向来都是用PATH的方法的。。
只有在UPLOAD的时候会用到BINARY的转换。。
反正有高受灾。。我还有想学学一样东西。。
有什么方法可以让我知道一个OBJECT的FILE TYPE..
我不要READ FILE EXTENSION的那种。。因为不准的。。
EXTENSION很容易就可以更改的。。
听说可以从FILE HEADER中查询。。可是SYNTAX要怎么样写??
还是有其他的方法?? |
|
|
|
|
|
|
|
发表于 29-1-2005 09:21 AM
|
显示全部楼层
JR86 于 28-1-2005 09:02 PM 说 :
这样的话资料库不会负担很大吗??
我一向来都是用PATH的方法的。。
只有在UPLOAD的时候会用到BINARY的转换。。
反正有高受灾。。我还有想学学一样东西。。
有什么方法可以让我知道一个OBJECT的FILE TYPE..
...
我觉得这还可以接受,通常我们都会 limit file size 避免超重。如果用PATH的方法,这会增加很多麻烦,比如说,你必需 create multiple folders for each client 或用 client' username + file name 这样的方式来避免模糊不清 (example two clients attempt to upload a file which the file has same identifier), 其实这些 tedious tasks can be easily avoided by using relational database. 还有你可要小心管理你的 path, 因为 Windows 和 linux 的 path 是不同。This is one of the reasons i said hold it in dabatase much more portable than keeping path...
如果你用 hibernate 的话,那太好办事啦。。。,我本人太爱用 hibernate 啦,它的主子 - Gavin King 太强啦,一流的编程高手 (http://www.hibernate.org)
FILE HEADER中查询,我没试过。大家可以交流。。。。
这是个人意见,请多多指教! |
|
|
|
|
|
|
|
楼主 |
发表于 1-2-2005 01:48 PM
|
显示全部楼层
huh~~谢谢你们的回答,但是现在一个头两个大。。如有问题再请问你们。。 :)
谢 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|