Java upload attachment to file system

Source: Internet
Author: User
Tags file system relative

Pictures, word,text files cannot be stored as VARCHAR2 types in the database, and are stored in orca in Clob,blob type. stored in SQL Server as a img,txt type. The advantage of storing attachments in the database is that attachments are not easily lost, But it's a little inconvenient to write to the database and read it out. Are you sure you want to store the attachment in the database?

The relative address of the attachment can be stored in the database. The attachment is really saved in the file system. It's easy to read:

Under the MyProject project, under Webroot, there is a catalog picture specifically for storing pictures

There is a field in the database Tpath store relative paths such as: String rpath=/picture/mypicture20070905.jpg

In JSP:

<a href= "<%url%>" ></a>//can be displayed

<a href= "<%world Document relative path%>" > Attachment a </a>//Click the right button to save to download

How to upload attachments to the Web server picture directory

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 () ignores any other form information that is a file field
if (!item.isformfield ()) {
String name = Item.getname ();
Long size = Item.getsize ();
if (name==null| | Name.equals ("")) && size==0)
Continue
String Mpath=application.getrealpath ("/picture")//Get current page absolute path
String fileseparator=system.getproperty ("File.separator");
Item.write (New Java.io.File (MPath + fileseparator+rname));//The Web server absolute path to the picture
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.