What about the text file of idea? Please help...
JSP mode:
<meta http-equiv="Content-Type" content="text/html; charset=gbk"> <HTML> <HEAD> </HEAD> <BODY> <a href = "download1.jsp?filepath=d:\\&filename=1a.txt" >downloadtest1</a> </BODY> </HTML>
File on local server(Download. JAP),
<% String filename = request.getParameter("filename");//"1a.txt"; String filepath = request.getParameter("filepath");//"d:\\"; int i = 0; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition","attachment;filename = "+filename); java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath+filename); while((i= fileInputStream.read()) != -1){ out.write(i); } %>
File on the remote server (download. JAP ),
<% @ Page Language = "Java" pageencoding = "UTF-8" contenttype = "text/html; charset = UTF-8" %> <% @ page import = "Java. util. * "%> <% @ page import =" java.net. httpurlconnection "%> <% @ page import =" java.net. URL "%> <% @ page import =" java.net. urlencoder "%> <% @ page import =" Java. io. datainputstream "%> <% @ page import =" Java. io. outputstream "%> <% // Remote File Name and path string file = request. getparameter ("file"); // download the local file name string loca Lname = file. substring (file. lastindexof ("/") + 1); // solve Chinese file name garbled localname = new string (localname. getbytes ("gb2312"), "iso8859-1"); response. reset (); response. setcontenttype ("application/force-download"); response. addheader ("content-disposition", "attachment; filename =" + localname); Url url = NULL; datainputstream in = NULL; httpurlconnection connection = NULL; outputstream = NULL; try {Url = new URL (File); connection = (httpurlconnection) URL. openconnection (); In = new datainputstream (connection. getinputstream (); outputstream = response. getoutputstream (); byte [] buffer = new byte [4096]; int COUNT = 0; while (COUNT = in. read ())! =-1) {outputstream. write (count) ;}} catch (exception e) {e. printstacktrace (); out. write ("File Download error"); out. write (E. getmessage ();} finally {outputstream. flush (); If (in! = NULL) {in. Close (); In = NULL;} out. Clear (); out = pagecontext. pushbody (); If (in! = NULL) {in. Close (); In = NULL;} connection. Disconnect (); url = NULL;} %>
This is called download1.jsp. This JSP is used to directly download files, whether txt or Word documents.
JS method (there may be security tips)
function svcode() {var winname = window.open('D:/Project/cimissworkspace/GDS/WebContent/js/jquery.min.js', '_blank', 'height=1,width=1,top=200,left=300');winname.document.open('text/html', 'replace');winname.document.writeln(obj.value);winname.document.execCommand('saveas','','code.txt');winname.close();}