The program code (php/jsp/java) for downloading files by opening the page in the browser. Students who need to learn can refer to it.
The configuration in tomcat is as follows:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
txt
application/octet-stream
jpg
application/octet-stream
|
|
代码如下 |
复制代码 |
header("Content-type:application/octet-stream");
header('Content-Disposition: attachment; filename="downloaded.txt"');
|
For the above configuration, when accessing resources with extension txt or jpg, a download prompt box will appear. If you only need to have a download prompt box appear for some mentioned resources, the above configuration will not work. The solution is Just set the content-type in the response header of the resource, for example:
php in
代码如下 |
复制代码 |
header("content-type:text/html; charset=utf-8");
$file_name=$_GET['name']; //服务器的真实文件名
$file_realName=urldecode($_GET['real']); //数据库的文件名urlencode编码过的
$file_dir="upload/";
$file = fopen($file_dir . $file_name,"r"); // 打开文件
// 输入文件标签
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));
// 输出文件内容
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit;
?> |
The code is as follows |
Copy code |
header("Content-type:application/octet-stream");
header('Content-Disposition: attachment; filename="downloaded.txt"');
|
代码如下 |
复制代码 |
response.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "attachment;filename="downloaded.txt"); |
Download file program
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 attribute to specify it.
Example
The code is as follows
代码如下 |
复制代码 |
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gbk"%><%
response.reset();//可以加也可以不加
response.setContentType("application/x-download");//设置为下载application/x-download
// /../../退WEB-INF/classes两级到应用的根目录下去,注意Tomcat与WebLogic下面这一句得到的路径不同,WebLogic中路径最后没有/
ServletContext context = session.getServletContext();
String realContextPath = context.getRealPath("")+"plan计划数据模板.xls";
String filenamedisplay = "计划数据模板.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;
}
}
%>
|
|
Copy code |
|
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gbk"%><%
response.reset();//You can add it or not add it
response.setContentType("application/x-download");//Set to download application/x-download
// /../../ Go back two levels to WEB-INF/classes and go to the root directory of the application. Note that the paths obtained in the following sentence are different between Tomcat and WebLogic. There is no / at the end of the path in WebLogic.
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.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631598.htmlThe program code for downloading files by opening the page in the browser (php/jsp/java) Students who need to learn can refer to it one time. The configuration in tomcat is as follows: The code is as follows Copy the code mime-mapping exten...