java struts2下载zip_Struts2多文件下载

使用场景:

1)在JSP页面,有一个展现附件的列表。

2)对列表中的每一个附件,提供单独下载。

3)同时提供复选框,提供选择多个文件下载。

2b369737d2eac26dbb3568ace59aeba4.png

实现思路:

1)写一个通用的具有下载功能的Action,只需要接收一个文件路径就可以下载。单个附件的下载直接调用这个Action,只需要传递附件的路径即可。

2)多个文件下载,可以将多个文件的路径传递到一个处理Action,将多个文件打包成zip。然后重定向到通用的下载Action,同时传递zip包的路径给通用下载Action。

1、通用的下载Action。

这个Action里面有一个成员变量fileName,负责接收传递的文件路径。

package cn.luxh.struts2.action;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**

* 文件下载

* @author Luxh

*/

public class DownloadAction extends ActionSupport {

private static final long serialVersionUID = -3036349171314867490L;

//文件名

private String fileName;

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) throws UnsupportedEncodingException {

//用UTF-8重新编码文件名,解决中文乱码

this.fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");

}

public InputStream getInputStream() throws UnsupportedEncodingException, FileNotFoundException{

HttpServletResponse response = ServletActionContext.getResponse();

//attachment,以附件的方式下载文件,会打开保存文件对话框;inline,以内联的方式下载,浏览器会直接打开文件

response.setHeader("Content-Disposition", "attachment;fileName="

+ java.net.URLEncoder.encode(fileName,"UTF-8"));//java.net.URLEncoder.encode(fileName,"UTF-8") 编码转换,解决乱码

//如果fileName是相对路径

//return ServletActionContext.getServletContext().getResourceAsStream(fileName);

//如果fileName是绝对路径

return new BufferedInputStream(new FileInputStream(fileName));

}

@Override

public String execute() throws Exception {

return SUCCESS;

}

}

这个Action的配置文件:

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

application/octet-stream

inputStream

attachment;filename="${fileName}"

4096

2、处理多个附件下载的Action。

package cn.luxh.struts2.action;

import java.io.File;

import java.text.SimpleDateFormat;

import java.util.Date;

import cn.luxh.utils.ZipFileUtil;

import com.opensymphony.xwork2.ActionSupport;

/**

* 处理多个附件下载

* @author Luxh

*/

public class MultiFileDownloadAction extends ActionSupport {

private static final long serialVersionUID = 2743077909387361587L;

//接收JSP页面传递过来的附件的路径

private String attachmentPath;

//最终压缩后的zip文件的路径,传递给通用的下载Action

private String fileName;

/**

* 下载多个附件

* 实现:将多个附近压缩成zip包,然后再下载zip包

*/

public String downloadMultiFile() {

//使用当前时间生成文件名称

String formatDate =new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());

//压缩后的zip文件存放路径

fileName = "D:/test/" + formatDate + ".zip";

if(attachmentPath != null && !"".equals(attachmentPath)) {

//将多个附件的路径取出

String[] attachmentPathArray = attachmentPath.split(",");

if(attachmentPathArray != null && attachmentPathArray.length >0) {

File[] files = new File[attachmentPathArray.length];

for(int i=0;i

if(attachmentPathArray[i] != null) {

File file = new File(attachmentPathArray[i].trim());

if(file.exists()) {

files[i] = file;

}

}

}

//将多个附件压缩成zip

ZipFileUtil.compressFiles2Zip(files,fileName);

}

}

return SUCCESS;

}

public String getAttachmentPath() {

return attachmentPath;

}

public void setAttachmentPath(String attachmentPath) {

this.attachmentPath = attachmentPath;

}

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

}

配置文件:

download

/download

${fileName}

3、附件列表展现的JSP页面。

File Download

function checkFile() {

if($("#all").attr("checked")){

$("input[name='attachmentPath']").attr("checked",true);

}else {

$("input[name='attachmentPath']").attr("checked",false);

}

}

全选

文件名

文件路径

下载

${attachment.fileName}${attachment.filePath}

下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值