JavaEE实现文件下载
我们先来看一个最简单的文件下载的例子:
package com.yyz.response;
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 ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String realpath = this.getServletContext().getRealPath("/download/1.gif");
String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
response.setHeader("content-disposition", "attachment;filename="+filename);
//服务器通过这个头,告诉浏览器以下载方式打开数据
FileInputStream in = new FileInputStream(realpath);
int len = 0;
byte buffer[]=new byte[1024];
OutputStream out = response.getOutputStream();
while((len = in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
//out不用close,response在销毁的时候服务器会自动关闭与response相关的流。
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
该段代码的功能是从服务器端下载图片1.png。目录结构用MyEclipse的package explorer显示如下:
让我们增加一点难度,我们要下载的文件是一个中文名字的文件。由于在http协议中头文件中的东西只能是ASCII字符,因而通过上述方式(直接将 String realpath = this.getServletContext().getRealPath("/download/1.gif");改为 String realpath = this.getServletContext().getRealPath("/download/图片.gif");)直接拿文件,
会出现乱码问题。附上测试结果:
要解决这个问题,要用到 URLEncoder类的encode方法:
package com.yyz.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//中文文件下载时,中文文件名要经过URL编码。
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String realpath = this.getServletContext().getRealPath("/download/图片.gif");
String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
//本函数将字符串以 URL 编码
FileInputStream in = new FileInputStream(realpath);
int len = 0;
byte buffer[]=new byte[1024];
OutputStream out = response.getOutputStream();
while((len = in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
附上测试结果:
另外这里有一个小细节需要大家注意:
不能用FileReader代替FileInputStream。用FileReader会丢失数据,原因是这样的:FileReader是字符流,而图片,媒体文件等的数据都是以01的方式存储,用FileReader读的时候需要查阅一个编码表,如果未指定一种编码,则使用相应平台的默认编码。如在中国的电脑就会去查GB2312。当读到GB2312码表中不存在的编码时,会将该数据编码成'?',结束后数据就变成中文和'?'的混合。发到客户端后显示时再次查阅码表,将所有的'?'替换成'?'的编码,就会丢失数据。向这种细节只需要记住一点:字节流可以处理任意类型的数据,字符流只能处理字符数据。