Form file upload and file download

Source: Internet
Author: User

First, Introduction

You need to add the enctype= "Multipart/form-data" property to the form using the form form, in addition to changing the form's submission method to post, method= "POST" as follows.

Ii. examples

1, form file upload

The page code is as follows:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 </Head>5 <Body>        6     <formAction= "${pagecontext.request.contextpath}/file/upload.action"Method= "POST"enctype= "Multipart/form-data">7         <DivID= "Contenttable"style= "border:0px;">8             <H1class= "title"style= "font-size:15px; border-bottom:1px solid #DFE3E6;">Import data</H1>9             <Tablewidth= "80%">Ten             <TR> One                  <TDwidth= "20%"Align= "Right"> A Select a file to upload -                   </TD>  -                 <TDwidth= "70%"ID= "Name_h"title=""style= "Text-align:center;">  the                 <inputtype= "File"name= "Xlsfile"ID= "Xlsfile" /> -                 </TD> -                 </TR> -             </Table> +             <DivID= "Activitytable"> -                  <inputID= "Btnsave"type= "Submit"value= "Guide &nbsp; in" /> +             </Div> A         </Div> at     </form> - </Body> - </HTML>

Back-end upload processing code:

1     /**2 * Use SPRINGMVC to process file uploads3     */4@RequestMapping ("Upload")5 @ResponseBody6      Public BooleanUpload (httpservletrequest request, HttpServletResponse response, HttpSession session)throwsunsupportedencodingexception {7String path = Request.getsession (). Getservletcontext (). Getrealpath ("");8Calendar Calendar =calendar.getinstance ();9Calendar.settime (NewDate ());TenRequest.setcharacterencoding ("UTF-8"); OnePath = String.Format ("%s%s%s\\%s\\%s\\%s", Path, "Upload", "file", Calendar.get (calendar. Year), A Calendar.get (Calendar. MONTH), Calendar.get (calendar. Day_of_month)); -File filepath =NewFile (path); -         if(!filepath.exists ()) { the filepath.mkdirs (); -         } -Multiparthttpservletrequest multipartrequest =(multiparthttpservletrequest) request; - Get Files +Multipartfile multipartfile = Multipartrequest.getfile ("Xlsfile"); -OutputStream OS =NULL; +InputStream is =NULL; AFile UploadFile =NULL; at         Try { -is =Multipartfile.getinputstream (); -UploadFile =NewFile (filepath, System.currenttimemillis () + ". xls"); -OS =NewFileOutputStream (uploadfile); -Ioutils.copy (is, OS);//using the Commons-io component for file stream processing - Os.flush (); in}Catch(IOException e) { - e.printstacktrace (); to             return false; +}finally{ - ioutils.closequietly (OS); the ioutils.closequietly (IS); *         } $     return true;Panax Notoginseng}

2. File download

1     /**2 * Use SPRINGMVC for file download processing3     */4@RequestMapping ({"/template" })5      Public voiddownloadtemplate (httpservletrequest request, httpservletresponse response)6             throwsunsupportedencodingexception {7String path = Request.getsession (). Getservletcontext (). Getrealpath ("");8String filename = "template file. xls";9File File =NewFile (path + "\\file\\templagte\\" +filename);TenString useragent = Request.getheader ("User-agent"); One         byte[] bytes = Useragent.contains ("MSIE")? Filename.getbytes (): Filename.getbytes ("UTF-8");//filename.getbytes ("UTF-8") handles Safari's garbled problems AString FileName =NewString (Bytes, "Iso-8859-1");  -         //Format the output -Response.setcontenttype ("Multipart/form-data"); theResponse.setheader ("Content-disposition", "attachment;filename=" +fileName); -          -InputStream instream =NULL; -         Try { +Instream =Newfileinputstream (file); -Ioutils.copy (Instream, Response.getoutputstream ());//using the Commons-io component for file stream processing +}Catch(IOException e) { A e.printstacktrace (); at}finally{ - ioutils.closequietly (instream); -}

Form file upload and file download

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.