/ ** * Implementation * * @param response * @param headName: The default file name * @param fileUrl: destination file URL * @throws Exception ** / public void downLoad (HttpServletResponse response, String headName, String fileUrl) throws Exception {
IF (HeadName.endSwith) || (HeadName.endSwith ("))) {// no need setting content type and header} else if (HeadName.endSwith (". txt ")) { response.setContentType ( "text; charset = GB2312"); response.setHeader ( "Content-disposition", "attachment; filename =" headName);} else {response.setContentType ( "application / doc; charset = GB2312") Response.setHeader ("Content-Disposition", "Attachment; FileName = HeadName);
String fileurl = fileurl; if (fileurl == null) return;
Try {file file = new file (fileurl); fileinputstream bis = new fileInputstream (file); OutputStream bos = response.getOutputStream ();
Byte [] buff = new byte [1024]; int = 0; ing = 0; readcount = bis.read (buff); while (readcount! = -1) {bos.write (buff, 0, readcount); ReadCount = Bis.read (BUF);}} (bis! = null) bis.close (); if (bos! = null) bos.close ();
} catCH (Exception E) {E.PrintStackTrace (); throw e;}}