下載之后就停不下來,一直在下載 都下載1個G了 也沒有顯示文件大小
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());
//循環(huán)選擇下載的文件
for (String filename : filenames) {
str+=filename+rt;
//文件所在的準(zhǔn)確路徑
File file =new File(path+filename);
//設(shè)置下一個需要壓縮的文件條目
zos.putNextEntry(new ZipEntry(filename));
//讀取需要下載的文件內(nèi)容
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>圖片預(yù)覽</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>
2019-01-02
打一下斷點,看看哪里一直循環(huán)