本篇功能是基于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:"請至少選擇一行"
})
}
}
上述是批量下載功能函數(shù)唯笙,第一步是獲取所有行螟蒸,如果沒有選擇行,將彈出警告框“請至少選擇一行”睁本,第二步尿庐,根據(jù)所選行去查詢文件的服務(wù)器路徑以及文件名,假如路徑都為空呢堰,則下載一個空的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:設(shè)置打包后的文件名
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:調(diào)用通用方法下載fastfds文件,打包成zip文件
ZipUtil.zipFile(zipModelList, toClient);
toClient.close();
outStream.close();
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
//todo:將zip文件下載下來
ZipUtil.downloadZip(file, response);
實現(xiàn)步驟主要是將所有的fastdfs的文件路徑和文件名存儲到一個zipModelList對象中骂维。然后先調(diào)用通用方法zipFile將這些文件下載下來打包成一個zip文件惹资,放在一個臨時存儲位置,最后再通過通用方法downloadZip將文件下載下來航闺。
存儲數(shù)據(jù)對象
ZipModel.Java
public class ZipModel {
/**
* 文件名
*/
private String fileName;
/**
* 文件在fastdfs文件服務(wù)器路徑
*/
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; //流需要分開的數(shù)量
int leaveByte = 0; //文件剩下的字符數(shù)
byte[] inOutbyte; //byte數(shù)組接受文件的數(shù)據(jù)
streamTotal = bInStream.available(); //通過available方法取得流的最大字符數(shù)
streamNum = (int) Math.floor(streamTotal / MAX_BYTE); //取得流文件需要分開的數(shù)量
leaveByte = (int) streamTotal % MAX_BYTE; //分開文件之后,剩余的數(shù)量
if (streamNum > 0) {
for (int j = 0; j < streamNum; ++j) {
inOutbyte = new byte[MAX_BYTE];
//讀入流,保存在byte數(shù)組
bInStream.read(inOutbyte, 0, MAX_BYTE);
outputstream.write(inOutbyte, 0, MAX_BYTE); //寫出流
}
}
//寫出剩下的流數(shù)據(jù)
inOutbyte = new byte[leaveByte];
bInStream.read(inOutbyte, 0, leaveByte);
outputstream.write(inOutbyte);
outputstream.closeEntry();
bInStream.close(); //關(guān)閉
}
} 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(); //將生成的服務(wù)器端文件刪除
} 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文件下載的全部方法