java上傳附件到檔案系統

來源:互聯網
上載者:User

圖片,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伺服器絕對路徑
   }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.