在response.getOutputStream()实现文件下载,如果是多个文件可以把压缩为一个zip文件
package com.xu.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xu.utils.DownloadFile;
/**
* Servlet implementation class FileDownloadServlet
*/
@WebServlet(name="download",urlPatterns = {"/FileDownloadServlet", "/download"})
public class FileDownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FileDownloadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("op").equalsIgnoreCase("download")) {
DownloadFile file=new DownloadFile();
String[] files= {"E:/1.txt","E:/3.txt","E:/2.txt"};
try {
file.multFileDownload(response, files, "a.zip");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.xu.utils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings(value="all")
public class DownloadFile {
/**
* 1 多文件下载
* @param response HttpServletResponse
* @param files[] 待下载文件
* @param downloadfile 下载文件文件名+文件类型(a.zip)
* @throws Exception
* @date 2019年3月29日11:31:35
*/
public void multFileDownload(HttpServletResponse response,String[] files,String downloadfile) throws Exception {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName="+downloadfile);
ServletOutputStream out;
FileInputStream instream = null;
try {
ZipOutputStream zipstream=new ZipOutputStream(response.getOutputStream());
for (String file:files) {
if (!new File(file).exists()) {
continue;
}
instream=new FileInputStream(file);
ZipEntry entry = new ZipEntry(file);
zipstream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = 0;
while (len != -1){
len = instream.read(buffer);
zipstream.write(buffer,0,buffer.length);
}
instream.close();
zipstream.closeEntry();
zipstream.flush();
}
zipstream.finish();
zipstream.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 2 多文件下载
* @param response HttpServletResponse
* @param files[] 待下载文件
* @param downloadfile 下载文件
* @param path 待下载文件路径
* @param tempfile 待下载临时文件
* @throws Exception
* @date 2019年3月29日11:31:35
*/
public void multFileDownload(HttpServletResponse response,String[] files,String path,String downloadfile) throws Exception {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName="+downloadfile);
ServletOutputStream out;
FileInputStream instream = null;
try {
ZipOutputStream zipstream=new ZipOutputStream(response.getOutputStream());
for (String file:files) {
if (!new File(path+file).exists()) {
continue;
}
instream=new FileInputStream(path+file);
ZipEntry entry = new ZipEntry(path+file);
zipstream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = 0;
while (len != -1){
len = instream.read(buffer);
zipstream.write(buffer,0,buffer.length);
}
instream.close();
zipstream.closeEntry();
zipstream.flush();
}
zipstream.finish();
zipstream.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 1 单文件下载
* @param response HttpServletResponse
* @param filepath 待下载文件路径
* @param downloadfile 下载文件
* @date 2019年3月29日11:31:35
*/
public void singleFileDownload(HttpServletResponse response,String filepath,String downloadfile) {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName="+downloadfile);
ServletOutputStream out;
File file = new File(filepath);
if (!file.exists()) {
new RuntimeException("文件不存在");
}
try {
FileInputStream inputStream = new FileInputStream(file);
out = response.getOutputStream();
byte[] buffer = new byte[1024];
int b = 0;
while (b != -1){
b = inputStream.read(buffer);
out.write(buffer,0,b);
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 2 单文件下载
* @param response HttpServletResponse
* @param filepath 待下载文件路径
* @param filename 下载文件名称
* @param filetype 下载文件类型
* @date 2019年3月29日11:31:35
*/
public void singleFileDownload(HttpServletResponse response,String filepath,String filename,String filetype) {
response.setContentType("multipart/form-data");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader("Content-Disposition", "attachment;fileName="+filename+filetype);
ServletOutputStream out;
File file = new File(filepath);
if (!file.exists()) {
new RuntimeException("文件不存在");
}
try {
FileInputStream inputStream = new FileInputStream(file);
out = response.getOutputStream();
byte[] buffer = new byte[1024];
int b = 0;
while (b != -1){
b = inputStream.read(buffer);
out.write(buffer,0,b);
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
new RuntimeException(e.getMessage());
}
}
/**
* 1 多文件 压缩成 ZIP
* @param files 文件名字
* @param path 文件路径
* @param loaclfilepath 临时文件全路径
* @date 2019年3月29日11:31:35
*/
public void toZip(String[] files,String path,String loaclfilepath) throws Exception {
FileInputStream instream = null;
ZipOutputStream zipstream=new ZipOutputStream(new FileOutputStream(new File(loaclfilepath)));
for (String file:files) {
if (!new File(path+file).exists()) {
continue;
}
instream=new FileInputStream(path+file);
ZipEntry entry = new ZipEntry(path+file);
zipstream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = 0;
while (len != -1){
len = instream.read(buffer);
zipstream.write(buffer,0,buffer.length);
}
zipstream.closeEntry();
zipstream.flush();
}
zipstream.finish();
zipstream.close();
}
/**
* 2 多文件 压缩成 ZIP
* @param files 文件名字
* @param path 文件路径
* @param loaclfilepath 压缩文件全路径
* @date 2019年3月29日11:31:35
*/
public void toZip(List<String> files,String path,String loaclfilepath) throws Exception {
FileInputStream instream = null;
ZipOutputStream zipstream=new ZipOutputStream(new FileOutputStream(new File(loaclfilepath)));
for (String file:files) {
if (!new File(path+file).exists()) {
continue;
}
instream=new FileInputStream(path+file);
ZipEntry entry = new ZipEntry(path+file);
zipstream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = 0;
while (len != -1){
len = instream.read(buffer);
zipstream.write(buffer,0,buffer.length);
}
zipstream.closeEntry();
zipstream.flush();
}
zipstream.finish();
zipstream.close();
}
}