本篇功能是基于Fastdfs文件上传下载功能,作出的将文件打包成一个zip文件下载下来。
页面效果如下
选择行,点击批量下载按钮,然后就会将所有的文件打包成一个File.zip文件下载下来。解压zip文件夹,就能看到里面包含了下载的所有文件。这种下载方式不局限于xlsx文件,图片格式,文本格式,Java文件等都可以使用这种方式下载下来。
前台代码
//批量下载
function downloadFunction() {
var selected = grid.selectedDataItems();
if(selected.length>0) {
kendo.ui.showConfirmDialog({
title: "提示",
message: "确定要下载?"
}).done(function(event) {
if (event.button == "OK") {
var recpIdList = new Array();
$.each(selected,function(i,v){
recpIdList.push(v.recpId);
});
debugger;
window.location.href="${base.contextPath}/ect/fut/receipt/batchDownload?recpIdList="+recpIdList;
}});
}else{
kendo.ui.showWarningDialog({
message:"请至少选择一行"
})
}
}
上述是批量下载功能函数,第一步是获取所有行,如果没有选择行,将弹出警告框“请至少选择一行”,第二步,根据所选行去查询文件的服务器路径以及文件名,假如路径都为空,则下载一个空的zip文件,不为空,则将文件打包成zip格式下载。
后台代码
List<ZipModel> zipModelList = new ArrayList<>();
for (Long recpId : recpIds) {
FutReceipt futReceipt = new FutReceipt();
futReceipt.setRecpId(recpId.floatValue());
futReceipt = futReceiptMapper.selectByPrimaryKey(futReceipt);
//todo:仓单表主键不能为空并且文件不能为空
if (futReceipt.getRecpId() != null && futReceiptMapper.getFilePathById(futReceipt.getRecpId()) != null) {
try {
ZipModel zipModel = new ZipModel();
String filePatch = getFilePatch(futReceipt, requestContext);
//todo:存储文件路径
zipModel.setFilePath(filePatch);
//todo:获取文件后缀,并且将仓单批次作为文件名
zipModel.setFileName(futReceipt.getBatchNum() + filePatch.substring(filePatch.indexOf(".") + 1));
zipModelList.add(zipModel);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//todo:设置打包后的文件名
String fileName = "File.zip";
//todo:临时文件目录,用于存储打包的下载文件
String globalUploadPath = request.getSession().getServletContext().getRealPath("/");
String outFilePath = globalUploadPath + File.separator + fileName;
File file = new File(outFilePath);
//文件输出流
FileOutputStream outStream = new FileOutputStream(file);
//压缩流
ZipOutputStream toClient = new ZipOutputStream(outStream);
//todo:调用通用方法下载fastfds文件,打包成zip文件
ZipUtil.zipFile(zipModelList, toClient);
toClient.close();
outStream.close();
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
//todo:将zip文件下载下来
ZipUtil.downloadZip(file, response);
实现步骤主要是将所有的fastdfs的文件路径和文件名存储到一个zipModelList对象中。然后先调用通用方法zipFile将这些文件下载下来打包成一个zip文件,放在一个临时存储位置,最后再通过通用方法downloadZip将文件下载下来。
存储数据对象
ZipModel.Java
public class ZipModel {
/**
* 文件名
*/
private String fileName;
/**
* 文件在fastdfs文件服务器路径
*/
private String filePath;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
通用生成下载zip方法
ZipUtil.Java
public class ZipUtil {
private static String IFastdfsService = "hfastdfs.fastdfsfile.service.IFastdfsService";
/**
* 压缩文件列表中的文件
*
* @param files
* @param outputStream
* @throws IOException
* @throws ServletException
*/
public static void zipFile(List<ZipModel> files, ZipOutputStream outputStream) throws IOException {
try {
int size = files.size();
//压缩列表中的文件
for (int i = 0; i < size; i++) {
ZipModel zipModel = files.get(i);
try {
zipFile(zipModel, outputStream);
} catch (Exception e) {
continue;
}
}
} catch (Exception e) {
throw e;
}
}
/**
* 将文件写入到zip文件中
*
* @param zipModel
* @param outputstream
* @throws IOException
* @throws ServletException
*/
public static void zipFile(ZipModel zipModel, ZipOutputStream outputstream) throws AppException, IOException, MyException {
try {
if (zipModel != null && zipModel.getFilePath() != null && zipModel.getFileName() != null) {
IFastdfsService iFastdfsService = (IFastdfsService) getBean(IFastdfsService);
InputStream bInStream = new ByteArrayInputStream(iFastdfsService.getFile(zipModel.getFilePath()));
ZipEntry entry = new ZipEntry(zipModel.getFileName());
outputstream.putNextEntry(entry);
final int MAX_BYTE = 10 * 1024 * 1024; //最大的流为10M
long streamTotal = 0; //接受流的容量
int streamNum = 0; //流需要分开的数量
int leaveByte = 0; //文件剩下的字符数
byte[] inOutbyte; //byte数组接受文件的数据
streamTotal = bInStream.available(); //通过available方法取得流的最大字符数
streamNum = (int) Math.floor(streamTotal / MAX_BYTE); //取得流文件需要分开的数量
leaveByte = (int) streamTotal % MAX_BYTE; //分开文件之后,剩余的数量
if (streamNum > 0) {
for (int j = 0; j < streamNum; ++j) {
inOutbyte = new byte[MAX_BYTE];
//读入流,保存在byte数组
bInStream.read(inOutbyte, 0, MAX_BYTE);
outputstream.write(inOutbyte, 0, MAX_BYTE); //写出流
}
}
//写出剩下的流数据
inOutbyte = new byte[leaveByte];
bInStream.read(inOutbyte, 0, leaveByte);
outputstream.write(inOutbyte);
outputstream.closeEntry();
bInStream.close(); //关闭
}
} catch (IOException e) {
throw e;
}
}
/**
* 下载打包的文件
*
* @param file
* @param response
*/
public static void downloadZip(File file, HttpServletResponse response) {
try {
if (!file.exists()) {
file.createNewFile();
}
// 以流的形式下载文件。
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
toClient.write(buffer);
toClient.flush();
toClient.close();
file.delete(); //将生成的服务器端文件删除
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 获得bean,通过ApplicationContext获取
*
* @return
*/
public static Object getBean(String className) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
ServletContext sc = request.getSession().getServletContext();
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
Class c;
try {
c = Class.forName(className);
return ac.getBean(c);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
以上就是fastdfs文件下载的全部方法