`
java_fei
  • 浏览: 21133 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

JAVA实现文件下载

    博客分类:
  • JAVA
阅读更多
//使用servlet页面进行操作
package com.download;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileDownServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String CONTENT_TYPE = "text/html; charset=utf-8";
    /**
     * 使用doGet方法
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
    request.setCharacterEncoding("utf-8");
        response.setContentType(CONTENT_TYPE);
        //得到下载文件的名字
        String filename=request.getParameter("filename");
        //创建file对象
        File file=new File("E://temp//"+filename);
        //设置response的编码方式
        response.setContentType("application/x-msdownload");
        //写明要下载的文件的大小
        response.setContentLength((int)file.length());
        //发送到客户端的文章
        response.setHeader("Content-Disposition","attachment;filename="+filename);       
        //读出文件到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<file.length()){
            int j=buff.read(b,0,1024);
            k+=j;
            //将b中的数据写到客户端的内存
            myout.write(b,0,j);
        }
        //将写入到客户端的内存的数据,刷新到磁盘
        myout.flush();
    }
}
//html页面
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>download.xhtml</title>
<meta http-equiv="keywords" content="enter,your,keywords,here" />
<meta http-equiv="description" content="A short description of this page." />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
</head>
<body>
<p>
<a href=fileDownServlet.do?filename=cc.doc>5.1下载书--java</a>
</p>
</body>
</html>
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>FileDownServlet</servlet-name>
    <servlet-class>com.download.FileDownServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>FileDownServlet</servlet-name>
    <url-pattern>/fileDownServlet.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>download.html</welcome-file>
  </welcome-file-list>
 
</web-app>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics