jsp文件下载完整方法
第一种:
就是直接给出下载的地址,这种方式很不好,因为会暴露你的地址,带来很多不安全的因素,可以说是千万不要用这种
第二种:
下载页面
download新建文档.txt
然后编写如下页面
testresponse.setContentType("text/html");
javax.servlet.ServletOutputStreamou=response.getOutputStream();
Stringfilepath="uploadfile/";
Stringfilename=newString(request.getParameter("filename").getBytes("ISO8859_1"),"GB2312").toString();
System.out.println("DownloadFilefilepath:"+filepath);
System.out.println("DownloadFilefilename:"+filename);
java.io.Filefile=newjava.io.File(filepath+filename);
if(!file.exists()){
System.out.println(file.getAbsolutePath()+"文件不存在!");
return;
}
//读取文件流java.io.FileInputStreamfileInputStream=newjava.io.FileInputStream(file);
//下载文件//设置响应头和下载保存的文件名if(filename!=null&&filename.length()>0){
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;filename="+newString(filename.getBytes("gb2312"),"iso8859-1")+"");
if(fileInputStream!=null){
intfilelen=fileInputStream.available();
//文件太大时内存不能一次读出,要循环bytea[]=newbyte[filelen];
fileInputStream.read(a);
ou.write(a);
}
fileInputStream.close();
ou.close();
}
%>
第三种方法
使用servlet首先配置web.xml
DownloadFile
libin123.com.cn.servlet.DownloadFile
DownloadFile
/downloadfile
编写DownloadFile.java类
packagelibin123.com.cn.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.sun.image.codec.jpeg.ImageFormatException;
importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGImageDecoder;
publicclassDownloadFileextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
response.setContentType("text/html");
javax.servlet.ServletOutputStreamout=response.getOutputStream();
Stringfilepath=request.getRealPath("/")+"uploadfile/";
Stringfilename=newString(request.getParameter("filename").getBytes("ISO8859_1"),"GB2312").toString();
System.out.println("DownloadFilefilepath:"+filepath);
System.out.println("DownloadFilefilename:"+filename);
java.io.Filefile=newjava.io.File(filepath+filename);
if(!file.exists()){
System.out.println(file.getAbsolutePath()+"文件不存在!");
return;
}
//读取文件流java.io.FileInputStreamfileInputStream=newjava.io.FileInputStream(file);
//下载文件//设置响应头和下载保存的文件名if(filename!=null&&filename.length()>0){
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;filename="+newString(filename.getBytes("gb2312"),"iso8859-1")+"");
if(fileInputStream!=null){
intfilelen=fileInputStream.available();
//文件太大时内存不能一次读出,要循环bytea[]=newbyte[filelen];
fileInputStream.read(a);
out.write(a);
}
fileInputStream.close();
out.close();
}
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
out.println("/W3C//DTDHTML4.01Transitional//EN>");
out.println("");
out.println("
AServlet");out.println("
");out.print("Thisis");
out.print(this.getClass().getName());
out.println(",usingthePOSTmethod");
out.println("");
out.println("");
out.flush();
out.close();
}
}
下载页面
新建文档.txt