如何让页面一打开就远程自动下载文件
比如在页面这样写,当然,最好在后台写:
<%@ page language="java" import="java.util.*" pageencoding="gbk"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>">
<title>my jsp 'test.' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
// 得到文件名字和路径
string filename = "example.zip";
string filepath = "d:\\";
// 设置响应头和下载保存的文件名
response.setcontenttype("application/octet-stream");
response.setheader("content-disposition",
"attachment; filename=\"" + filename + "\"");
// 打开指定文件的流信息
java.io.fileinputstream fileinputstream =
new java.io.fileinputstream(filepath + filename);
// 写出流信息
int i;
while ((i=fileinputstream.read()) != -1) {
out.write(i);
}
fileinputstream.close();
out.close();
%>
</body>
</html>
上一篇: win10怎么进入和退出安全模式?
下一篇: 介绍一下Mysql的统计行数与影响行数