圖片,word,text檔案在資料庫中是不能以varchar2類型儲存的,在orca中以clob,blob類型儲存.在SqlServer中以img,txt類型儲存.把附件儲存在資料庫中的好處是附件不容易丟失,不過要寫入資料庫,然後在讀出來就有點不方便了.難道一定要把附件儲存在資料庫中?
可以把附件的相對位址儲存在資料庫中.附件真真儲存在檔案系統中.這樣很容易讀出來:
Myproject工程下webroot下有一個目錄picture專門用來儲存圖片
資料庫中有一欄位tPath儲存相對路徑如:String rpath=/picture/mypicture20070905.jpg
jsp中:
<a href="<%url%>"><img width=220px height=170px src="<%repath%>"></a>//可以顯示
<a href="<%world文檔所在相對路徑%>">附件一</a>//點擊右鍵儲存可以下載
如何上傳附件到web伺服器picture目錄下
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(10000000);
fu.setSizeThreshold(4096);
List fileItems = fu.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next(); //item.isFormField()忽略其他是檔案域的所有表單資訊
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
String mpath=application.getRealPath("/picture");//擷取當前網頁絕對路徑
String fileseparator=System.getProperty("file.separator");
item.write(new java.io.File(mpath + fileseparator+rname));//圖片所在web伺服器絕對路徑
}