实现文件下载的java代码
//这是实现下载类(servlet),详细思路代码例如以下:
//也可连接数据库
package com.message;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class FileDownServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
//得到下载文件的名字
//String filename=request.getParameter("filename");
//解决中文乱码问题
String filename=new String(request.getParameter("filename").getBytes("iso-8859-1"),"gbk");
//创建file对象
File file=new File("F://book//WebRoot//"+filename);
//设置response的编码方式
response.setContentType("application/x-msdownload");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
//设置附加文件名称
// response.setHeader("Content-Disposition","attachment;filename="+filename);
//解决中文乱码
response.setHeader("Content-Disposition","attachment;filename="+new String
(filename.getBytes("gbk"),"iso-8859-1"));
//读出文件到i/o流
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//相当于我们的缓存
long k=0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
OutputStream myout=response.getOutputStream();
//開始循环下载
while(k
int j=buff.read(b,0,1024);
k+=j;
//将b中的数据写到client的内存
myout.write(b,0,j);
}
//将写入到client的内存的数据,刷新到磁盘
myout.flush();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
//这是javabean写法 ,主要是实现和数据库连接的,假设没有连接数据库,就可直接不要这
package com;
public class MessageBean {
private String Message;
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
}
//HTML页面加载写发,也可连接数据库(案例代码也在)
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'book.jsp' starting page//Vector vv=(Vector)session.getAttribute("message");
//for(int i=0;i
// MessagesBean mb=(MessagesBean)vv.get(i);
// out.println("
");// out.println("
"+mb.getSender()+"");// out.println("
"+mb.getMess_time()+"");// out.println("
"+mb.getGeter()+"");// out.println("
"+mb.getMess_content()+"");String filename=null;
session.setAttribute("java.rar",filename);
//取得文件名称
// filename=getFilepath().substring(mb.getFilepath().lastIndexOf("/")+1);
out.println("
5.1下载书--java");//java.rar这个能够改成变量out.println("
dos视频教程");out.println("
经常使用DOS命令");out.println("
");// }
%>
//这是web.xml配置文件,这非常重要,不然,非常难实现哦
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
This is the description of my J2EE component
This is the display name of my J2EE component
FileDownServlet
com.message.FileDownServlet
FileDownServlet
/FileDownServlet