页面端:
function jqchk(){ //jquery获取复选框值
var s='';
$('input[name="zydName"]:checked').each(function(){
s+=$(this).val()+',';
});
if(s.length>0)
{
s=s.substring(0,s.length-1);
location.href="$!webPath/usercenter/show/res/downAll.htm?ids="+s+"";
}
else
{
alert("请选中需要下载的资源单!");
}
}
服务端:
// 下载选中的
@SuppressWarnings({ "deprecation", "rawtypes", "null", "unchecked" })
@RequestMapping({ "/downAll.htm" })
public void downExcelAll(HttpServletRequest request,
HttpServletResponse response, String ids) {
String[] strID = null;
if (ids != null) {
strID = ids.split(",");
}
String accessoryId = "";
String tmpFilePath = "";
List filePaths = new ArrayList();
if (strID != null) {
for (int i = 0; i < strID.length; i++) {
Res res = resImpl.getObjById(strID[i]);
if (CommUtil.isNotNull(res)) {
accessoryId = res.getAccessoryId();
if (CommUtil.isNotNull(accessoryId)) {
Accessory accessory = accessoryService
.getObjById(accessoryId);
String ac_path = accessory.getAcPath();
String fileName = accessory.getName();
if (tmpFilePath == null || "".equals(tmpFilePath)) {
tmpFilePath = request.getRealPath("") + ac_path;
}
String filePath = request.getRealPath("") + ac_path
+ fileName;
filePaths.add(filePath);
int downNum = res.getDownNum();
downNum += 1;
res.setDownNum(downNum);
resImpl.update(res);
}
}
}
}
this.downAll(request, response, tmpFilePath, filePaths);
}
@SuppressWarnings({ "rawtypes", "unchecked", "null" })
public String downAll(HttpServletRequest request,
HttpServletResponse response,String tmpFilePath,List filePaths) {
//生成的ZIP文件名为Demo.zip
String tmpFileName = "zyd.zip";
byte[] buffer = new byte[1024];
String strZipPath = tmpFilePath + tmpFileName;
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
List fielpathss=new ArrayList();
if(filePaths!=null)
{
for (int i = 0; i < filePaths.size(); i++) {
if(new File(filePaths.get(i).toString()).exists())
{
fielpathss.add(filePaths.get(i));
}
}
}
// 需要同时下载多个文件
File[] file1 =null;
if(fielpathss.size()>0)
{
file1=new File[fielpathss.size()];
for (int i = 0; i < fielpathss.size(); i++) {
file1[i]=new File(fielpathss.get(i).toString());
}
}
if(file1!=null)
{
for (int i = 0; i < file1.length; i++) {
FileInputStream fis = new FileInputStream(file1[i]);
out.putNextEntry(new ZipEntry(file1[i].getName()));
//设置压缩文件内的字符编码,不然会变成乱码
out.setEncoding("UTF-8");
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
}
this.downFile(response,request,strZipPath,tmpFileName);
} catch (Exception e) {
logger.error("文件下载出错", e);
}
return null;
}
/**
* 文件下载
* @param response
* @param str
*/
@SuppressWarnings("deprecation")
private void downFile(HttpServletResponse response,HttpServletRequest request,String str,String tmpFileName) {
try {
String path =str;
File file = new File(path);
if (file.exists()) {
InputStream ins = new FileInputStream(path);
BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面
OutputStream outs = response.getOutputStream();// 获取文件输出IO流
BufferedOutputStream bouts = new BufferedOutputStream(outs);
response.setContentType("application/x-download");// 设置response内容的类型
response.setHeader("Content-disposition","attachment;filename="+URLEncoder.encode(tmpFileName, "UTF-8"));// 设置头部信息
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这里一定要调用flush()方法
ins.close();
bins.close();
outs.close();
bouts.close();
} else {
response.sendRedirect(""+request.getRealPath("")+"/usercenter/show/res/downerror.htm");
}
} catch (IOException e) {
e.printStackTrace();
}
}
pom依赖:
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</version>
</dependency>
分享到:
相关推荐
Java多文件压缩下载解决方案.docx
Java把文件压缩成zip,粘贴在项目中即可使用
本例是把多个文件压缩成一个zip文件,然后下载到本地。 项目完整,下载解压即可运行。
主要为大家详细介绍了java后台批量下载文件并压缩成zip下载的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java 版的文件分割压缩 可以对大文件进行拆分。 文件大小可定义
如果您想从服务器端下载多个文件,若按照传统的下载方式,这下载速度是非常慢的,我这里提供一个把所有需要下载的文件先压缩,后下载的完全解决方案。在此秀一下,工大家参考之用。
zip,然后下载删除源文件与压缩文件。 3、可以指定文件都为最后生成为zip文件下载,且删除源文件。 1、压缩文件或者目录(只是压缩不删除):zipFileOrDirectory 2、程序自动判断如果是文件那么就是源文件下载,...
java实现多个文件压缩
Java调用Zip类批量压缩多个文件,此前有一个是压缩单个文件,也可参考,相关代码中可找到此源码。 public class ZipDemo extends JFrame{ JFileChooser fileChooser; //文件选择器 JList fileList; //待...
java代码实现单个或多个文件压缩成rar包,本地要安装winRar插件。
主要为大家详细介绍了java多文件压缩下载的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java批量压缩文件跟文件夹,目前还不支持中文名,后期会改进,谢谢
主要通过java代码实现视频压缩,且提供压缩需要的jave.jar.
JAVA文件压缩与解压缩实践(源代码+论文)
这段Spring Boot代码展示了如何实现文件的批量下载与压缩。主要功能包括通过HTTP响应将多个文件打包成zip压缩包,并提供给用户一次性下载。...通过这段代码,实现了用户一次性下载多个文件的需求,提高了用户体验
JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码
java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文)....
JAVA 中单个或多个下载服务器上带超链接的文件 并压缩后下载, 如:http://172.16.10.24/recordwav/20150625/48/20150625140223_48_8003_8002_to_18064000047.wav http://www.baidu.com/img/test.png 此资源中还...