第一种:
就是直接给出下载的地址,这种方式很不好,因为会暴露你的地址,带来很多不安全的因素,可以说是千万不要用这种
第二种:
下载页面
<%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
<html>
<head>
<title>download</title>
</head>
<body>
<ahref="xia.jsp?filename=新建文档.txt">新建文档.txt</a>
</body>
</html>
然后编写如下页面
<%@pagecontentType="text/html;charset=gbk"%>
<%@pagelanguage="java"import="java.io.*,java.net.*"pageEncoding="gbk"%>
<html>
<head>
<title>test</title>
</head>
<body>
<%
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();
}
%>
</body>
</html>
第三种方法
使用servlet首先配置web.xml
<servlet>
<servlet-name>DownloadFile</servlet-name>
<servlet-class>libin123.com.cn.servlet.DownloadFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadFile</servlet-name>
<url-pattern>/downloadfile</url-pattern>
</servlet-mapping>
编写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("<!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//EN>");
out.println("<HTML>");
out.println("<HEAD><TITLE>AServlet</TITLE></HEAD>");
out.println("<BODY>");
out.print("Thisis");
out.print(this.getClass().getName());
out.println(",usingthePOSTmethod");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
下载页面
<%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
<html>
<head>
</head>
<body>
<ahref="downloadfile?filename=新建文档.txt">新建文档.txt</a>
</body>
</html>