Java implementation downloads files for the specified directory

Source: Internet
Author: User

@RequestMapping ("/exportdocument") @ResponseBody Public voidExportdocument (httpservletrequest request,httpservletresponse response)throwsIOException {xwpfdocument Xdoc=NULL; FileInputStream is=NULL; OutputStream out=NULL; Try{String Wordname= "Poly Airport 2.0 User manual. docx"; Wordname=NewString (Wordname.getbytes (), "Iso8859-1"); //File File = new file ("/root/usersguide.docx");Response.setcontenttype ("Application/octet-stream"); Response.setheader ("Content-disposition", "attachment; Filename= "+wordname); is=NewFileInputStream ("/root/usersguide.docx"); out=response.getoutputstream ();//get an Output objectXdoc =Newxwpfdocument (IS); } Catch(IOException e) {e.printstacktrace (); }finally{                Try{xdoc.write (out);//Write out the This document to an outputstream.Is.close ();                Out.close (); } Catch(IOException e) {e.printstacktrace (); }            }                 /*The above is the download of all Word documents*/

/*Here are the various types of file downloads*/ /*2. Get the filename to download String filename = "Test.png"; 3. Set the Content-disposition response header to control the browser to download the form of open file Response.setheader ("Content-disposition", "Attachment;filename = "+filename"); 4. Get the file input stream to be downloaded InputStream in=null; OutputStream Out=null; try {//Get the absolute path to the file to be downloaded in=new fileinputstream ("/root/operating route map 0.3.5 version. png"); int len = 0; 5. Create a data buffer byte[] buffer = new byte[1024]; 6. Get OutputStream stream out by response object = Response.getoutputstream (); 7. Write the FileInputStream stream to the buffer buffer while (len = in.read (buffer)) > 0) {//in.read (byte[] b) Read in B.len Gth bytes returns 1//8 when it touches the end of a stream. Use OutputStream to output buffer data to the client browser out.write (Buffer,0,len ); }} catch (FileNotFoundException e) { E.printstacktrace (); }finally{In.close (); Out.close (); }*/ }

Java implementation downloads files for the specified directory

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.