The configuration in tomcat is as follows:
The code is as follows: |
Copy code |
<Mime-mapping> <Extension> txt </extension> <Mime-type> application/octet-stream </mime-type> </Mime-mapping> <Mime-mapping> <Extension> jpg </extension> <Mime-type> application/octet-stream </mime-type> </Mime-mapping> |
For the above configuration, a download prompt box appears when you access resources with the extension txt or jpg. If you only need to display a download prompt box for some of the mentioned resources, the above configuration will not work, the solution is to set content-type in the resource response header, for example:
Php
The code is as follows: |
Copy code |
Header ("Content-type: application/octet-stream "); Header ('content-Disposition: attachment; filename = "downloaded.txt "'); |
Download file programs
The code is as follows: |
Copy code |
<? Header ("content-type: text/html; charset = utf-8 "); $ File_name = $ _ GET ['name']; // the actual file name of the server $ File_realName = urldecode ($ _ GET ['real']); // URL encoded database file name $ File_dir = "upload /"; $ File = fopen ($ file_dir. $ file_name, "r"); // open the file // Input file tag 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 // transcoder", $ file_realName )); // Output file content Echo fread ($ file, filesize ($ file_dir. $ file_name )); Fclose ($ file ); Exit; ?> |
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 attribute 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 (); // either or not Response. setContentType ("application/x-download"); // Set it to download application/x-download ///.. /.. /Back to the WEB-INF/classes two levels to the application root directory, pay attention to Tomcat and WebLogic below this sentence to get the path is different, WebLogic in the path does not last/ ServletContext context = session. getServletContext (); String realContextPath = context. getRealPath ("") + "\ plan \ planning data template.xls "; String filenamedisplay = "planning data template.xls "; Filenamedisplay = URLEncoder. encode (filenamedisplay, "UTF-8 "); Response. addHeader ("Content-Disposition", "attachment; filename =" + filenamedisplay ); OutputStream output = null; FileInputStream FCM = null; Try { Output = response. getOutputStream (); FCM = new FileInputStream (realContextPath ); Byte [] B = new byte [1024]; Int I = 0; While (I = FCM. read (B)> 0) { Output. write (B, 0, I ); } Output. flush (); } Catch (Exception e) { System. out. println ("Error! "); E. printStackTrace (); } Finally { If (FS! = Null) { FCM. close (); FS = null; } If (output! = Null) { Output. close (); Output = null; } } %> |