81,091
社区成员
发帖
与我相关
我的任务
分享
<%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%>
<%@page import="java.io.*"%>
<%
//关于文件下载时采用文件流输出的方式处理:
//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
//application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径
String filedownload = "E:/download/1.pptx";
String filedisplay = "1.pptx";
response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
java.io.OutputStream outp = null;
java.io.FileInputStream in = null;
try
{
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
//
outp.flush();
//要加以下两句话,否则会报错
//java.lang.IllegalStateException: getOutputStream() has already been called for //this response
out.clear();
out = pageContext.pushBody();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in != null)
{
in.close();
in = null;
}
//这里不能关闭
//if(outp != null)
//{
//outp.close();
//outp = null;
//}
}
%>
<%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%>
<%
String filename=request.getParameter("student.pdf");//传递过来的 文件名如:11.doc
int type=Convert.parseToInt32(request.getParameter("type"),0);
String folder="";
if(type==1)
{
folder="law";
}
else
{
folder="std";
}
out.print(folder);
File txtfile=new File(AppConfig.getDataDirBase()+"app_water_mon/archive/"+folder+"/index.txt");
String filekuozhan="pdf";//文件的扩展名 如doc txt rar
String filetrue="student"; //扩展名前面部分 如11.doc,22.txt,33.rar中的11,22,33
if(filename!=null)
{
StringTokenizer kz= new StringTokenizer(filename,".") ;
filetrue=kz.nextToken();
filekuozhan=kz.nextToken();
//out.print(filekuozhan);
}
String title=request.getParameter("filetitle");
out.print(title);
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
String filedownload =filename;//文件名
String filedisplay = title+"."+filekuozhan;//下载文件时显示的文件保存名称
String filenamedisplay =java.net.URLEncoder.encode(filedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try
{
outp = response.getOutputStream();
//文件的路径 如:d:/app_water_mon/archive/law/11.doc
File truefile=new File(AppConfig.getDataDirBase()+"download\"+folder+"\"+filename);
in = new FileInputStream(truefile);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
outp.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in != null)
{
in.close();
in = null;
}
if(outp != null)
{
outp.close();
outp = null;
}
}%>