response.setContentType("application/octet-stream"); response.setContentType("application/OCTET-STREAM;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName1);File file = new File(filePath + fileName);try { fis = new FileInputStream(file); out = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { out.write(buffer, 0, len); out.flush(); }}catch.......
An online open or Download Method
Public String executecommandudownload (actioncontext context) throws exception {httpservletresponse response = context. getresponse (); response. setcharacterencoding ("UTF-8"); Boolean isonline = false; string filename = context. getrequest (). getparameter ("FILENAME"); connection con = NULL; httpservletrequest request = context. getrequest (); string filepath = NULL; bufferedinputstream buffer = NULL; outputstream ou T = NULL; try {con = This. getconnection (context); If ("". equals (filename) | filename = NULL) {fileinfobean bean = new fileinfobean (); filename = bean. findname (con, ID);} file F = new file (filepath); // check whether the file exists if (! F. exists () {response. senderror (404, "file not found! "); Return" file not found! ";} Buffer = new bufferedinputstream (New fileinputstream (f); byte [] Buf = new byte [1024]; int Len = 0; response. reset (); // very important if (isonline) {// online open mode URL u = new URL ("file: //" + filepath); response. setcontenttype (U. openconnection (). getcontenttype (); response. setheader ("content-disposition", "inline; filename =" + (F. getname ()). getbytes ("GBK"); // the file name should be encoded into a UTF-8} else {// pure download method response. setcontenttype ("application/X-msdownload"); response. setheader ("content-disposition", "attachment; filename =" + java.net. urlencoder. encode (F. getname (), "UTF-8");} Out = response. getoutputstream (); While (LEN = buffer. read (BUF)> 0) out. write (BUF, 0, Len);} catch (throwable e) {e. printstacktrace ();} finally {try {buffer. close (); out. close ();} catch (throwable e) {e. printstacktrace () ;}} return "";}