Browser open page implementation file download program code (PHP/JSP/JAVA) There are students who need to learn to refer to.
The configuration in Tomcat is as follows:
The code is as follows |
Copy Code |
Txt Application/octet-stream Jpg Application/octet-stream |
For the above configuration, when access to the extension txt or JPG resources when the download prompt box, if only to some of the mentioned resources to make it appear in the Download prompt box, the above configuration will not work, the solution is to set Content-type in the response header of the resource, for example:
in PHP
The code is as follows |
Copy Code |
Header ("Content-type:application/octet-stream"); Header (' content-disposition:attachment; filename= ' Downloaded.txt '); |
Download the file program
The code is as follows |
Copy Code |
Header ("content-type:text/html; Charset=utf-8 "); $file _name=$_get[' name ']; The real file name of the server $file _realname=urldecode ($_get[' real '); The file name of the database UrlEncode encoded $file _dir= "upload/"; $file = fopen ($file _dir. $file _name, "R"); Open File Input File Label Header ("Pragma:public"); Header ("expires:0"); Header ("Content-type:application/octet-stream"); Header ("Accept-ranges:bytes"); Header ("Accept-length:". FileSize ($file _dir. $file _name)); Header ("content-disposition:attachment; Filename= ". Iconv ("UTF-8", "Gb2312//translit", $file _realname)); Output file contents Echo fread ($file, FileSize ($file _dir. $file _name)); Fclose ($file); Exit ?> |
In Java
The code is as follows |
Copy Code |
Response.setcontenttype ("Application/octet-stream"); Resp.setheader ("Content-disposition", "Attachment;filename=" Downloaded.txt "); |
If you need to set a saved name for the download, you can use the Content-disposition property to specify it.
Instance
The code is as follows |
Copy Code |
<% @page language= "java" contenttype= "Application/x-msdownload" import= "java.io.*,java.net.*" pageEncoding= "GBK" %><% Response.reset ();//Can be added or not added Response.setcontenttype ("Application/x-download");//set to download Application/x-download // /.. /.. /Retire Web-inf/classes two to the root of the application, notice that Tomcat and WebLogic have different paths from the following sentence, and the path in WebLogic does not last ServletContext context = Session.getservletcontext (); String Realcontextpath = Context.getrealpath ("") + "\plan\ plan data template. xls"; String filenamedisplay = "plan data template. xls"; Filenamedisplay = Urlencoder.encode (Filenamedisplay, "UTF-8"); Response.AddHeader ("Content-disposition", "attachment;filename=" + filenamedisplay); OutputStream output = null; FileInputStream FIS = null; Try { Output = Response.getoutputstream (); FIS = new FileInputStream (Realcontextpath); Byte[] B = new byte[1024]; int i = 0; while ((i = Fis.read (b)) > 0) { Output.write (b, 0, I); } Output.flush (); } catch (Exception e) { System.out.println ("error!"); E.printstacktrace (); } Finally { if (FIS! = null) { Fis.close (); FIS = null; } if (output! = NULL) { Output.close (); output = null; } } %> |
http://www.bkjia.com/PHPjc/631598.html www.bkjia.com true http://www.bkjia.com/PHPjc/631598.html techarticle Browser open page implementation file download program code (PHP/JSP/JAVA) There are students who need to learn to refer to. Tomcat is configured as follows: Code to copy code mime-mapping exten ...