Open the page in the browser to download the program code (php/jsp/java)

Source: Internet
Author: User

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;
}
}
%>

Related Article

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.