qq_A许小杰_0
2018-11-16 17:25
package com.imooc.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BatchDownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attchment;filename=test.zip");
//获取下载的路径
String path=getServletContext().getRealPath("/")+"images/";
//获取需要下载的文件的列表
String[] filenames=req.getParameterValues("filename");
String str="";
String rt="\r\n";
//压缩需要下载的文件
ZipOutputStream zos=new ZipOutputStream(resp.getOutputStream());
//循环选择下载的文件
for (String filename : filenames) {
str+=filename+rt;
//文件所在的准确路径
File file =new File(path+filename);
//设置下一个需要压缩的文件条目
zos.putNextEntry(new ZipEntry(filename));
//读取需要下载的文件内容
FileInputStream fis =new FileInputStream(file);
byte b[]=new byte[1024];
int n=fis.read(b);
while(n!=-1){
zos.write(b, 0, n);
}
zos.close();
fis.close();
}
//添加注释信息,下载了哪些文件
/*zos.setComment("download success:"+rt+str);
zos.flush();
zos.close();*/
}
}
<%@ 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 '01.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">
<link rel="stylesheet" type="text/css" href="css/common.css" />
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(function(){
$(".thumbs a").click(function(){
var largePath = $(this).attr("href");
var largeAlt = $(this).attr("title");
$("#largeImg").attr({
src : largePath,
alt : largeAlt
});
return false;
});
});
</script>
</head>
<body>
<form action="smartUploadServlet.do" method="post" enctype="multipart/form-data" >
<h2>文件批量上传</h2>
上传文件1:<input type="file" name="myfile1" ><br>
上传文件2:<input type="file" name="myfile2" ><br>
上传文件3:<input type="file" name="myfile3" ><br>
<input type="submit" value="提交" >${result }
</form>
<hr>
<!-- 下载:<a href="smartDownloadServlet.do?filename=img2-lg.jpg">img2-lg.jpg</a> -->
<h2>文件批量下载</h2>
<form action="batchDownloadServlet.do">
<input type="checkbox" name="filename" value="img2-thumb.jpg">Image2<br>
<input type="checkbox" name="filename" value="img3-thumb.jpg">Image3<br>
<input type="checkbox" name="filename" value="img4-thumb.jpg">Image4<br>
<input type="submit" value="下载" >
</form>
<hr>
<h2>图片预览</h2>
<p><img id="largeImg" src="images/img1-lg.jpg" alt="Large Image"/></p>
<p class="thumbs">
<a href="images/img2-lg.jpg" title="Image2"><img src="images/img2-thumb.jpg"></a>
<a href="images/img3-lg.jpg" title="Image3"><img src="images/img3-thumb.jpg"></a>
<a href="images/img4-lg.jpg" title="Image4"><img src="images/img4-thumb.jpg"></a>
<a href="images/img5-lg.jpg" title="Image5"><img src="images/img5-thumb.jpg"></a>
<a href="images/img6-lg.jpg" title="Image6"><img src="images/img6-thumb.jpg"></a>
</p>
</body>
</html>
打一下断点,看看哪里一直循环
Java中的文件上传下载
77391 学习 · 337 问题
相似问题