struts2 ,spring,spring jdbc框架:
//批量下载
function batch_dowload(){
// window.open("detail/in!batchDowload");
Limit.excel({url:'detail/in!batchDowload', root:'NODE_HJMX_HRMX', keys:['start_time', 'end_time'], form:'limit_form'});
}
<pre name="code" class="java">/**
* 批量下载
*/
public void batchDowload(){
String ent_id = (String) Util.session().getAttribute("ent_id");
try {
end_time = longSdf.parse(shortSdf.format(end_time.getTime()) + " 23:59:59");
} catch (ParseException e) {
e.printStackTrace();
}
orderby = orderkey + " " + ordersc;
//数据库读取数据列表
List<Map<String, Object>> datas = service.findPageList(null, c_agent, cdr, start_time, end_time, orderby, ent_id, timeStart, timeEnd);
InputStream inStream = null;
OutputStream servletOS = null;
try {
HttpServletResponse response = Util.response();
Properties properties = FooUtil.read("/system.properties");
String osName = Util.osName();
String path = properties.getProperty("path.record." + osName);
List<File> listFile = new ArrayList<File>();
File file = new File(path);
File[] files = file.listFiles();
for(File f : files){
if(f.isFile()){
if(!f.getName().substring(f.getName().lastIndexOf(".")+1, f.getName().length()).equals("zip")){
String str_record_name = f.getName().subSequence(0, f.getName().length()).toString();
for(Map<String, Object> data : datas){
String record_name = data.get("record_name").toString();
if(record_name != null && !record_name.equals("")){
if(str_record_name.equals(record_name)){
listFile.add(f);
}
}
}
}
}
}
if(listFile != null){
Date date = new Date();
String zipFileName =DateUtil.format(date, "yyyyMMddHHmmss") + ".zip";
File strZipFile = new File(zipFileName);//最终打包的压缩包
String strZipPath = path + strZipFile;
ZipOutputStream outputStream= new ZipOutputStream(new FileOutputStream(strZipPath));
for(int i=0;i<listFile.size();i++){
File inputFile = listFile.get(i);
if(inputFile.isFile()){ //文件是否存在
FileInputStream in = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(in, 4096);
ZipEntry entry = new ZipEntry(inputFile.getName());
outputStream.putNextEntry(entry);
(如果在下载过程中,需要对打包中的文件重命名:
不能用renameTo, 此方法对文件相当于剪切,原文件就没有了。
for(Map<String, Object> data : datas){
String record_name = data.get("record_name").toString();
String agent_id = data.get("agent_id").toString();
String src = data.get("src").toString();
if(record_name != null && !record_name.equals("")){
if(inputFile.getName().equals(record_name)){
// inputFile.renameTo(new File(agent_id+src+".wav"));
ZipEntry entry = new ZipEntry(agent_id+src+".wav");
outputStream.putNextEntry(entry);
}
}
}
)
int nNumber;
byte[] buffer = new byte[4096];
while((nNumber = bins.read(buffer)) != -1){
outputStream.write(buffer, 0, nNumber);
}
outputStream.closeEntry();
bins.close();
in.close();
}
}
outputStream.close();
String zipPath = path + zipFileName;
File zipfile = new File(zipPath);
if (zipfile.exists()) {
String zipFilename = URLEncoder.encode(zipfile.getName(), "utf-8");
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; filename=\"" + zipFilename + "\"");
int zipFileLength = (int) zipfile.length();
response.setContentLength(zipFileLength);
if (zipFileLength != 0) {
inStream = new FileInputStream(zipfile);
byte[] buf = new byte[4096];
servletOS = response.getOutputStream();
int readLength;
while (((readLength = inStream.read(buf)) != -1)) {
servletOS.write(buf, 0, readLength);
}
inStream.close();
servletOS.flush();
servletOS.close();
}
}
}else {
Util.out("打包文件不存在");
}
} catch (IOException e) {
Util.out("批量下载录音为空");
}finally {
try {
if(inStream != null) {
inStream.close();
}
} catch (IOException e) {
}
try {
if(servletOS != null) {
servletOS.flush();
servletOS.close();
}
} catch (IOException e) {
}
}
}