Uploading files using Apache FileUpload Library (supports multiple files)

Source: Internet
Author: User

Spring 3.0 Validation Passes

Import Org.apache.commons.fileupload.DiskFileUpload;

Import Org.apache.commons.fileupload.FileItem;

Public String upload (httpservletrequest request,

HttpServletResponse response) throws Exception {

String fullfilename = null;

Diskfileupload disk = new Diskfileupload ();

try {

list<fileitem> files = (list<fileitem>) disk.parserequest (request);

for (Fileitem item:files) {

Fullfilename = Item.getname ();

if (Fullfilename = = null) {

System.out.println ("Fullfilename not existed.");

Continue

}

String fileName1 = fullfilename.substring (fullfilename.lastindexof ("\ \") +1);

File File1 = new file (realpath,filename1);

File1.getparentfile (). Mkdirs ();

File1.createnewfile ();

InputStream ins = Item.getinputstream ();

OutputStream ous = new FileOutputStream (file1);

try{

byte[] buffer = new byte[1024];

int len = 0;

while (len = ins.read (buffer)) >-1)

Ous.write (Buffer,0,len);

}finally{

Ous.close ();

Ins.close ();

}

}

} catch (Exception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

return "Redirect:item.do";

}

<form action= "item.do" enctype= "Multipart/form-data" method= "POST" >

<input name= "Itemfile" type= "file" class= "Text1" size= "All" maxlength= ">" (Must be a. gif or. jpg file)

</form>


Uploading files using Apache FileUpload Library (supports multiple files)

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.