public static void compresszip (List(File) fileList, Outputstream outputstream)if (nuli--fileList 1)fileList.isEmpty()II null--outputStream)return
//ZIP输出流
Zipoutputstream zipoutputstream null
//将文件流或者网络流包装成ZIP输出流,
zipoutputstream new zipoutputstrear w Bufferedoutputstream(outputstream)//设置压缩方法
zipoutputstream.setMethod(zipEntry.DEFLATED);
for(File file:fileList){
//如果文件不存在,或是隐藏文件,或是目录,则过滤
if (Ifile, exists() ll file, isHidden() ll file, isDirectory()) continue;
Inputstream in new FileInputstream(file);
//添加zipEntry
zipoutputstream.putNextEntry(new ZipEntry(f41e.getName());byte[] cache.new byte[1024];
int count ;
while(count-in.read(cache))I--1){
11 详zip输出流对象写入数据
zipoutputstream.write(cache,off:e,count);
//关闭当前输入流
in.close();
1/关闭entry流
zipoutputstream.closeEntry();
} catch (FileNotFoundException e) {
e.printstackTrace();
System.out.println("文件未找到异常:"+e.getMessage());
} catch(IOException e){
e-printstackTrace();
System.out.println("读取文件异常:"+e.getMessage());
} finally {
try
if(objects.nonNull(zipoutputstream)){
// 将最后写人的数据刷入输出流
ZipQutputstream.flush();
zipoutputstream.close();
if(objects,nonNull(outputstream))
outputstream,close();
public static HttpServletResponse setDownloadResponse (HttpServletRequest request, HttpServletResponse response, String downloadione)try {
response.reset();
response.setCharacterEncoding("UTF-8"):
response.setContentType("application/octet-stream");
response.setheader(s"Content-Disposition",sl:"attachment;filename-"+FileUtils.setFileDownLoodHeader(request,downloadilame));return respons
} catch (UnsupportedEncodingException e){
e-printstackTrace();
return null;
public static String setFileDown loadHeader(HttpServletRequest request, String fileName)throws UnsupportedEncodingException(
final String agent - request-getHeader("USER-AGENT");
String filename=fileName;
if(agent.contains("MSIE")){
//IE浏览器
filename·URLEncoder.encode(filename,"utf-8");
filename=filename.replace("+", "");
}else if(agent.contains("Firefox")){
//火狐浏览器
filename = new String(fileName.getBytes(), "IS08859-1");
}else if(agent.contains("Chrome")){
// google浏览器
filename = new String(fileName.getBytes(), "IS08859-1");
// filename = URLEncoder . encode ( filename , " IS08859-1 " ) ;
}else
// 它浏览器
filename=new String(fileName.getBytes(),"utf-8");
// filename = URLEncoder.encode(filename,"utf-8");
return filename;
版权声明:本文为Mrzhang__原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。