怎么通过按钮下载自己web项目里的某个文件 为什么我下面的代码下载出来的文件总是空的呢
<%@pagelanguage="java"import="java.util.*,java.net.*"pageEncoding="gbk"%><%@pageimpor...
<%@ page language="java" import="java.util.*, java.net.*" pageEncoding="gbk"%>
<%@ page import="pro.dao.*, pro.entity.*"%>
<%
request.setCharacterEncoding("gbk");
int bid=Integer.parseInt(request.getParameter("bid"));
bookInfo book=new bookDao().selectByID(bid);
response.setContentType("application/x-download"); //设置下载application/x-download
String filedownload="a.txt"; //即将下载下载(在项目中确实存在)
String fileddisplay=book.getBName()+".txt"; //下载时显示的文件保存名称
String filenamedisplay=URLEncoder.encode(fileddisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename="+filenamedisplay);
try{
RequestDispatcher dis=application.getRequestDispatcher(filedownload);
if(dis!=null){
dis.forward(request,response);
}
response.flushBuffer();
}catch(Exception ex){
ex.printStackTrace();
}
%> 展开
<%@ page import="pro.dao.*, pro.entity.*"%>
<%
request.setCharacterEncoding("gbk");
int bid=Integer.parseInt(request.getParameter("bid"));
bookInfo book=new bookDao().selectByID(bid);
response.setContentType("application/x-download"); //设置下载application/x-download
String filedownload="a.txt"; //即将下载下载(在项目中确实存在)
String fileddisplay=book.getBName()+".txt"; //下载时显示的文件保存名称
String filenamedisplay=URLEncoder.encode(fileddisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename="+filenamedisplay);
try{
RequestDispatcher dis=application.getRequestDispatcher(filedownload);
if(dis!=null){
dis.forward(request,response);
}
response.flushBuffer();
}catch(Exception ex){
ex.printStackTrace();
}
%> 展开
2个回答
展开全部
你的这种想发挺不错的,但是我认为forword不会说是这样的一个作用。它是负责转发请求,而不是处理流的,虽然是有流的一个作用,但是机制应当是将文件处理后放到一个流中传到前台。那么所设置的若干response应当是无效的,会呗祛除掉。所以下载还是看下面代码,规规矩矩的使用IO流
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.text.*, java.io.*, com.stxdalian.mro.util.*" %>
<%
out.clear();
out = pageContext.pushBody();
request.setCharacterEncoding("UTF-8");
String fname = java.net.URLDecoder.decode(request.getParameter("fn"),"utf-8");
String oname = java.net.URLDecoder.decode(request.getParameter("on"),"utf-8");
String upDir ="";
String dname = upDir + fname;
File fp = new File(dname);
String fsize = String.valueOf(fp.length());
byte b[] = new byte[Integer.parseInt(fsize)];
response.setHeader("Content-type","application/unknowon");
response.setHeader("Content-Disposition","attachment; filename=\"" + java.net.URLEncoder.encode(oname, "UTF-8") +"\"");
response.setHeader("Content-Length",fsize+";");
response.setHeader("Content-Transfer-Encoding","binary");
response.setHeader("Pragma","no-cache");
response.setHeader("Expires","0");
if(fp.isFile()){
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(fp));
BufferedOutputStream fon = new BufferedOutputStream(response.getOutputStream());
int read = 0;
try{
while((read = fin.read(b)) != -1){
fon.write(b,0,read );
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(fon!=null)
fon.close();
if(fin!=null)
fin.close();
}
}
%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.text.*, java.io.*, com.stxdalian.mro.util.*" %>
<%
out.clear();
out = pageContext.pushBody();
request.setCharacterEncoding("UTF-8");
String fname = java.net.URLDecoder.decode(request.getParameter("fn"),"utf-8");
String oname = java.net.URLDecoder.decode(request.getParameter("on"),"utf-8");
String upDir ="";
String dname = upDir + fname;
File fp = new File(dname);
String fsize = String.valueOf(fp.length());
byte b[] = new byte[Integer.parseInt(fsize)];
response.setHeader("Content-type","application/unknowon");
response.setHeader("Content-Disposition","attachment; filename=\"" + java.net.URLEncoder.encode(oname, "UTF-8") +"\"");
response.setHeader("Content-Length",fsize+";");
response.setHeader("Content-Transfer-Encoding","binary");
response.setHeader("Pragma","no-cache");
response.setHeader("Expires","0");
if(fp.isFile()){
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(fp));
BufferedOutputStream fon = new BufferedOutputStream(response.getOutputStream());
int read = 0;
try{
while((read = fin.read(b)) != -1){
fon.write(b,0,read );
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(fon!=null)
fon.close();
if(fin!=null)
fin.close();
}
}
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询