下载txt
来源:银行招聘 发布时间:2020-09-23 点击:
不好意思,最近工作忙,没上来看贴.我在这把代码给大家下:
Java代码
<meta?http-equiv="Content-Type"?content="text/html;?charset=gbk">
<HTML>
<HEAD>
</HEAD>
<BODY>
<a?href?=?"download1.jsp?filepath=d:\\&filename=1a.txt"?>downloadtest1</a>
</BODY>
</HTML>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<HTML>
<HEAD>
</HEAD>
<BODY>
<a href = "download1.jsp?filepath=d:\\&filename=1a.txt" >downloadtest1</a>
</BODY>
</HTML>
这是页面上的连接,我传了个路径和文件名
Java代码
<%?
?String?filename?=?request.getParameter("filename");//"1a.txt";?
?String?filepath?=?request.getParameter("filepath");//"d:\\";
int?i?=?0;
?response.setContentType("application/octet-stream");
?response.setHeader("Content-Disposition","attachment;filename?=?"+filename);?
?java.io.FileInputStream?fileInputStream?=?new?java.io.FileInputStream(filepath+filename);
?while((i=?fileInputStream.read())?!=?-1){
out.write(i);
?}
%>
<%
String filename = request.getParameter("filename");//"1a.txt";
String filepath = request.getParameter("filepath");//"d:\\";
int i = 0;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename = "+filename);
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath+filename);
while((i= fileInputStream.read()) != -1){
out.write(i);
}
%>
这是被调用的download1.jsp,这个jsp就是执行直接下载文件的不管是txt还是word文档都可以直接下载
这个东东是我在一个专业编程杂志的网站花3块钱买的,嘿嘿.没办法,项目急用,今天上来看到有人需要赶紧发出来,希望3,4楼还能看到.
out.clear();
out = pageContext.pushBody();
<%@page language="java" import=".*" pageEncoding="gb2312"%>
<%
response.setContentType("application/x-download");//设置为下载application/x-download
String filenamedownload = "/shushan.txt";//即将下载的文件的相对路径
String filenamedisplay = "shushan.txt";//下载文件时显示的文件保存名称
filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
try
{
RequestDispatcher dispatcher = application.getRequestDispatcher(filenamedownload);
if(dispatcher != null)
{
dispatcher.forward(request,response);
}
response.flushBuffer();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
out.clear();
out = pageContext.pushBody();
}
%>
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,.*" pageEncoding="UTF-8"%>
<%
String txt ="实验";
System.out.println(txt);
response.setContentType("application/txt");
response.setHeader("Content-disposition", "attachment;filename=table.txt");
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(txt.getBytes());
} catch (IOException e) {
throw e;
} finally {
if (bos != null)
bos.close();
out.clear();
out = pageContext.pushBody();
}
%>
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,.*" pageEncoding="gb2312"%><%
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
String filenamedownload = "D:\\shushan.txt";
String filenamedisplay = "shushan.txt";//系统解决方案.txt
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(filenamedownload);
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;
}
out.clear();
out = pageContext.pushBody();
}
%>
推荐访问:机动风暴txt全文下载 下载 txt推荐文章
- [德国移民眼中的德国是怎样的]移民德国我后悔死了
- 2018法考成绩公布时间 天津2018年证券从业资格考试成绩查询时间:7月10日
- [新西兰留学陪读条件是什么] 新西兰留学条件及费用
- 【手抄报素材】10个寓言成语故事资料_六年级寓言成语故事手抄报大全
- 演讲比赛活动总结 [青春演讲比赛活动总结范文推荐]
- 四川成都的3所公立初中,谁最厉害?|四川成都地震厉害吗
- 北京2018年7月证券从业资格考试成绩查询入口已开通_证券从业资格证报名
- 新西兰留学安全吗 [新西兰留学的安全常识需了解]
- 【新西兰留学申请需要准备的材料有什么】新西兰留学申请材料
- [2018年新疆一级建造师报考条件、报名资格]一建挂靠费2017暴跌