Java direct File Download

Source: Internet
Author: User

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();} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.