81,092
社区成员
发帖
与我相关
我的任务
分享
//文件的物理路径
String filePath = "";
File file = new File(filePath);
if(!file.exists()){
//TODO 这里可以写点提示
return;
}
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(),"utf-8"));
OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(file);
int l;
byte[] b = new byte[128];
while((l = fis.read(b))>0) {
os.write(b, 0, l);
}
fis.close();
os.close();
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'index.jsp' 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>
<a href="download.jsp">下载</a>
</body>
</html>
<%@ page import="java.io.*, java.net.*" pageEncoding="utf-8"%>
<%
response.reset();
//文件的物理路径
String filePath = System.getProperty("user.home") + "/1.mp3";
File file = new File(filePath);
if(!file.exists()){
return;
}
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(),"utf-8"));
OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(file);
int l;
byte[] b = new byte[128];
while((l = fis.read(b))>0) {
os.write(b, 0, l);
}
fis.close();
os.close();
out.clear();
out = pageContext.pushBody();
%>
<input type="file">