链接信息 | 前台压缩下载后台的图片 |
function downloadZipFill() {
function download(){ var selections=$("#table").datagrid("getSelections");var tag = true; if(selections.length==0){ $.messager.alert("系统提示", "请选择需要下载的记录,同时确定记录下存在二维码!", "info"); }else{ $.messager.confirm("系统提示", "确认并下载?", function(b){ if(b){ $.showLoad(); var tt_ids=new Array(); // var ccj_status_array = new Array(); $.each(selections, function (i, o) { tt_ids.push(o.tt_id) }); if(tag){ $.ajaxPost("${path}${path}/trans/tower_downloadFills.do", {"tt_ids":tt_ids}, function(result) { if(!downloadZipFill()){ $.hideLoad(); } if("0"!=result){ $.messager.show({ title:"消息提醒", msg:"下载成功!", timeout:5000, showType:"slide" }); } else{ alert("选择项无二维码,请生成后下载!") ; } }); } } }); } }
/** * @param request * @param response * @return * @throws Exception */ String zipPath = ""; @ActionAnnotation(name = "压缩下载", group = "下载", log = true) public ModelAndView downloadFills(HttpServletRequest request, HttpServletResponse response) throws Exception { String[] tt_ids = BeanUtil.wrapArray(String.class, request.getParameter("tt_ids")); zipPath = towerService.downloadFills(tt_ids); String result = zipPath; return responseText(response, result); } /** * @param request * @param response * @return * @throws Exception */ @ActionAnnotation(name = "下载附件", group = "下载", log = true) public ModelAndView downloadZipFill(HttpServletRequest request, HttpServletResponse response) throws Exception { FileDownLoad fileDownLoad = new FileDownLoad(response); fileDownLoad.write(zipPath); response.flushBuffer(); towerService.deleteFile(); //下载后删除文件 return null; } /** * 得到附件 * * @param sf_id * @return * @throws Exception */ public Files getFj(String sf_id) throws Exception { return sqlDao.query("sys_file.queryBlob", new Files("sf_id", sf_id)); } String zipPath = ""; public boolean makeDirs(String[] tt_ids) throws Exception { zipPath = "杆塔二维码"; //使用日期作为压缩文件的文件名 ServletContext context = getServletContext(); zipPath = context.getRealPath("") + "\\" + zipPath + "\\" + "code" + DateUtil.timeToStr2(new Date()); //定义path 与盘符无关 createDir(zipPath); //在c盘创建相应文件目录 for (String tt_id : tt_ids) { Tower tower = query(tt_id); if (!StringUtil.isEmpty(tower) && !StringUtil.isEmpty(tower.getTt_picurl())) { File t = createFile(zipPath + "\\" + tower.getTt_picurl()); File pic = new File(servletContext.getRealPath("/ECODE/" + tower.getTt_picurl())); if (pic.exists() && pic.isFile() && t.exists() && t.isFile()) { FileUtil.copy(pic, t); //将图片拷贝到path路径下 } } } return true; } /** * @param tt_ids * @return * @throws Exception */ public String downloadFills(String[] tt_ids) throws Exception { if (!StringUtil.isEmpty(tt_ids)) { makeDirs(tt_ids); } if (!"".equals(zipPath)) { zip(zipPath, zipPath + ".zip"); return zipPath + ".zip"; } else { return null; } } //----------------文件操作 /** * 创建文件夹 * * @param path * @return */ private File createDir(String path) { File dirFile = null; try { dirFile = new File(path); if (!(dirFile.exists()) && !(dirFile.isDirectory())) { dirFile.mkdirs(); //创建文件夹 } } catch (Exception e) { e.printStackTrace(); } return dirFile; } /** * 创建文件 * * @param path * @return */ private File createFile(String path) { File file = new File(path); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } return file; } // 删除文件 /** * @param path * @return * @throws Exception */ public boolean delFile(String path) throws Exception { boolean result = false; File file = new File(path); if (file.exists() && file.isFile()) { file.delete(); result = true; } return result; } // 删除文件及文件夹 /** * @param folder * @return */ private boolean delDir(File folder) { boolean result = false; try { String childs[] = folder.list(); if (childs == null || childs.length <= 0) { if (folder.delete()) { result = true; } } else { for (int i = 0; i < childs.length; i++) { String childName = childs[i]; String childPath = folder.getPath() + File.separator + childName; File filePath = new File(childPath); if (filePath.exists() && filePath.isFile()) { if (filePath.delete()) { result = true; } else { result = false; break; } } else if (filePath.exists() && filePath.isDirectory()) { if (delDir(filePath)) { result = true; } else { result = false; break; } } } } folder.delete(); } catch (Exception e) { e.printStackTrace(); result = false; } return result; } public void deleteFile() { if (!StringUtil.isEmpty(zipPath)) { deleteFile(zipPath); deleteFile(zipPath+".zip"); }zipPath = "";//删除后将文件赋空值 } /** * 删除文件、文件夹 */ public void deleteFile(String path) { File file = new File(path); if (file.isDirectory()) { File[] ff = file.listFiles(); for (int i = 0; i < ff.length; i++) { deleteFile(ff[i].getPath()); } } file.delete(); } public void zip(String inputFileName, String zipFileName) throws Exception { System.out.println(zipFileName); zip(zipFileName, new File(inputFileName)); } private void zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFileName)); out.setEncoding("gbk"); zip(out, inputFile, ""); System.out.println("zip done"); out.close(); } private void zip(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { //判断是否为目录 File[] fl = f.listFiles(); // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/")); out.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { //压缩目录中的所有文件 // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; System.out.println(base); while ((b = in.read()) != -1) { out.write(b); } in.close(); } } FileUtil:
public static void copy(File s,File t){ |
欢迎关注我的公众号,会有不一样的惊喜。