Not much to say, direct sticker code:
File download public static String downloadFile (file file) {HttpServletResponse response = Servletactioncontex T.getresponse (); FileInputStream FIS = null; Bufferedinputstream buff = null; OutputStream out = null; try {/* If the file exists */if (file.exists ()) {//set to no cache response . Reset (); Set the encoding method of the response//response.setcontenttype ("Application/x-download"); Response.setcontenttype ("Application/ms-excel"); This sentence is more detailed, tell the browser to download the Excel file//settings download file name Response.setheader ("Content-disposition", "Filena Me= "+new String (File.getname (). GetBytes ()," UTF-8 ")); read out file to I/O stream fis=new fileinputstream (file); Buff=new Bufferedinputstream (FIS); Get the output stream from the response object, ready to download out = Response.getoutputstream (); //PrintWriter out = Response.getwriter (); I can write anything in bytes.//Writes the contents of int i; while ((i = Buff.read ())! =-1) {out.write (i); }//Push the contents into the document Out.flush (); }else{return "Download fail";//File Not Present}} catch (Exception e) {//To Do:handle exception}finally{try {if (buff! = null) b Uff.close (); if (out! = null) out.close (); } catch (IOException e) {return "download fail"; }} return "Download Success"; }
Java file download (with very detailed comments).