http下载小实例

前端download.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'book.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>
<%=path %>
<body>
<p><a href=DownFileServlet?filename=java.rar>java</a></p>
<p><a href=DownFileServlet?filename=dos.rar>dos</a></p>
<p><a href=DownFileServlet?filename=com.rar>com</a></p>
</body>
</html>


后台servlet:


package com.guohui.test.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class DownFileServlet
*/
public class DownFileServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 7627015152811667298L;
private static final String CONTENT_TYPE = "text/html; charset=utf-8";

// 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对象
//String fileDir = System.getProperty("user.dir",".")+"/dos.rar";// + File.separator + filename;
//File file = new File(fileDir);

InputStream is=getServletContext().getResourceAsStream( "resource"+File.pathSeparator+filename );

String fileDir2 = this.getServletContext().getRealPath("resource");

File file = new File(fileDir2+"/"+filename);
System.out.println(fileDir2+"/"+filename);

System.out.println(File.separator);
//File.pathSeparator// :
//File.separator // /


// 设置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 < file.length()) {

int j = buff.read(b, 0, 1024);
k += j;

// 将b中的数据写到客户端的内存
myout.write(b, 0, j);

}

// 将写入到客户端的内存的数据,刷新到磁盘
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() {
}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值