@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