java文件下载

网上有很多文件下载的实现,但很多都存在一些问题,我在写的时候也遇到了问题,如文件乱码,为防以后再犯所以就有了这篇文章,废话不说,直接贴码

servlet中

@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String fileName = URLDecoder.decode(request.getParameter("fileName"), "UTF-8");
		System.out.println(fileName);
		String filePath = FileUtil.UPLOAD_PATH + "\\" + fileName;//下载文件路径
		if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
	    	fileName = URLEncoder.encode(fileName, "UTF-8");//IE浏览器
		else 
			fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");//其它浏览器
		File file = new File(filePath);
		if (file.exists()) {
			int fileLength = (int) file.length();
			if (fileLength != 0) {
				response.reset();
				response.setContentType("application/octet-stream");
				response.setHeader("Content-disposition", "attachment;filename="+fileName);
				response.setContentLength(fileLength);
				
				InputStream is = new  FileInputStream(filePath);
				byte[] buffer = new byte[1024];
				ServletOutputStream fos = response.getOutputStream();
				int len = 0;
				while ((len = is.read(buffer)) != -1)
					fos.write(buffer, 0, len);
				is.close();
				fos.flush();
				fos.close();
			}
		}
	}
jsp中

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File down page</title>
<script type="text/javascript">
	function downfile(name) {
//		alert(name);
		/* var xmlhttp;
		if (window.XMLHttpRequest)
			xmlhttp = new XMLHttpRequest();
		else 
			xmlhttp = new ActiveXObject("MIcrosoft.XMLHTTP");
//		xmlhttp = createRequestObject();//用此方法创建xmlHttpRequest,避免浏览器兼容问题
		xmlhttp.open("GET", "DownLoadFile?fileName=" + encodeURI(encodeURI(name)), true);
		xmlhttp.send(); */
		window.location.href="DownLoadFile?fileName=" + encodeURI(encodeURI(name));
	}
	
</script>
</head>
<body>
	<table width="100%" border="1">
		<tr>
			<td width="60%" align="center">文件名称</td>
			<td width="40%" align="center">操作</td>
		</tr>
		<c:forEach items="${fileList}" var="f">
			<tr>
				<td>${f }</td>
				<td>
					<input type="button" name="downfile" value="文件下载" οnclick="downfile('${f}')">
				</td>
			</tr>
		</c:forEach>
	</table>
	
</body>
</html>
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>FileOperation</display-name>
	
	
	<!-- 文件下载 -->
	<servlet>
		<description></description>
		<display-name>DownLoadFile</display-name>
		<servlet-name>DownLoadFile</servlet-name>
		<servlet-class>com.dengsilinming.file.servlet.DownLoadFile</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>DownLoadFile</servlet-name>
		<url-pattern>/DownLoadFile</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
文件下载时标题的乱码问题得到了解决,在IE、FireFox、Google Chrome中都已经测试过。
项目源码可以到 这里下载,源码中还包括一个简单的文件上传功能(逻辑有点乱,没有完善)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值