struts2 多文件批量下载

在一次项目实施中遇到客户提出对导出的单个文件进行分割为多个文件的需求,印象中未遇到过类似的文件导出功能。在网上搜索资料,研究struts2文件下载功能,未发现有支持此功能,但网上资料显示struts2支持批量文件下载功能,鉴于此思路实现代码如下。


配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="oltConfig" extends="json-default">
<action name="download" class="downLoadAction">
<result name="success" type="stream">
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<param name="contentType">
application/octet-stream;charset=ISO8859-1
</param>
<param name="fileName">
struts2中文附件.txt
</param>
<param name="inputName">
downloadFile
</param>
</result>
</action>
</package>
</struts>

java 代码部分:
/**产生压缩文件函数
*/
public ByteArrayOutputStream baleZip(String[] names,String[] fileContents) throws IOException{
String temp = "导出文件失败,详情请查看日志!";
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(bout);
for(int i=0,len=fileContents.length;i<len;i++){
try {
out.putNextEntry(new ZipEntry(names[i]));
if(fileContents[i]!=null){
out.write(fileContents[i].getBytes());
}else{
out.write(temp.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
out.flush();
out.close();
return bout;
}
/**返回下载文件流函数
*/
public InputStream getDownloadFile() {
String strResult [] = {} ;
String fileNames [] = new String [2];
String fileContents[] = new String [2];
String fileContent="";
String vendorName ="";
if(vendorId == 1){
vendorName = "zte";
}else if(vendorId == 2){
vendorName = "hw";
}else if(vendorId == 3){
vendorName = "fh";
}else if(vendorId == 4){
vendorName = "bell";
}
try {
if(vendorId.longValue() == 2){
strResult = this.genHwCmds(areaCode, frame, board, pon, svlan, vendorId, devModel,ip,upModel,downModel,
lineModel,busiModel,voicePri,ITMSPri,IPTVPri,netWorkPri);
}else if(vendorId.longValue() == 1){
strResult = this.genZteCmds(areaCode, frame, board, pon, svlan, vendorId, devModel, ip,upModel,downModel,onuType);
}else if(vendorId.longValue() == 4){
strResult = this.genBellCmds(areaCode, frame, board, pon, svlan, vendorId, devModel, ip, upModel, downModel);
}
fileContents[0] = strResult[1];
fileContents[1] = strResult[2];
if(strResult[0].trim().equals("1")){
fileContent = "导出文件内容失败,详情请查看日志!";
this.fileName = vendorName+devModel+ip+this.getDateStr(new Date())+".txt";
ByteArrayOutputStream output=new ByteArrayOutputStream();
output.write(fileContent.getBytes());
InputStream in = new ByteArrayInputStream(output.toByteArray());
log.info("导出文件成功!");
return in;
}else{
this.setFileName();
fileNames[0] = vendorName+devModel+ip+this.getDateStr(new Date())+"Add.txt";
fileNames[0] = new String(fileNames[0].getBytes("ISO8859-1"),"UTF-8");//对文件名编码
fileNames[1] = vendorName+devModel+ip+this.getDateStr(new Date())+"Del.txt";
fileNames[1] = new String(fileNames[1].getBytes("ISO8859-1"),"UTF-8");
ByteArrayOutputStream bout = this.baleZip(fileNames, fileContents);
InputStream in = new ByteArrayInputStream(bout.toByteArray());
log.info("导出文件成功!");
return in;
}
} catch (Exception ex) {
ex.printStackTrace() ;
log.info("导出文件异常!");
}
return null;
}
实现思路如下:利用ant.jar包里的文件压缩功能对多个文件进行压缩,把压缩输入流返回给struts2,然后进行下载。
注意事项:1、实现过程中,最好不要使用临时文件形式,避免在没有磁盘写权限时出现错误,利用类ByteArrayOutputStream 将文件内容转化为压缩文件输出流。
2、文件下载过程中,注意中文乱码问题,压缩文件实现方式最好调用ant.jar,不用java自带java.util.zip包,该包不支持压缩文件内含有中文名的文件(原因未研究)。
3、文件类型未知时,文件配置类型为application/octet-stream。
4、struts2导出文件大小有限制,可以在配置文件里修改默认大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值