java 文件下载 jsp,[JAVAWEB实战篇]---jsp文件下载

jsp文件下载完整方法

第一种:

就是直接给出下载的地址,这种方式很不好,因为会暴露你的地址,带来很多不安全的因素,可以说是千万不要用这种

第二种:

下载页面

download

新建文档.txt

然后编写如下页面

test

response.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值